标签:
#import <Foundation/Foundation.h>
/**
* 分数类
*/
@interface HHFraction : NSObject {
NSInteger _num; //分子
NSInteger _den; // 分母
}
/**
* 初始化方法
*
* @param num 分子
* @param den 分母
*
* @return 返回一个初始化对象
*/
+ (instancetype)initWithFraction:(NSInteger)num andDen:(NSInteger)den;
/**
* 用字符串初始化对象
*
* @param str 字符串
*
* @return 返回一个创建的分数
*/
+ (instancetype)initWithString:(NSString *)str;
/**
* 用小数初始化分数
*
* @param value 小数
*
* @return 返回分数
*/
+ (instancetype)initWithDoubleValue:(double)value;
/**
* 加法
*
* @param other 另一个分数
*
* @return 返回一个分数(求和后的结果)
*/
- (HHFraction *)add:(HHFraction *)other;
/**
* 减法
*
* @param other 另一个分数
*
* @return 返回一个分数(求差后的结果)
*/
- (HHFraction *)sub:(HHFraction *)other;
/**
* 乘法
*
* @param other 另一个分数
*
* @return 返回一个分数(求积后的结果)
*/
- (HHFraction *)mul:(HHFraction *)other;
/**
* 除法
*
* @param other 另一个分数
*
* @return 返回一个分数(求商后的结果)
*/
- (HHFraction *)div:(HHFraction *)other;
@end
#import "HHFraction.h"
NSInteger gcd(NSInteger a , NSInteger b){
if (a < b) {
gcd(b, a);
}
return b==0?a:gcd(b, a % b);
}
@implementation HHFraction
+ (instancetype)initWithFraction:(NSInteger)num andDen:(NSInteger)den{
return [[self alloc]initWithNum:num andDen:den];
}
- (instancetype)initWithNum:(NSInteger)num andDen:(NSInteger)den{
if (den == 0) {
@throw [NSException exceptionWithName:@"分数初始化异常" reason:@"分母不为零" userInfo:nil];
}
else{
if (self = [super init]) {
_num = num;
_den =den;
}
return [[self normal] reduce];
}
}
+ (instancetype)initWithString:(NSString *)str{
NSArray *array = [str componentsSeparatedByString:@"/"];
if (array.count != 2) {
@throw [NSException exceptionWithName:@"分数初始化异常" reason:@"输入字符串有误" userInfo:nil];
}
else{
return [[self alloc] initWithNum:[[array firstObject] integerValue] andDen:[[array lastObject] integerValue]];
}
}
+ (instancetype)initWithDoubleValue:(double)value{
return [[self alloc] initWithNum:value * 10000 andDen:10000];
}
- (HHFraction *)add:(HHFraction *)other{
return [[[[HHFraction alloc]initWithNum:_den * other->_num + _num * other->_den andDen:_den * other->_den] normal] reduce];
}
- (HHFraction *)sub:(HHFraction *)other{
return [[[[HHFraction alloc]initWithNum:_den * other->_num - _num * other->_den andDen:_den * other->_den] normal] reduce];
}
- (HHFraction *)mul:(HHFraction *)other{
return [[[[HHFraction alloc]initWithNum:_num * other->_num andDen:_den * other->_den] normal] reduce];
}
- (HHFraction *)div:(HHFraction *)other{
return [[[[HHFraction alloc]initWithNum:_num * other->_den andDen:_den * other->_num] normal] reduce];
}
//分数正常化
- (HHFraction *)normal{
if (_den < 0) {
_num = -_num;
_den = -_den;
}
return self;
}
//约分
- (HHFraction *)reduce{
if (_num == 0) {
_den = 1;
}else{
NSInteger x = _num > 0?_num : -_num;
NSInteger y = _den > 0?_den : -_den;
NSInteger max = gcd(x, y);
_num /= max;
_den /= max;
}
return self;
}
- (NSString *)description{
if (_den == 1) {
return [NSString stringWithFormat:@"%ld",_num];
}
return [NSString stringWithFormat:@"%@%ld/%ld%@",_num < 0?@"(":@"",_num,_den,_num < 0?@")":@""];
}
@end
#import <Foundation/Foundation.h>
#import "HHFraction.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
HHFraction *f1 = [HHFraction initWithFraction:2 andDen:-9];
NSLog(@"%@",f1);
HHFraction *f2 = [HHFraction initWithFraction:2 andDen:-4];
NSLog(@"%@ + %@ = %@",f1,f2,[f1 add:f2]);
NSLog(@"%@ - %@ = %@",f1,f2,[f1 sub:f2]);
NSLog(@"%@ ?? %@ = %@",f1,f2,[f1 mul:f2]);
NSLog(@"%@ ? %@ = %@",f1,f2,[f1 div:f2]);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/yuanwang942217/p/4975578.html