5.20 一个混合算法与面向对象思想的例子
来源:互联网 发布:0--100水仙花数java 编辑:程序博客网 时间:2024/06/07 00:10
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
- 5.20 一个混合算法与面向对象思想的例子
- 一个完整的面向对象分析与设计例子
- 一个完整的面向对象分析与设计例子
- 一个例子体现JAVA中的面向对象思想,接口(抽象类)的用处
- 老子的思想与面向对象
- Javascript面向对象相关的一个例子
- JavaScript:一个面向对象的例子
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想
- 面向对象的思想~~
- 面向对象的思想
- 面向对象的思想
- MyBatis Annotation配置
- 二、我们应当怎样做需求调研:初识
- C# List多字段排序
- 工业相机编程模型和流程
- 三、我们应当怎样做需求调研:拜访
- 5.20 一个混合算法与面向对象思想的例子
- vim
- CodeForces 66D--思维(yy)
- c++中的inline用法
- (转)10+年程序员总结的20+条经验教训
- 四、我们应当怎样做需求调研:研讨会
- 保存
- [leetcode][tree] Symmetric Tree
- Bootstrap 模态对话框居中