OC---分数的加、减、乘、除、比较大小

//Fraction.h

@interface Fraction : NSObject


//属性

@property(nonatomic, assign) int numerator;//分子

@property(nonatomic, assign) int denominator;//分母


//初始化方法

- (id)initWithNumerator:(int)numerator

         andDenominator:(int)denominator;

//分数相加

- (Fraction *)add:(Fraction *)fraction;


//分数相减

- (Fraction *)sub:(Fraction *)fraction;


//分数相乘

- (Fraction *)mul:(Fraction *)fraction;


//分数相除

- (Fraction *)div:(Fraction *)fraction;


//分数比较

- (Fraction *)compareValues:(Fraction *)fraction;


//分数约分

- (void)reduce;


//计算结果

- (void)printResult;


@end



//Fraction.m

#import "Fraction.h"


@implementation Fraction


//初始化方法

- (id)initWithNumerator:(int)numerator

         andDenominator:(int)denominator;

{

    self = [super init];

    if (self != nil) {

        self.numerator = numerator;

        self.denominator = denominator;

    }

    return self;

}


//分数相加

- (Fraction *)add:(Fraction *)fraction

{

    int newNu = self.numerator * fraction.denominator + self.denominator * fraction.numerator;

    int newDe = self.denominator * fraction.denominator;

    Fraction *f = [[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];

    return f;

}


//分数相减

- (Fraction *)sub:(Fraction *)fraction

{

    int newNu = self.numerator * fraction.denominator - self.denominator * fraction.numerator;

    int newDe = self.denominator * fraction.denominator;

    Fraction *f = [[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];

    return f;

}


//分数相乘

- (Fraction *)mul:(Fraction *)fraction

{

    int newNu = self.numerator * fraction.numerator;

    int newDe = self.denominator * fraction.denominator;

    Fraction *f = [[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];

    return f;

}


//分数相除

- (Fraction *)div:(Fraction *)fraction

{

    int newNu = self.numerator * fraction.denominator;

    int newDe = self.denominator * fraction.numerator;

    Fraction *f  =[[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];

    [f reduce];

    return f;

}


//分数比较

- (Fraction *)compareValues:(Fraction *)fraction

{

    int newNu1 = self.numerator * fraction.denominator;//分子1

    int newNu2 = self.denominator * fraction.numerator;//分子2

    //分母相同,比较分子的大小

    if (newNu1 > newNu2) {

        return self;

    } else {

        return fraction;

    }

}


//分数约分

- (void)reduce

{

    int gcd = [self gcdWithNum1:self.numerator andNum2:self.denominator];

    self.numerator = self.numerator / gcd;

    self.denominator = self.denominator / gcd;

}


//辗转相除法 求两个分数的最大公约数

- (int)gcdWithNum1:(int)num1

           andNum2:(int)num2

{

    while (num2 != 0) {

        int temp = num1 % num2;

        num1 = num2;

        num2 = temp;

    }

    return num1;

}


//输出结果

- (void)printResult

{

    NSLog(@"%d/%d", self.numerator, self.denominator);

}


@end


//main.m

#import <Foundation/Foundation.h>


#import "Fraction.h"

#import "Book.h"

#import "BookManager.h"


int main(int argc, const char * argv[]) {


    //创建对象

    Fraction *f1 = [[Fraction alloc] initWithNumerator:1 andDenominator:3];

    Fraction *f2 = [[Fraction alloc] initWithNumerator:3 andDenominator:-5];

    //相加

    Fraction *add = [f1 add:f2];

    [add printResult];

    //相减

    Fraction *sub = [f1 sub:f2];

    [sub printResult];

    //相乘

    Fraction *mul = [f1 mul:f2];

    [mul printResult];

    //相除

    Fraction *div = [f1 div:f2];

    [div printResult];

    //比较

    Fraction *com = [f1 compareValues:f2];

    [com printResult];



    return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值