試圖對分數寫操作簡單的程序。 不能編譯的代碼。獲得一個「期望的標識符或‘(’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;
}
老兄,你有行號嗎? –
Xcode->首選項 - >文本編輯 - >顯示:行號 – Caleb
真棒迦勒!但數字也複製時,我複製代碼行? –