Create a fraction class (fraction) to achieve the subtraction of the score, more than??、 numerator and other methods. Requirement: Add property.

Source: Internet
Author: User
Tags greatest common divisor

//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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.