我附上了以下程序代码,用于显示分数的实际值,构建成功,但程序因线程错误而崩溃。你能告诉我,我在哪里犯了错误。非常感谢您的帮助。
谢谢
小牛10
#import<Foundation/Foundation.h>
@interface Fraction : NSObject
{
int numerator;
int denominator; // variables to be used
}
-(void) print;
-(void) setNumerator:(int) n;
-(void) setDenominator:(int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum; // new method declared
@end
@implementation Fraction
-(void) print
{
NSLog(@"%i/%i",numerator,denominator);
}
-(void) setNumerator :(int) n
{
numerator=n;
}
-(void) setDenominator :(int)d
{
denominator=d;
}
-(int)numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
-(double) convertToNum
{
if(denominator!=0) // checking for denominator to be non zero
return (double) numerator/denominator;
else
return NAN;
}
@end
//Program Section
int main(int argc, char*argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
Fraction * aFraction =[[Fraction alloc] init];
Fraction * bFraction =[[Fraction alloc] init];
[aFraction setNumerator:1]; // setting the numerator for the first fraction.Similarly denominator
[aFraction setDenominator:4];
[aFraction print];
NSLog(@"= ");
NSLog(@"%g",[aFraction convertToNum]);
[bFraction print];
NSLog(@"=");
NSLog(@"%g",[bFraction convertToNum]);
[aFraction release];
[bFraction release];
[pool drain];
return 0;
}