#import
#import "Member.h"
#import "Denominator.h"
#import "Score.h"
#import "Fraction.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Member *member = [[Member alloc]
initWithSize:20];
Denominator *denominator = [[Denominator alloc]
initWithSize:30];
//赋值
[member setSize:10];
[denominator setSize:20];
Score *score = [[Score alloc]
initWithMember:member denominator:denominator];
//打印
[score printf];
Member *member1 = [[Member alloc] init];
Denominator *denominator1 = [[Denominator alloc] init];
[member1 setSize:20];
[denominator1 setSize:30];
Score *score1 = [[Score alloc]
initWithMember:member1 denominator:denominator1];
[score1 printf];
//求公约数
[score commonDivisor];
//加法
[[score add:score1] commonDivisor];
//减法
[[score subtract:score1] commonDivisor];
//乘法
[[score multiply:score1] commonDivisor];
//除法
[[score division:score1] commonDivisor];
//------------------------------------------------------------------------
Fraction *fraction = [[Fraction alloc]
initWithMember:2 den:3];
[fraction setMember:20];
[fraction setDen:30];
[fraction printf];
Fraction *fraction1 = [[Fraction alloc]
initWithMember:30 den:40];
[fraction1 printf];
//加法
[[fraction add:fraction1] commonDivisor];
//减法
[[fraction subtraction:fraction1] commonDivisor];
//乘法
[[fraction mul:fraction1] commonDivisor];
//除法
[[fraction division:fraction1] commonDivisor];
}
return 0;
}
//声明文件
#import
@interface Fraction : NSObject
{
int _member;
int _den;
}
-(id)initWithMember:(int)member den:(int)den;
-(void)printf;
-(void)setMember:(int)member;
//
-(void)setDen:(int)den;
//
//-(int)den;
//
//-(int)member;
//----------------------------------------
// 加法
-(Fraction *)add:(Fraction *)add;
// 减法
-(Fraction *)subtraction:(Fraction *)add;
// 乘法
-(Fraction *)mul:(Fraction *)mul;
// 除法
-(Fraction *)division:(Fraction *)division;
// 约分
-(void)commonDivisor;
@end
//实现文件
#import "Fraction.h"
@implementation Fraction
-(id)initWithMember:(int)member den:(int)den
{
_member = member;
_den = den;
return self;
}
-(void)printf
{
NSLog(@"%d / %d", _member, _den);
}
-(void)setMember:(int)member
{
_member = member;
}
-(void)setDen:(int)den
{
_den = den;
}
//
//-(int)den
//{
// return _den;
//}
//
//-(int)member
//{
// return _member;
//}
//-----------------------------------
// 加法
-(Fraction *)add:(Fraction *)add
{
Fraction *fractionNew = [[Fraction alloc] init];
fractionNew->_member = _member * add->_den + _den * add->_member;
fractionNew->_den = _den * add->_den;
return fractionNew;
}
// 减法
-(Fraction *)subtraction:(Fraction *)subtraction
{
Fraction *New = [[Fraction alloc] init];
New->_member = _member * subtraction->_den - _den * subtraction->_member;
New->_den = _den * subtraction->_den;
return New;
}
// 乘法
-(Fraction *)mul:(Fraction *)mul
{
Fraction *New = [[Fraction alloc] init];
New->_member = _member * mul->_member;
New->_den = _den * mul->_den;
return New;
}
// 除法
-(Fraction *)division:(Fraction *)division
{
Fraction *New = [[Fraction alloc] init];
New->_member = _member * division->_den;
New->_den = _den * division->_member;
return New;
}
// 约分
-(void)commonDivisor
{
int a = 0, b = 0, c = 0, d = 0, gbs = 0;
// scanf("%d%d", &a, &b);
a = _member;
b = _den;
if (a < b) {
c = a;
a = b;
b = c;
}
gbs = a * b;
while (b != 0) {
d = a % b;
a = b;
b = d;
}
NSLog(@"%d / %d", _member / a, _den /a);
}
@end