//Fraction.h#import<Foundation/Foundation.h>@interfaceFraction:nsobject@property (nonatomic, assign) Nsinteger above;//molecule@property (nonatomic, assign) Nsinteger below;//Denominator//Initialize- (ID) Initwithname: (Nsinteger) above below: (Nsinteger) below;//Greatest Common Divisor-(Nsinteger) Maxgy: (Nsinteger) above below: (Nsinteger) below;//Numerator- (void) simply;//Add- (void) Sumfraction: (Fraction *) fraction;//Subtract- (void) subfraction: (Fraction *) fraction;//Multiply- (void) Mulfraction: (Fraction *) fraction;//Divide- (void) Divfraction: (Fraction *) fraction;//Output-(NSString *) description;@end
. m file
//fraction.m#import "Fraction.h"@implementationFraction//Initialize- (ID) Initwithname: (Nsinteger) above below: (Nsinteger) below{ Self=[Super Init]; if(self) {self.above=above; Self.below=below; } returnSelf ;}//Greatest Common Divisor-(Nsinteger) Maxgy: (Nsinteger) above below: (Nsinteger) below{Nsinteger a= above%below; while(A! =0) {above=below; Below=A; A= above%below; } returnbelow; }//Numerator- (void) simply{Nsinteger b=[self maxGY:self.above below:self.below]; Self.above/=b; Self.below/=b; }//Add- (void) Sumfraction: (Fraction *) fraction{Self.above= Self.above * Fraction.below + fraction.above *Self.below; Self.below= Self.below *Fraction.below; [Self simply]; }- (void) subfraction: (Fraction *) fraction{Self.above= Self.above * Fraction.below-fraction.above *Self.below; Self.below= Self.below *Fraction.below; [Self simply]; }- (void) Mulfraction: (Fraction *) fraction{Self.above= Self.above *Fraction.above; Self.below= Self.below *Fraction.below; [Self simply]; }- (void) Divfraction: (Fraction *) fraction{Self.above= Self.above *Fraction.below; Self.below= Self.below *Fraction.above; [self simply];}//Output-(NSString *) description{return[NSString stringWithFormat:@"%lu/%lu", Self.above, Self.below];}@end
Main. File implementation
//main.m#import<Foundation/Foundation.h>#import "Fraction.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//Create a fraction class (fraction) to achieve the subtraction of the score, more than??、 numerator and other methods. Requirements: Adding attributes to a classFraction *FR1 = [[Fraction alloc]initwithname:2Below4]; Fraction*FR2 = [[Fraction alloc]initwithname:3Below5]; [FR1 SUMFRACTION:FR2]; NSLog (@"%@", FR1); [FR1 SUBFRACTION:FR2]; NSLog (@"%@", FR1); [FR1 MULFRACTION:FR2]; NSLog (@"%@", FR1); [FR1 DIVFRACTION:FR2]; NSLog (@"%@", FR2); } return 0;}
For reference only
Create a fraction class (fraction) to achieve the subtraction of the score, more than??、 numerator and other methods. Requirement: Add property.