OC 便利构造器 自定义初始化 分数加减乘除

//OC 便利构造器  自定义初始化  分数加减乘除

main.m

#import <Foundation/Foundation.h>

#import "Fraction.h"


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


    Fraction *value = [Fraction fractionWithFenzi:7 withFenmu:5];

    Fraction *value1 = [Fraction fractionWithFenzi:4 withFenmu:5];

    [value reduce];

    [value print];

    [[value addFraction:value1] print];

    [[value minusFraction:value1] print];

    [[value mulFraction:value1] print];

    [[value divFraction:value1] print];

    return 0;

}




Fraction.h


@interface Fraction : NSObject {

    NSInteger _fenzi;//分子

    NSInteger _fenmu;//分母

}


- (instancetype)initWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu;

//便利构造器

+ (instancetype)fractionWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu;

//约分

- (void)reduce;

- (void)reduce1;

- (void)print;

//get

- (NSInteger )getFenzi;

- (NSInteger )getFenmu;

//

- (Fraction *)addFraction:(Fraction *)anotherFraction;

//

- (Fraction *)minusFraction:(Fraction *)anotherFraction;

//

- (Fraction *)mulFraction:(Fraction *)anotherFraction;

//

- (Fraction *)divFraction:(Fraction *)anotherFraction;


Fraction.m

@implementation Fraction


//自定义初始化

- (instancetype)initWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu {

    self = [super init];

    if (self) {

        _fenzi = fenzi;

        _fenmu = fenmu;

    }

    return self;

}

//便利构造器

+ (instancetype)fractionWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu {

    return [[Fraction alloc] initWithFenzi:fenzi withFenmu:fenmu];

}

//约分

- (void)reduce {

    NSInteger min = _fenzi < _fenmu ? _fenzi : _fenmu;

    for (NSInteger i = min; i >= 0; i--) {

        if (_fenzi % i == 0 && _fenmu % i == 0) {

            _fenzi /= i;

            _fenmu /= i;

            break;

        }

    }

}

//约分2

- (void)reduce1 {

    NSInteger a = _fenzi;

    NSInteger b = _fenmu;

    NSInteger c = 0;

    while (a % b) {

        c = a % b;

        a = b;

        b = c;

    }

    _fenzi /= b;

    _fenmu /= b;

}

//打印

- (void)print {

    NSLog(@"%ld/%ld", _fenzi, _fenmu);

}


- (NSInteger)getFenzi {

    return _fenzi;

}

- (NSInteger)getFenmu {

    return _fenmu;

}

//

- (Fraction *)addFraction:(Fraction *)anotherFraction {

    //定义分数对象存结果作为返回值

    Fraction *value = [[Fraction alloc] init];

    value->_fenmu = [anotherFraction getFenmu] * _fenmu;

    value->_fenzi = [anotherFraction getFenzi] * _fenmu + [anotherFraction getFenmu] * _fenzi;

    [value reduce];

    return value;

}

#pragma mark -

#pragma mark 减法

//

- (Fraction *)minusFraction:(Fraction *)anotherFraction {

    Fraction *value = [[Fraction alloc] init];

    value->_fenmu = [anotherFraction getFenmu] * _fenmu;

    value->_fenzi = [anotherFraction getFenmu] * _fenzi - [anotherFraction getFenzi] * _fenmu;

        [value reduce1];

    return value;

}

#pragma mark -

#pragma mark 乘法

//

- (Fraction *)mulFraction:(Fraction *)anotherFraction {

    Fraction *value = [[Fraction alloc] init];

    value->_fenmu = [anotherFraction getFenmu] * _fenmu;

    value->_fenzi = [anotherFraction getFenzi] * _fenzi;

    [value reduce1];

    return value;

}

//

- (Fraction *)divFraction:(Fraction *)anotherFraction {

    Fraction *value = [[Fraction alloc] init];

    value->_fenzi = [anotherFraction getFenmu] * _fenzi;

    value->_fenmu = [anotherFraction getFenzi] * _fenmu;

    [value reduce1];

    return value;

}


@end



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值