//Fraction.h
@interface Fraction : NSObject
//属性
@property(nonatomic, assign) int numerator;//分子
@property(nonatomic, assign) int denominator;//分母
//初始化方法
- (id)initWithNumerator:(int)numerator
andDenominator:(int)denominator;
//分数相加
- (Fraction *)add:(Fraction *)fraction;
//分数相减
- (Fraction *)sub:(Fraction *)fraction;
//分数相乘
- (Fraction *)mul:(Fraction *)fraction;
//分数相除
- (Fraction *)div:(Fraction *)fraction;
//分数比较
- (Fraction *)compareValues:(Fraction *)fraction;
//分数约分
- (void)reduce;
//计算结果
- (void)printResult;
@end
//Fraction.m
#import "Fraction.h"
@implementation Fraction
//初始化方法
- (id)initWithNumerator:(int)numerator
andDenominator:(int)denominator;
{
self = [super init];
if (self != nil) {
self.numerator = numerator;
self.denominator = denominator;
}
return self;
}
//分数相加
- (Fraction *)add:(Fraction *)fraction
{
int newNu = self.numerator * fraction.denominator + self.denominator * fraction.numerator;
int newDe = self.denominator * fraction.denominator;
Fraction *f = [[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];
return f;
}
//分数相减
- (Fraction *)sub:(Fraction *)fraction
{
int newNu = self.numerator * fraction.denominator - self.denominator * fraction.numerator;
int newDe = self.denominator * fraction.denominator;
Fraction *f = [[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];
return f;
}
//分数相乘
- (Fraction *)mul:(Fraction *)fraction
{
int newNu = self.numerator * fraction.numerator;
int newDe = self.denominator * fraction.denominator;
Fraction *f = [[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];
return f;
}
//分数相除
- (Fraction *)div:(Fraction *)fraction
{
int newNu = self.numerator * fraction.denominator;
int newDe = self.denominator * fraction.numerator;
Fraction *f =[[Fraction alloc] initWithNumerator:newNu andDenominator:newDe];
[f reduce];
return f;
}
//分数比较
- (Fraction *)compareValues:(Fraction *)fraction
{
int newNu1 = self.numerator * fraction.denominator;//分子1
int newNu2 = self.denominator * fraction.numerator;//分子2
//分母相同,比较分子的大小
if (newNu1 > newNu2) {
return self;
} else {
return fraction;
}
}
//分数约分
- (void)reduce
{
int gcd = [self gcdWithNum1:self.numerator andNum2:self.denominator];
self.numerator = self.numerator / gcd;
self.denominator = self.denominator / gcd;
}
//辗转相除法 求两个分数的最大公约数
- (int)gcdWithNum1:(int)num1
andNum2:(int)num2
{
while (num2 != 0) {
int temp = num1 % num2;
num1 = num2;
num2 = temp;
}
return num1;
}
//输出结果
- (void)printResult
{
NSLog(@"%d/%d", self.numerator, self.denominator);
}
@end
#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Book.h"
#import "BookManager.h"
int main(int argc, const char * argv[]) {
//创建对象
Fraction *f1 = [[Fraction alloc] initWithNumerator:1 andDenominator:3];
Fraction *f2 = [[Fraction alloc] initWithNumerator:3 andDenominator:-5];
//相加
Fraction *add = [f1 add:f2];
[add printResult];
//相减
Fraction *sub = [f1 sub:f2];
[sub printResult];
//相乘
Fraction *mul = [f1 mul:f2];
[mul printResult];
//相除
Fraction *div = [f1 div:f2];
[div printResult];
//比较
Fraction *com = [f1 compareValues:f2];
[com printResult];
return 0;
}