OC 创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。 要求:为类添加属性

#import 
/*创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。
 
 要求:为类添加属性
 */
@interface Fraction : NSObject
{
    NSInteger _denominator;  //分母
    NSInteger _member;       //分子
}
@property (nonatomic) NSInteger denominator;  //分母
@property (nonatomic) NSInteger member;       //分子
//初始化方法
- (id)initWithDenominator:(NSInteger)denominator member:(NSInteger)member;
//分数相加
- (Fraction *)add:(Fraction *)num1;
//分数相减
- (Fraction *)subtract:(Fraction *)num1;
//分数相乘
- (Fraction *)multiply:(Fraction *)num1;
//分数相除
- (Fraction *)division:(Fraction *)num1;
//分数比较大小
- (NSComparisonResult)compare:(Fraction *)num1;
//分数约分
- (void)reductionOfFraction;
//求最大公约数
- (NSInteger)getMaxDivisorWithNumber:(NSInteger)number1 number2:(NSInteger)number2;
@end





#import "Fraction.h"
@implementation Fraction
@synthesize denominator = _denominator;
@synthesize member = _member;
//初始化方法
- (id)initWithDenominator:(NSInteger)denominator member:(NSInteger)member
{
    self = [super init];
    if (self) {
        self.denominator = denominator;
        self.member = member;
    }
    return self;
}
//求最大公约数
- (NSInteger)getMaxDivisorWithNumber:(NSInteger)number1 number2:(NSInteger)number2
{
    NSInteger  temp = 0;
    if (number1 < 0) {
        number1 = -number1;
    }
    while (number2 != 0) {
        temp = number1 % number2;
        number1 = number2;
        number2 = temp;
    }
    return number1;
}
//分数约分
- (void)reductionOfFraction
{
    NSInteger maxDivisor = [self getMaxDivisorWithNumber:self.member number2:self.denominator];
    self.member /= maxDivisor;
    self.denominator /= maxDivisor;
}
//分数相加
- (Fraction *)add:(Fraction *)num1
{
    Fraction *num = [[Fraction alloc] init];
    num.member = self.member * num1.denominator + num1.member * self.denominator;
    num.denominator = self.denominator * num1.denominator;
    [num reductionOfFraction];
    return num;
}
//分数相减
- (Fraction *)subtract:(Fraction *)num1
{
    Fraction *num = [[Fraction alloc] init];
    num.member = self.member * num1.denominator - num1.member * self.denominator;
    num.denominator = self.denominator * num1.denominator;
    [num reductionOfFraction];
    return num;
}
//分数相乘
- (Fraction *)multiply:(Fraction *)num1
{
    Fraction *num = [[Fraction alloc] init];
    num.member = self.member * num1.member;
    num.denominator = self.denominator * num1.denominator;
    [num reductionOfFraction];
    return num;
}
//分数相除
- (Fraction *)division:(Fraction *)num1
{
    Fraction *num = [[Fraction alloc] init];
    num.member = self.member * num1.denominator;
    num.denominator = self.denominator * num1.member;
    [num reductionOfFraction];
    return num;
}
//分数比较大小
- (NSComparisonResult)compare:(Fraction *)num1
{
    NSInteger a = self.member * num1.denominator;
    NSInteger b = num1.member * self.denominator;
    if (a > b) {
        return NSOrderedDescending;
    }else if(a < b){
        return NSOrderedAscending;
    }else{
        return NSOrderedSame;
    }
}
//重写description
- (NSString *)description
{
    NSString *str = nil;
    if (self.member % self.denominator == 0) {
        str = [NSString stringWithFormat:@"%ld", self.member / self.denominator];
    }else{
    str = [NSString stringWithFormat:@"%ld/%ld", self.member, self.denominator];
    }
    return str;
}
@end



@autoreleasepool {
        Fraction *number1 = [[Fraction alloc] initWithDenominator:6 member:0];
        Fraction *number2 = [[Fraction alloc] initWithDenominator:6 member:6];
        //加法
        Fraction *addNum = [number1 add:number2];
        NSLog(@"%@", addNum);
        //约分
        [number1 reductionOfFraction];
        NSLog(@"%@", number1);

        //减法
        Fraction *subNum = [number1 subtract:number2];
        NSLog(@"%@", subNum);
        //乘法
        Fraction *mulNum = [number1 multiply:number2];
        NSLog(@"%@", mulNum);
        //除法
        Fraction *divNum = [number1 division:number2];
        NSLog(@"%@", divNum);
        //比较大小
        NSInteger a =  [number1 compare:number2];
        NSLog(@"%ld",a);       
    }



 
  


你可能感兴趣的:(OC编程,OC属性)