Fraction 演算に関する簡単なプログラムを作成しようとしています。コードをコンパイルできません。「subtract」メソッド周辺のインターフェースで「予期される識別子または '(' xcode」エラーを取得します。
私はその問題について調査を行いましたが、それはある種の一般的なエラーのようです。これが私のプログラムです:
#import <Foundation/Foundation.h>
// Define the Fraction class
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(Fraction *) add: (Fraction *) f;
–(Fraction *) subtract: (Fraction *) f;
–(Fraction *) multiply: (Fraction *) f;
–(Fraction *) divide: (Fraction *) f;
-(void) reduce;
@end
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return NAN;
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
// add a Fraction to the receiver
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator +
denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
-(Fraction *) subtract: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator -
denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
-(Fraction *) multiply: (Fraction *) f
{
Fraction *result= [[Fraction alloc] init];
result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
-(Fraction *) divide: (Fraction *) f;
{
Fraction *result = [[Fraction alloc] init];
result.numerator= numerator * f.denominator;
result.denominator = denominator * f.numerator;
[result reduce];
return result;
}
-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
@end
#import "Fraction.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction1 = [[Fraction alloc] init];
Fraction *myFraction2 = [[Fraction alloc] init];
[myFraction2 setTo:200 over:300];
[myFraction1 setTo: 100 over: 300];
[myFraction2 subtract: myFraction1];
NSLog (@"The value of myFraction2 is:");
[myFraction2 print];
[myFraction2 release];
[myFraction1 release];
[pool drain];
return 0;
}