refer to Addison Wesley - Programming.in.ObjectiveC.2.0.2nd (2009)
Complex.h
@interface Complex: NSObject
{
double real;
double imaginary;
}
@property double real, imaginary;
-(void) print;
-(void) setReal: (double) a andImaginary: (double) b;
-(Complex *) add: (Complex *) f;
@end
Complex.m
#import “ Complex.h ”
@implementation Complex
@synthesize real, imaginary;
-(void) print
{
NSLog (@ ” %g + %gi “ , real, imaginary);
}
-(void) setReal: (double) a andImaginary: (double) b
{
real = a;
imaginary = b;
}
-(Complex *) add: (Complex *) f
{
Complex *result = [[Complex alloc] init];
[result setReal: real + [f real]
andImaginary: imaginary + [f imaginary]];
return result;
}
@end
main.m
#import “ Fraction.h ”
#import “ Complex.h ”
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];
Fraction *fracResult;
Complex *c1 = [[Complex alloc] init];
Complex *c2 = [[Complex alloc] init];
Complex *compResult;
[f1 setTo: 1 over: 10];
[f2 setTo: 2 over: 15];
[c1 setReal: 18.0 andImaginary: 2.5];
[c2 setReal: -5.0 andImaginary: 3.2];
// add and print 2 complex numbers
[c1 print]; NSLog (@ ” + ” ); [c2 print];
NSLog (@ ” --------- ” );
compResult = [c1 add: c2]; // add belong to complex class
[compResult print];
NSLog (@ ” \n ” );
[c1 release];
[c2 release];
[compResult release];
// add and print 2 fractions
[f1 print]; NSLog (@ ” + ” ); [f2 print];
NSLog (@ ” ---- ” );
fracResult = [f1 add: f2]; // add belong to fraction class
[fracResult print];
[f1 release];
[f2 release];
[fracResult release];
[pool drain];
return 0;
}
沒有留言:
張貼留言