标签:
// Fraction 分数 // Complex 复数
#import <Foundation/Foundation.h> #import "Fraction.h" #import "Complex.h" int main(int argc, constchar * argv[]) { @autoreleasepool {
Fraction *f1 = [Fractionnew]; Fraction *f2 = [Fractionnew]; //Fraction *ff;
Complex *c1 = [Complexnew]; Complex *c2 = [Complexnew]; //Complex *cc;
// id dataValue; // f1.fenZi = 1; // f1.fenMu = 10; // dataValue = f1; // [dataValue print]; //
// f2.fenZi = 2; // f2.fenMu = 15; // // [f1 print]; // [f1 add:f2]; // [f1 reduce]; // [f1 print]; // // c1.shiBu = 2; // c1.xuBu = 3.5; // c2.shiBu = 4.3; // c2.xuBu = 3; // [c1 print]; // [c2 print]; // [c1 add:c2]; // [c1 print];
} return 0; }
|
#import <Foundation/Foundation.h> @interface Fraction : NSObject @propertyint fenZi, fenMu;
-(void)print; -(void)add: (Fraction *) f; -(void)reduce;
@end
|
#import "Fraction.h" @implementation Fraction
@synthesize fenZi, fenMu;
-(void)print{ NSLog(@"%i/%i",fenZi,fenMu); }
-(void)add: (Fraction *) f{
fenZi = fenZi * f.fenMu + fenMu * f.fenZi; fenMu = fenMu * f.fenMu; }
-(void)reduce{ int u = fenZi; int v = fenMu; int temp;
while (v != 0) { temp = u % v; u = v; v = temp; } fenZi /= u; fenMu /= u; }
@end
|
#import <Foundation/Foundation.h> @interface Complex : NSObject
@propertyfloat shiBu, xuBu;
-(void)print; -(void)add: (Complex *)p; @end
|
#import "Complex.h" @implementation Complex @synthesize shiBu, xuBu;
-(void)print{
NSLog(@"%.2f + %.2f i",shiBu,xuBu); } -(void)add: (Complex *)p{
shiBu = shiBu + p.shiBu; xuBu = xuBu + p.xuBu; } @end
|
标签:
原文地址:http://www.cnblogs.com/GhostKZShadow/p/5105194.html