Objective-C练习_分数类实现
2015-08-02 15:45
477 查看
要求
定义一个分数类(Fraction)实例变量:分子,分母
⽅法:初始化方法(2个参数),便利构造器,约分,打印,加,减,乘,除。
提示:加法运算 - (Fraction )addFraction:(Fraction )anotherFraction
本博文只是用最基本的方式完成了题目的相关功能,仅供初学者参考。请爱挑剔的技术大牛对于本代码的拙见,见谅,谢谢!
代码实现
分数类有两个实例变量:分子和分母。分数类的对象要实现分数的四则运算,方法的参数必须是另一个对象。这样就可以按照数学中分数运算的规律进行加、减、乘、除运算。其中,注意除法和约分有隐含的数学陷阱,如约分时分子或分母为0,0不能做除数等。按照要求,创建Fraction类,定义实例变量、方法
Fraction.h 接口文件代码如下:
#import <Foundation/Foundation.h> @interface Fraction : NSObject { @protected NSInteger _numerator; /* 分子 */ NSInteger _denominator; /* 分母 */ } /* 实例变量setter, getter方法 */ - (void)setNumerator:(NSInteger)numerator; - (NSInteger)numerator; - (void)setDenominator:(NSInteger)denominator; - (NSInteger)denominator; /* 自定义初始化方法 */ - (id)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator; /* 便利构造器 */ + (id)fractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator; /* 约分 */ - (id)reductionOfAFraction; /* 打印 */ - (void)printFraction; /* 加 */ - (Fraction *)addFraction:(Fraction *)anotherFraction; /* 减 */ - (Fraction *)subFraction:(Fraction *)anotherFraction; /* 乘 */ - (Fraction *)mulFraction:(Fraction *)anotherFraction; /* 除 */ - (Fraction *)divFraction:(Fraction *)anotherFraction; @end
Fraction.m 实现文件代码如下:
#import "Fraction.h" @implementation Fraction - (void)setNumerator:(NSInteger)numerator { _numerator = numerator; } - (NSInteger)numerator { return _numerator; } - (void)setDenominator:(NSInteger)denominator { _denominator = denominator; } - (NSInteger)denominator { return _denominator; } - (id)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator { self = [super init]; if (self) { _numerator = numerator; _denominator = denominator; } return self; } + (id)fractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator { Fraction *fra = [[Fraction alloc] initWithNumerator:numerator denominator:denominator]; return fra; } - (id)reductionOfAFraction { NSInteger m = [self numerator]; NSInteger n = [self denominator]; /* 如果分子或分母为负数, 则忽略符号 */ if (m < 0) { m = -m; } if (n < 0) { n = -n; } /* 欧几里得算法求最大公约数 */ NSInteger r = m % n; while (r > 0) { m = n; n = r; r = m % n; } [self setNumerator:([self numerator] / n)]; [self setDenominator:([self denominator] / n)]; return self; } - (void)printFraction { NSLog(@"%ld / %ld", [self numerator], [self denominator]); } - (Fraction *)addFraction:(Fraction *)anotherFraction { NSInteger num = [self numerator] * [anotherFraction denominator] + [self denominator] * [anotherFraction numerator]; NSInteger den = [self denominator] * [anotherFraction denominator]; Fraction *fra = [Fraction fractionWithNumerator:num denominator:den]; return fra; } - (Fraction *)subFraction:(Fraction *)anotherFraction { NSInteger num = [self numerator] * [anotherFraction denominator] - [self denominator] * [anotherFraction numerator]; NSInteger den = [self denominator] * [anotherFraction denominator]; Fraction *fra = [Fraction fractionWithNumerator:num denominator:den]; return fra; } - (Fraction *)mulFraction:(Fraction *)anotherFraction { NSInteger num = [self numerator] * [anotherFraction numerator]; NSInteger den = [self denominator] * [anotherFraction denominator]; Fraction *fra = [Fraction fractionWithNumerator:num denominator:den]; return fra; } - (Fraction *)divFraction:(Fraction *)anotherFraction { NSInteger num = [self numerator] * [anotherFraction denominator]; NSInteger den = [self denominator] * [anotherFraction numerator]; Fraction *fra = [Fraction fractionWithNumerator:num denominator:den]; return fra; } @end
main.m 文件代码如下:
#import <Foundation/Foundation.h> #import "Fraction.h" int main(int argc, const char * argv[]) { @autoreleasepool { Fraction *fraA = [Fraction fractionWithNumerator:2 denominator:4]; [fraA reductionOfAFraction]; [fraA printFraction]; Fraction *fraB = [Fraction fractionWithNumerator:2 denominator:3]; Fraction *fraction = [[Fraction alloc] init]; /* 分数加法 */ fraction = [fraA addFraction:fraB]; [fraction printFraction]; /* 分数减法 */ fraction = [fraA subFraction:fraB]; [fraction printFraction]; /* 分数乘法*/ fraction = [fraA mulFraction:fraB]; [fraction printFraction]; /* 分数除法 */ fraction = [fraA divFraction:fraB]; [fraction printFraction]; } return 0; }
相关文章推荐
- Objective-C学习笔记_继承、初始化方法、便利构造器
- Objective-C学习笔记_实例变量可⻅度及方法
- JXPath(1.3) - Object Graph Traversal
- Objective-C学习笔记_类和对象
- objective-c中字符串长度计算
- objective-c中字符串长度计算
- 【iOS开发之Objective-C】数据持久化--文件和归档
- objective-c 运算符优先级排序
- Objective-C中的instancetype和id区别
- 用objective c开发的第一个mac程序(2)——初识oc cocoa swift
- [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第十二章 类别
- Spark源码阅读笔记之BlockObjectWriter
- 用objective-c开发的第一个mac程序(1)——依赖库的生成
- objective C程序 NSString用法和类的概念
- Objective-C语法之代码块(block)的使用
- net.sf.json.JSONObject需要jar包
- gtk+2.0中GtkObject结构中没有klass成员的解决办法
- gtk+2.0中GtkObject结构中没有klass成员的解决办法
- gtk+2.0中GtkObject结构中没有klass成员的解决办法
- 动态库的动态链接(error while loading shared libraries,cannot open shared object file)