//OC 便利构造器 自定义初始化 分数加减乘除
main.m
#import <Foundation/Foundation.h>
#import "Fraction.h"
int main(int argc, const char * argv[]) {
Fraction *value = [Fraction fractionWithFenzi:7 withFenmu:5];
Fraction *value1 = [Fraction fractionWithFenzi:4 withFenmu:5];
[value reduce];
[value print];
[[value addFraction:value1] print];
[[value minusFraction:value1] print];
[[value mulFraction:value1] print];
[[value divFraction:value1] print];
return 0;
}
Fraction.h
@interface Fraction : NSObject {
NSInteger _fenzi;//分子
NSInteger _fenmu;//分母
}
- (instancetype)initWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu;
//便利构造器
+ (instancetype)fractionWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu;
//约分
- (void)reduce;
- (void)reduce1;
- (void)print;
//get
- (NSInteger )getFenzi;
- (NSInteger )getFenmu;
//加
- (Fraction *)addFraction:(Fraction *)anotherFraction;
//减
- (Fraction *)minusFraction:(Fraction *)anotherFraction;
//乘
- (Fraction *)mulFraction:(Fraction *)anotherFraction;
//除
- (Fraction *)divFraction:(Fraction *)anotherFraction;
Fraction.m
@implementation Fraction
- (instancetype)initWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu {
self = [super init];
if (self) {
_fenzi = fenzi;
_fenmu = fenmu;
}
return self;
}
//便利构造器
+ (instancetype)fractionWithFenzi:(NSInteger )fenzi withFenmu:(NSInteger )fenmu {
return [[Fraction alloc] initWithFenzi:fenzi withFenmu:fenmu];
}
//约分
- (void)reduce {
NSInteger min = _fenzi < _fenmu ? _fenzi : _fenmu;
for (NSInteger i = min; i >= 0; i--) {
if (_fenzi % i == 0 && _fenmu % i == 0) {
_fenzi /= i;
_fenmu /= i;
break;
}
}
}
//约分2
- (void)reduce1 {
NSInteger a = _fenzi;
NSInteger b = _fenmu;
NSInteger c = 0;
while (a % b) {
c = a % b;
a = b;
b = c;
}
_fenzi /= b;
_fenmu /= b;
}
//打印
- (void)print {
NSLog(@"%ld/%ld", _fenzi, _fenmu);
}
- (NSInteger)getFenzi {
return _fenzi;
}
- (NSInteger)getFenmu {
return _fenmu;
}
//加
- (Fraction *)addFraction:(Fraction *)anotherFraction {
//定义分数对象存结果作为返回值
Fraction *value = [[Fraction alloc] init];
value->_fenmu = [anotherFraction getFenmu] * _fenmu;
value->_fenzi = [anotherFraction getFenzi] * _fenmu + [anotherFraction getFenmu] * _fenzi;
[value reduce];
return value;
}
#pragma mark -
#pragma mark 减法
//减
- (Fraction *)minusFraction:(Fraction *)anotherFraction {
Fraction *value = [[Fraction alloc] init];
value->_fenmu = [anotherFraction getFenmu] * _fenmu;
value->_fenzi = [anotherFraction getFenmu] * _fenzi - [anotherFraction getFenzi] * _fenmu;
[value reduce1];
return value;
}
#pragma mark -
#pragma mark 乘法
//乘
- (Fraction *)mulFraction:(Fraction *)anotherFraction {
Fraction *value = [[Fraction alloc] init];
value->_fenmu = [anotherFraction getFenmu] * _fenmu;
value->_fenzi = [anotherFraction getFenzi] * _fenzi;
[value reduce1];
return value;
}
//除
- (Fraction *)divFraction:(Fraction *)anotherFraction {
Fraction *value = [[Fraction alloc] init];
value->_fenzi = [anotherFraction getFenmu] * _fenzi;
value->_fenmu = [anotherFraction getFenzi] * _fenmu;
[value reduce1];
return value;
}
@end