码迷,mamicode.com
首页 > 其他好文 > 详细

创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。要求:为类添加属性

时间:2014-08-12 03:32:14      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:property   属性   

//在.h文件里
{
    NSInteger _numerator;     //分子
    NSInteger _denominator;   //分母
   
}
 
//属性
@property (nonatomic) NSInteger numerator;
@property (nonatomic) NSInteger denominator;
//初始化
 
- (id)initwithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator;
 
 
//加法
 
- (void)sumNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2;
 
 
//减法
- (void)subNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2;
 
 
//乘法
- (void) mulNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2;
 
 
//除法
- (void) divNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2;
 
 
//比较大小
- (void)compareNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2;
 
 
//约分
- (void)reduceNumerator:(NSInteger)numerator denominator:(NSInteger)denominator;
 
 
    return [NSStringstringWithFormat:@"%ld/%ld",_numerator,_denominator];
}
 
 
//初始化
 
- (id)initwithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    self = [superinit];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    returnself;
}
 
 
//约分
- (void)reduceNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    NSInteger num = 0;
    for (NSInteger i = numerator; i > 0; i --) {
        for (NSInteger j = denominator; j > 0; j --) {
            if (numerator % i == 0 && denominator %j == 0&& i == j) {
                num =i;
            }
            if (num ==i) {
                break;
            }
        }
        if (num==i) {
            break;
        }
    }
    _numerator = numerator / num;
    _denominator = denominator / num;
}
 
 
 
//加法
 
- (void)sumNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2
{
   NSInteger _numerator1 = numerator1 * denominator2 + numerator2 *denominator1;
   NSInteger _denominator1 = denominator1 * denominator2;
    Fraction *sum = [[Fractionalloc] init];
[sum reduceNumerator:_numerator1 denominator:_denominator1];
    NSLog(@"%ld/%ld +%ld/%ld = %@",numerator1,denominator1,numerator2,denominator2,sum);
}
 
 
 
//减法
- (void)subNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2
{
    NSInteger _numerator1 = numerator1 * denominator2 - numerator2 *denominator1;
    NSInteger _denominator1 = denominator1 * denominator2;
    Fraction *sub = [[Fractionalloc] init];
    [sub reduceNumerator:_numerator1 denominator:_denominator1];
    NSLog(@"%ld/%ld +%ld/%ld = %@",numerator1,denominator1,numerator2,denominator2,sub);
    
}
 
 
//乘法
- (void) mulNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2
{
    NSInteger _numerator1 = numerator1 * numerator2 ;
    NSInteger _denominator1 = denominator1 * denominator2;
    Fraction *mul = [[Fractionalloc] init];
    [mul reduceNumerator:_numerator1 denominator:_denominator1];
    NSLog(@"%ld/%ld +%ld/%ld = %@",numerator1,denominator1,numerator2,denominator2,mul);
}
 
 
//除法
- (void) divNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2
{
    NSInteger _numerator1 = numerator1 * denominator2 ;
    NSInteger _denominator1 = denominator1 * numerator2;
    Fraction *div = [[Fractionalloc] init];
    [div reduceNumerator:_numerator1 denominator:_denominator1];
    NSLog(@"%ld/%ld +%ld/%ld = %@",numerator1,denominator1,numerator2,denominator2,div);
}
 
 
//比较大小
- (void)compareNumerator1:(NSInteger)numerator1denominator1:(NSInteger)denominator1 numerator2:(NSInteger)numerator2denominator2:(NSInteger)denominator2
{
    _numerator = numerator1 * denominator2;
    _denominator = numerator2 * denominator1;
    NSInteger num  = _numerator - _denominator;
    if (num > 0) {
        NSLog(@"%ld/%ld 比 %ld/%ld 大",numerator1,denominator1,numerator2,denominator2);
    } else {
        NSLog(@"%ld/%ld 比 %ld/%ld 小",numerator1,denominator1,numerator2,denominator2);
    }
}
 
//在main函数里调用方法
Fraction *fraction = [[Fractionalloc] initwithNumerator:5denominator:6];
        
        //加法
        [fraction sumNumerator1:4denominator1:5numerator2:3denominator2:5];
        
        
        //减法
        [fraction subNumerator1:2denominator1:5numerator2:1denominator2:3];
        
        
        //乘法
        [fraction mulNumerator1:3denominator1:8numerator2:5denominator2:6];
        
        
        //除法
        [fraction divNumerator1:11denominator1:15numerator2:11denominator2:20];
        [fraction reduceNumerator:fraction.numeratordenominator:fraction.denominator];
        
        
        //比较大小
        [fraction compareNumerator1:3denominator1:8numerator2:4denominator2:9];
        
        
        //约分
        [fraction reduceNumerator:6denominator:10];
        NSLog(@"约分后结果为%@",fraction);

创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。要求:为类添加属性,布布扣,bubuko.com

创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。要求:为类添加属性

标签:property   属性   

原文地址:http://9178463.blog.51cto.com/9168463/1538772

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!