3. 定义一个分数类(Fraction) 实例变量:分子,分母 方法:初始化方法(2个参数),便利构造器,约分,打印,加,减,乘,除。

<span style="font-size:18px;">                                  <span style="color:#FF6666;">main.m</span>                             </span>
<span style="font-size:18px;">#import <Foundation/Foundation.h>
#import "Person.h"
#import "Fraction.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {

        Fraction *num = [[Fraction alloc] init];
        [num print]; //打印初始化的值 3/5
        Fraction *num1 = [Fraction fractionWithNumerator:24 denominator:36];
        [num1 print]; //打印便利构造器值 24/36
        [num1 reductionOfFraction];
        [num1 print]; // 24/36 约分后 2/3
        
        Fraction *num2 = [Fraction fractionWithNumerator:9 denominator:12];
        Fraction *result1 = [Fraction addWitfAFraction:num1 bFraction:num2];// 加   24/36 + 2/3
        [result1 print];
        Fraction *result2 = [Fraction minusWitfAFraction:num1 bFraction:num2];// 减
        [result2 print];
        Fraction *result3 = [Fraction timeWitfAFraction:num1 bFraction:num2]; // 乘
        [result3 print];
        Fraction *result4 = [Fraction divideWitfAFraction:num1 bFraction:num2]; // 除
        [result4 print];
        
    }
    return 0;
}
</span>
<span style="color:#FF6666;">                                 Fraction.h</span>
#import <Foundation/Foundation.h>

@interface Fraction : NSObject {
    NSInteger _numerator;//分子
    NSInteger _denominator;//分母
}
//自定义初始化方法
- (id)initWithNumerator:(NSInteger)aNumerator;
- (id)initWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator;
//便利构造器
+ (Fraction *)fractionWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator;

//setter和getter
- (void)setNumerator:(NSInteger)aNumerator;
- (NSInteger)numerator;
- (void)setDenominator:(NSInteger)aDenominator;
- (NSInteger)denominator;

//最大公约数:辗转相除法
- (NSInteger)greatestCommonDivisorWithOneNumber:(NSInteger)oneNumber anotherNumber:(NSInteger)anotherNumber;
//约分
- (void)reductionOfFraction;
//打印
- (void)print;
//加
+ (Fraction *)addWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
//减
+ (Fraction *)minusWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
//乘
+ (Fraction *)timeWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
//除
+ (Fraction *)divideWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction;
@end

<span style="color:#FF6666;">                                  Fraction.m</span>
#import "Fraction.h"

@implementation Fraction
- (instancetype)init
{
    self = [super init];
    if (self) {
        _numerator = 3;
        _denominator = 5;
    }
    return self;
}
//自定义初始化方法
- (id)initWithNumerator:(NSInteger)aNumerator {
    self = [super init];
    if (self) {
        _numerator = aNumerator;
    }
    return self;
}
- (id)initWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator {
    self = [super init];
    if (self) {
        _numerator = aNumerator;
        _denominator = aDenominator;
    }
    return self;
}

//便利构造器
+ (Fraction *)fractionWithNumerator:(NSInteger)aNumerator denominator:(NSInteger)aDenominator {
    Fraction *fraction = [[Fraction alloc] init];
    [fraction setNumerator:aNumerator];
    [fraction setDenominator:aDenominator];
    return fraction;
}

//setter和getter
- (void)setNumerator:(NSInteger)aNumerator {
    _numerator = aNumerator;
}
- (NSInteger)numerator {
    return _numerator;
}
- (void)setDenominator:(NSInteger)aDenominator {
    _denominator = aDenominator;
}
- (NSInteger)denominator {
    return _denominator;
}

//最大公约数:辗转相除法
- (NSInteger)greatestCommonDivisorWithOneNumber:(NSInteger)oneNumber anotherNumber:(NSInteger)anotherNumber {
    while (anotherNumber != 0) {
        NSInteger temp = oneNumber % anotherNumber;
        oneNumber = anotherNumber;
        anotherNumber = temp;
    }
    return oneNumber;
}
//约分
- (void)reductionOfFraction {
    NSInteger temp = [self greatestCommonDivisorWithOneNumber:_numerator anotherNumber:_denominator];
    //self即可代指类, 也可代指对象, 谁调用就代表谁.
    _numerator /= temp;
    _denominator /= temp;
}
//打印
- (void)print {
    NSLog(@"分数: %ld/%ld",_numerator, _denominator);
}
//加
+ (Fraction *)addWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction {
    Fraction *result = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.denominator;
    result.numerator = aFraction.numerator * bFraction.denominator + bFraction.numerator * aFraction.denominator;
    [result reductionOfFraction];//加完后整体约分
    return result;
}
//减
+ (Fraction *)minusWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction {
    Fraction *result = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.denominator;
    result.numerator = aFraction.numerator * bFraction.denominator - bFraction.numerator * aFraction.denominator;
    [result reductionOfFraction];
    return result;
}
//乘
+ (Fraction *)timeWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction{
    Fraction *result  = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.denominator;
    result.numerator = aFraction.numerator * bFraction.numerator;
    [result reductionOfFraction];
    return result;
}
//除
+ (Fraction *)divideWitfAFraction:(Fraction *)aFraction bFraction:(Fraction *)bFraction {
    Fraction *result = [[Fraction alloc] init];
    result.denominator = aFraction.denominator * bFraction.numerator;
    result.numerator = aFraction.numerator * bFraction.denominator;
    [result reductionOfFraction];
    return  result;
}
@end


你可能感兴趣的:(OC源代码,分数加减乘除,约分)