5.20 一个混合算法与面向对象思想的例子

1,被动申明
2,方法的封装
3,方法的调用

/
//  Fraction.h
//  5.19_0C_property_HW
//
//  Created by rimi on 15/5/19.
//  Copyright (c) 2015年 rectinajh. All rights reserved.
//

#import <Foundation/Foundation.h>
//枚举类型,限制算法的加减乘除
typedef enum _Operation
{
    Plus = -1,
    Substration ,
    Multiplocation ,
    Dividing ,

}Operation;

@interface Fraction : NSObject

@property (assign,nonatomic)NSInteger molecular; //分子
@property (assign,nonatomic)NSInteger denominator;//分母

//- (instancetype)initWithmolecular :(NSInteger)molecular1
//                    andDenominator:(NSInteger)denominator1;
//
//
//- (void)print; //显示分数
//- (void)add : (Fraction *) Frac;//分数的相加
//- (Fraction *)substrate :(Fraction *)Frac;//分数的减法
//- (Fraction *)multiply  :(Fraction *)Frac;//乘法
//- (Fraction *)divide :(Fraction *)Frac;//除法



- (Fraction *)commonDenominator:(Fraction *)other;
- (Fraction *) plus:(Fraction *)other;
- (Fraction *)substration:(Fraction *)other;
- (Fraction *)multiplocation:(Fraction *)other;
- (Fraction *)dividing:(Fraction *)other;

- (Fraction *)calculationWithFraction:(Fraction *)other
                            Operation:(Operation)operation;


@end
//
//  Fraction.m
//  5.19_0C_property_HW
//
//  Created by rimi on 15/5/19.
//  Copyright (c) 2015年 rectinajh. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction


/***********************************************************************/


//通分:将2个分数分母变成一样,取最小公倍数
- (Fraction *)commonDenominator:(Fraction *)other
{
    Fraction *result = [[Fraction alloc] init];
    //求最小公倍数的分母
    for (NSInteger i = self.denominator ;i <= self.denominator*other.denominator; i ++)
    {
        if (i % self.denominator == 0 && i % other.denominator == 0)
        {
            result.denominator = i; //通分结果的分母设置为这个最小公倍数i
            break;
        }
    }
    result.molecular = self.molecular * result.denominator/self.denominator;
    return result;
}
//约分
- (void)simpleFraction
{
    //求分子分母的最大公约数,然后同时除
    for (NSInteger i = 1 ; i < self.molecular; i ++) {
        if (self.molecular % i == 0 && self.denominator % i == 0) {
            //分子分母同时除最大公约数
            self.molecular /= i;
            self.denominator /= i;
            break;
        }
    }

}
//加法方法
- (Fraction *) plus:(Fraction *)other
{
    Fraction *result = [[Fraction alloc] init];
    //将self与other通分,将2个分母变成一样,取最小公倍数
    Fraction * selfResult = [self commonDenominator:other];
    Fraction * otherResult = [other commonDenominator:self];

    result.denominator = selfResult.denominator;//结果分母,等于通分后的分数的分母
    result.molecular = selfResult.molecular + otherResult.molecular;//结果的分子,等于两个分子想家

    [result simpleFraction];//约分调用
    return result;
}
//减法方法
- (Fraction *)substration:(Fraction *)other
{
    Fraction *result = [[Fraction alloc] init];
    //将self与other通分,将2个分母变成一样,取最小公倍数
    Fraction * selfResult = [self commonDenominator:other];
    Fraction * otherResult = [other commonDenominator:self];

    result.denominator = selfResult.denominator;//结果分母,等于通分后的分数的分母
    result.molecular = selfResult.molecular - otherResult.molecular;//结果的分子,等于两个分子想家

    [result simpleFraction];//约分调用
    return result;

}
//乘法方法
- (Fraction *)multiplocation:(Fraction *)other
{
    Fraction *result = [[Fraction alloc] init];
    //将self与other通分,将2个分母变成一样,取最小公倍数
    Fraction * selfResult = [self commonDenominator:other];
    Fraction * otherResult = [other commonDenominator:self];

    result.denominator = selfResult.denominator * otherResult.denominator;
    result.molecular = selfResult.molecular * otherResult.molecular;

    [result simpleFraction];
    return result;

}

//除法方法
- (Fraction *)dividing:(Fraction *)other
{
    Fraction *result = [[Fraction alloc] init];
    //将self与other通分,将2个分母变成一样,取最小公倍数
    Fraction * selfResult = [self commonDenominator:other];
    Fraction * otherResult = [other commonDenominator:self];

    result.denominator = selfResult.denominator * otherResult.molecular;
    result.molecular = selfResult.molecular * otherResult.denominator;

    [result simpleFraction];
    return result;

}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%ld/%ld",self.molecular,self.denominator]; //被动调用,无需在主函数

}

//封装 将4个方法封装到一起,保留相同部分
- (Fraction *)calculationWithFraction:(Fraction *)other
                            Operation:(Operation)operation
{
    Fraction *result = [[Fraction alloc] init];
    //将self与other通分,将2个分母变成一样,取最小公倍数
    Fraction * selfResult = [self commonDenominator:other];
    Fraction * otherResult = [other commonDenominator:self];

    switch (operation ) {
        case Plus:
            //完成加法运算
        result.denominator = selfResult.denominator;//结果分母,等于通分后的分数的分母
        result.molecular = selfResult.molecular + otherResult.molecular;//结果的分子,等于两个分子想家

            break;

        case Substration:

            result.denominator = selfResult.denominator;//结果分母,等于通分后的分数的分母
            result.molecular = selfResult.molecular - otherResult.molecular;//结果的分子,等于两个分子想家

            break;

        case Multiplocation:
            result.denominator = selfResult.denominator * otherResult.denominator;
            result.molecular = selfResult.molecular * otherResult.molecular;

            break;

        case Dividing:
            result.denominator = selfResult.denominator * otherResult.molecular;
            result.molecular = selfResult.molecular * otherResult.denominator;

            break;

        default:
            break;
    }

        [result simpleFraction];
        return result;

}
//完成减法 乘法
/***************************************************************************/

@end
/***************************************************************************/
    //        2)分数之间的约分问题;
    //        3)属性及内存管理问题;
    //        4)方法的调用及代码规范;
    //创建并实例化2个分数
    //        2、建立一个分数类,实现分数之间的加、减、乘、除操作(分母不能为0)
    //        注:1)分数之间的通分问题;

    Fraction *f1 = [[Fraction alloc] init];
    f1.denominator = 18;
    f1.molecular = 1;

    Fraction *f2 = [[Fraction alloc] init];
    f2.denominator = 6;
    f2.molecular = 1;

//    Fraction *f3 = [f1 plus:f2];
//    Fraction *f4 = [f1 substration:f2];
//    Fraction *f5 = [f1 multiplocation:f2];
//    Fraction *f6 = [f1 dividing:f2];

    Fraction *f7 = [f1 calculationWithFraction:f2 Operation:Plus];
    Fraction *f8 = [f1 calculationWithFraction:f2 Operation:Substration];
    Fraction *f9 = [f1 calculationWithFraction:f2 Operation:Multiplocation];
    Fraction *f10 = [f1 calculationWithFraction:f2 Operation:Dividing];

    NSLog(@"%@ + %@ =%@",f1,f2,f7);
    NSLog(@"%@ + %@ =%@",f1,f2,f8);
    NSLog(@"%@ + %@ =%@",f1,f2,f9);
    NSLog(@"%@ + %@ =%@",f1,f2,f10);

    //NSLog(@"该分数相加为:%ld/%ld,该分数相减为:%ld/%ld,该分数相乘为:%ld/%ld,该分数相除为:%ld/%ld",f3.molecular,f3.denominator,f4.molecular,f4.denominator,f5.molecular,f5.denominator,f6.molecular,f6.denominator);


/****************************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值