Q: Does the fraction class apply to negative fractions? For example: Can 1/4-1/2 be displayed as-1/4?
1 //--------class definition implementation refer to the previous essay, where you need to change the print method and main Main method Section----------2- (void) Print: (BOOL) Yesorno//Print3 {4 if(Yesorno = =YES) {5 [self reduce];6 }7 8 if(Num <0)9 {Tennum =-num; OneNSLog (@"-%i/%i", Num,den); A } - Else - { theNSLog (@"%i/%i", Num,den); - } - } - +-(Fraction *) subfraction: (Fraction *) F - { +Fraction *result =[[Fraction alloc]init]; A atResult.num = num * F.den-den *F.num; -Result.den = den *F.den; - - //[result reduce]; - - returnresult; in - } to + - #import<Foundation/Foundation.h> the #import "Fraction.h" * $ intMainintargcConst Char*argv[]) {Panax Notoginseng @autoreleasepool { -Fraction *MYF =[[Fraction alloc]init]; theFraction *myf_2 =[[Fraction alloc]init]; +Fraction *Myresult; A the intI//accept input 1 or 0 +BOOL b;//Boolean type to determine the value of I - $[MyF SETN:1ANDSETD:4]; $[Myf_2 SETN:1ANDSETD:2]; - -NSLog (@"need to simple? 1 or 0");//prompt to enter 1 or 0 thescanf"%i",&i); -b = i;//Pass the I value to the Boolean type B to determine whether to reduce the scoreWuyi theMyresult = [MyF subfraction:myf_2];//Subtraction - [Myresult print:b]; Wu -Myresult = [MyF mulfraction:myf_2];//multiplication About //[Myresult print]; $ -Myresult = [MyF divfraction:myf_2];//Division - [Myresult print:b]; - A //[Myresult print]; + the - } $ return 0; the}
Objective-c Seventh Post-lesson Exercise 3