2012年5月23日 星期三

Categories

refer to Addison Wesley - Programming.in.ObjectiveC.2.0.2nd (2009)

#import <stdio.h>
// Define the Fraction class
@interface Fraction : NSObject
@property int numerator, denominator;
-(void)  setTo: (int) n over: (int) d;
//-(Fraction *) add: (Fraction *) f;  移除它
-(void)   reduce;
-(double) convertToNum;
-(void)   print;
@end

然後加入
#import “ Fraction.h ”  //如果使用另一個檔案來設計
@interface Fraction (MathOps)  //設定一個Categories 去加入method for the class of Fraction
-(Fraction *) add: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
@end

@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
Fraction *result = [[Fraction alloc] init];
int     resultNum, resultDenom;
resultNum = (numerator * f.denominator) +
(denominator * f.numerator);
resultDenom = denominator * f.denominator;
[result setTo: resultNum over: resultDenom];
[result reduce];
return result;
}
-(Fraction *) sub: (Fraction *) f
{
// To sub two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b * d)
Fraction *result = [[Fraction alloc] init];
int      resultNum, resultDenom;
resultNum = (numerator * f.denominator) -
(denominator * f.numerator);
resultDenom = denominator * f.denominator;
[result setTo: resultNum over: resultDenom];
[result reduce];
return result;
}
-(Fraction *) mul: (Fraction *) f
{
Fraction  *result = [[Fraction alloc] init];
[result setTo: numerator * f.numerator
over: denominator * f.denominator];
[result reduce];
return result;
}
-(Fraction *) div: (Fraction *) f
{
Fraction  *result = [[Fraction alloc] init];
[result setTo: numerator * f.denominator
over: denominator * f.numerator];
[result reduce];
return result;
}
@end

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *a = [[Fraction alloc] init];
Fraction *b = [[Fraction alloc] init];
Fraction *result;
[a setTo: 1 over: 3];
[b setTo: 2 over: 5];
[a print]; NSLog (@ ” + ” ); [b print]; NSLog (@ ” ----- ” );
result = [a add: b];
[result print];
NSLog (@ ” \n ” );
[result release];
[a print]; NSLog (@ ” - ” ); [b print]; NSLog (@ ” ----- ” );
result = [a sub: b];
[result print];
NSLog (@ ” \n ” );
[result release];
[a print]; NSLog (@ ” * ” ); [b print]; NSLog (@ ” ----- ” );
result = [a mul: b];
[result print];
NSLog (@ ” \n ” );
[result release];
[a print]; NSLog (@ ” / ” ); [b print]; NSLog (@ ” ----- ” );
result = [a div: b];
[result print];
NSLog (@ ” \n ” );
[result release];
[a release];
[b release];
[pool drain];
return 0;
}

沒有留言: