Fraction.h:
#import
@interface Fraction : NSObject
//声明两个实例变量
@property NSInteger upNumber, downNumber;
//自定义初始化方法
- (id)initWithUpNumber:(NSInteger)upNumber downNumber:(NSInteger)downNumber;
//便利构造器
+ (id)fractionWithUpNumber:(NSInteger)upNumber downNumber:(NSInteger)downNumber;
//求最大公约数
- (NSInteger)GCDNumber:(NSInteger)num1 number2:(NSInteger)num2;
//约分
- (void)reduce;
//加法
- (Fraction *)addFraction:(Fraction *)anotherFraction;
//减法
- (Fraction *)delFraction:(Fraction *)anotherFraction;
//乘法
- (Fraction *)mulFraction:(Fraction *)anotherFraction;
//除法
- (Fraction *)divFraction:(Fraction *)anotherFraction;
//比较大小
- (void)comFraction:(Fraction *)num1 ;
--------------------------------------------------------------------------------------------
Fraction.m:#import "Fraction.h"
@implementation Fraction
- (id)initWithUpNumber:(NSInteger)upNumber downNumber:(NSInteger)downNumber {
if (self = [super init]) {
_upNumber = upNumber;
_downNumber = downNumber;
}
return self;
}
+ (id)fractionWithUpNumber:(NSInteger)upNumber downNumber:(NSInteger)downNumber {
return [[Fraction alloc] initWithUpNumber:upNumber downNumber:downNumber];
}
//重写方法
- (NSString *)description
{
return [NSString stringWithFormat:@"%ld/%ld", _upNumber, _downNumber];
}
#pragma mark - 运算方法
//求最大公约数
- (NSInteger)GCDNumber:(NSInteger)num1 number2:(NSInteger)num2 {
//利用辗转相除法, 求出最大公约数
while (num2 != 0) {
NSInteger temp = num1 % num2;
num1 = num2;
num2 = temp;
}
return num1;
}
//约分
- (void)reduce {
NSInteger num = [self GCDNumber:_upNumber number2:_downNumber];
//分子, 分母都以最大公约数, 就是约分后的分子, 分母
_upNumber /= num;
_downNumber /= num;
}
//加法
- (Fraction *)addFraction:(Fraction *)anotherFraction {
Fraction *fraction = [[Fraction alloc] init];
//两分数相加后的分子
fraction.upNumber = _upNumber * anotherFraction.downNumber + anotherFraction.upNumber * _downNumber;
//相加后的分母
fraction.downNumber = _downNumber * anotherFraction.downNumber;
[fraction reduce];
return fraction;
}
//减法
- (Fraction *)delFraction:(Fraction *)anotherFraction {
Fraction *fraction = [[Fraction alloc] init];
//两分数相减后的分子
fraction.upNumber = _upNumber * anotherFraction.downNumber - anotherFraction.upNumber * _downNumber;
//两分数相减后的分母
fraction.downNumber = _downNumber * anotherFraction.downNumber;
[fraction reduce];
return fraction;
}
//乘法
- (Fraction *)mulFraction:(Fraction *)anotherFraction {
Fraction *fraction = [[Fraction alloc] init];
//两分数相乘后的分子
fraction.upNumber = _upNumber * anotherFraction.upNumber;
//两分数相乘后的分母
fraction.downNumber = _downNumber * anotherFraction.downNumber;
[fraction reduce];
return fraction;
}
//除法
- (Fraction *)divFraction:(Fraction *)anotherFraction {
Fraction *fraction = [[Fraction alloc] init];
//两分数相除后的分子
fraction.upNumber = _upNumber * anotherFraction.downNumber;
//两分数相除后的分母
fraction.downNumber = _downNumber * anotherFraction.upNumber;
[fraction reduce];
return fraction;
}
//比较大小
- (void)comFraction:(Fraction *)num1 {
Fraction *result = [self delFraction:num1];
//比较两个分数的大小
if (result == 0) {
NSLog(@"两数相等!");
}
if (result > 0) {
NSLog(@"%@ > %@", self, num1);
}else if (result < 0) {
NSLog(@"%@ < %@", self, num1);
}
}
@end
#import
#import "Fraction.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 创建⼀一个Fraction类(分数)实现分数的加减乘除,⽐比较⼤大⼩小、约分等⽅方法。
// 要求:为类添加属性
Fraction *num1 = [[Fraction alloc] initWithUpNumber:4 downNumber:12];
Fraction *num2 = [[Fraction alloc] initWithUpNumber:3 downNumber:12];
//两分数之和
Fraction *sum = [num1 addFraction:num2];
NSLog(@"%@", sum);
//两数之差
Fraction *del = [num1 delFraction:num2];
NSLog(@"%@", del);
//两数之积
Fraction *mul = [num1 mulFraction:num2];
NSLog(@"%@", mul);
//两数之商
Fraction *div = [num1 divFraction:num2];
NSLog(@"%@", div);
//两数比较大小
[num1 comFraction:num2];
}
return 0;
}