<span style="font-family:KaiTi_GB2312;font-size:32px;">#import <Foundation/Foundation.h>
@interface Fraction : NSObject
@property (nonatomic) NSInteger molecular; //分子
@property (nonatomic) NSInteger denominator; //分母
//初始化
- (id)initWithMolecular:(NSInteger)molecular denominator:(NSInteger)denominator;
//分数相加
- (Fraction *)sumFraction:(Fraction *)fraction;
//分数相减
- (Fraction *)differencingFraction:(Fraction *)fraction;
//分数相乘
- (Fraction *)multiplicaFraction:(Fraction *)fraction;
//分数相除
- (Fraction *)divisionFraction:(Fraction *)fraction;
//约分
- (Fraction *)reduceOfFraction;
//比较大小
- (void)compareFraction:(Fraction *)fraction;
@end
</span>
<span style="font-family:KaiTi_GB2312;font-size:32px;">@implementation Fraction
- (id)initWithMolecular:(NSInteger)molecular denominator:(NSInteger)denominator
{
self = [super init];
if (self) {
_molecular = molecular;
_denominator = denominator;
}
return self;
}
//分数相加
- (Fraction *)sumFraction:(Fraction *)fraction
{
Fraction *temp = [Fraction alloc];
temp.molecular = self.molecular * fraction.denominator + self.denominator * fraction.molecular;
temp.denominator = self.denominator * fraction.denominator;
return temp;
}
//分数相减
- (Fraction *)differencingFraction:(Fraction *)fraction
{
Fraction *temp = [Fraction alloc];
temp.molecular = _molecular * fraction.denominator - _denominator * fraction.molecular;
temp.denominator = _denominator * fraction.denominator;
return temp;
}
//分数相乘
- (Fraction *)multiplicaFraction:(Fraction *)fraction
{
Fraction *temp = [Fraction alloc];
temp.molecular = _molecular * fraction.molecular;
temp.denominator = _denominator * fraction.denominator;
return temp;
}
//分数相除
- (Fraction *)divisionFraction:(Fraction *)fraction
{
Fraction *temp = [Fraction alloc];
temp.molecular = self.molecular * fraction.denominator;
temp.denominator = self.denominator * fraction.molecular;
return temp;
}
//比较大小
- (void)compareFraction:(Fraction *)fraction
{
Fraction *temp = [Fraction alloc];
temp = [self differencingFraction:fraction];
if (temp.molecular > 0) {
NSLog(@"%ld/%ld较大",self.molecular,self.denominator);
}else if(temp.molecular == 0){
NSLog(@"两个数一样大");
}else{
NSLog(@"%ld/%ld较大",fraction.molecular,fraction.denominator);
}
}
//约分
- (Fraction *)reduceOfFraction
{
if (self.molecular < self.denominator) {
for (NSInteger i = self.molecular; i > 0; i--) {
if (self.molecular % i == 0 && self.denominator % i == 0) {
self.molecular = self.molecular / i;
self.denominator = self.denominator / i;
}
}
}else if(self.molecular > self.denominator){
for (NSInteger i = self.denominator; i > 0; i--) {
if (self.molecular % i == 0 && self.denominator % i == 0) {
self.molecular = self.molecular / i;
self.denominator = self.denominator / i;
}
}
}
return self;
}
@end</span>
<span style="font-family:KaiTi_GB2312;font-size:32px;"> @autoreleasepool {
Fraction *fra1 = [[Fraction alloc]initWithMolecular:3 denominator:4];
Fraction *fra2 = [[Fraction alloc]initWithMolecular:2 denominator:5];
//分数相加
Fraction *number1 =[fra1 sumFraction:fra2];
[number1 reduceOfFraction];
NSLog(@"%ld/%ld ",(long)number1.molecular,number1.denominator);
//分数相减
Fraction *number2 =[fra1 differencingFraction:fra2];
[number2 reduceOfFraction];
NSLog(@"%ld/%ld",number2.molecular,number2.denominator);
//分数相乘
Fraction *number3 = [fra1 multiplicaFraction:fra2];
[number3 reduceOfFraction];
NSLog(@"%ld/%ld",number3.molecular,number3.denominator);
//分数相除
Fraction *number4 = [fra1 divisionFraction:fra2];
[number4 reduceOfFraction];
NSLog(@"%ld/%ld",number4.molecular,number4.denominator);
//比较大小
[fra1 compareFraction:fra2];</span>