Examples of adding fractions

Source: Internet
Author: User
Tags greatest common divisor

The method is: a/b+c/d= ((a*d) + (B*C))/(B*D)
(1). h file
#import <Foundation/Foundation.h>

@interface Fraction: nsobject //fraction score

@property int Numerator,denominator ; //denominator denominator numerator molecule
-(void ) print;
-(void ) Setto: (int ) n over:(int ) D;
-(double ) Converttonum;     //convertion convert
-(void ) Add: (fraction *) F;
-(void ) reduce;               //reduce decrease

@end
(2). m file
#import "Fraction.h"

@implementation Fraction

@synthesize Numerator,denominator;
-(void) print
{
NSLog(@ "%i/%i",numerator, Denominator);
}

-(Double) Converttonum
{
   if(Denominator!=0)
    {
       return(Double)Numerator/Denominator;
    }
   Else
       returnNAN;
}
-(void) Setto: (int) n over: (int) d
{
   Numerator=n;
   Denominator=d;
}

//AddFractionto message Acceptance
-(void) Add: (Fraction*) F
{
   //add two scores:
   //a/b+c/d= ((a*d) + (B*C))/(B*D)//Two fractional addition of another method of finding
   
   Numerator=Numerator* F.Denominator+Denominator* F.Numerator; //Here's*it means the ride.
   Denominator=Denominator* F.Denominator;
}

-(void) Reduce
{
   intu=Numerator;
   intv=Denominator;
   inttemp;
   
    while(v!=0) {
Temp=u%v;
U=v; The method of seeking greatest common divisor
v=temp;
    }
   Numerator/=u; These two steps are the reduction of the score
   Denominator/=u;
}

@end

Examples of adding fractions

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.