在目标c中添加分数

时间:2014-01-20 06:05:13

标签: objective-c operators

我是Objective-c languange的新手,用(+)或添加运算符练习基本数学(分数)。

这里我想制作2/3 + 3/3 = 5/3,我想将此分数显示为1 2/3。

备注:5/3 ==> 5是分子,3是分母。

但不知怎的,我的代码无法正常工作,我相信我的错误是在这个实例上的if语句。

- (void)printsot:(BOOL)reduceFrac;

但我仍然无法弄明白,为什么输出不会打印结果,它不会NSLog任何东西。

以下是我上面与上述问题相关的代码

- (void)printsot:(BOOL)reduceFrac{
if (denominator == 0){
    NSLog(@"Result is NAN");
}
else if (denominator == 1){
    NSLog(@"%i",numerator);

}
else if (numerator == 1)  {
    NSLog(@" %i / %i",numerator,denominator);
}
else if (reduceFrac){
    Fraction *resultreduce = [[Fraction alloc]init];
    [resultreduce setTo:numerator over:denominator];
    [resultreduce reduce];
    if (resultreduce.denominator ==1) {
        NSLog(@"result is %i",resultreduce.numerator);
    }

    else if(resultreduce.numerator % resultreduce.denominator == 0){

        int result;

        result = resultreduce.numerator / resultreduce.denominator;
        NSLog(@"%i",result);

    }

    else if (resultreduce.denominator > resultreduce.numerator){

        int temp;
        int modNumerator;


        temp = resultreduce.numerator % resultreduce.denominator;

        modNumerator    = resultreduce.numerator -  temp * resultreduce.denominator;

        NSLog(@"fraction is %i %i / %i",temp,modNumerator,resultreduce.denominator);

    }


}

else{
    NSLog(@"%i/%i",numerator,denominator);
}

}

这是我的reduce实例方法

- (void) reduce{
int u = numerator;
int v = denominator;
int temp = 0 ;

while (v != 0) {
    temp= u % v;
    u = v;
    v = temp;
}

numerator /=   u;
denominator /=  u;

}



- (Fraction *) add2:(Fraction *)f {

Fraction *result = [[Fraction alloc]init] ;


result.numerator      = numerator * f.denominator + denominator * f.numerator;
result.denominator    = denominator * f.denominator;

return result;

}


xcode的当前输出

Prog2 Start Looping[5210:303] 2/3
Prog2 Start Looping[5210:303] +
Prog2 Start Looping[5210:303] 3/3
Prog2 Start Looping[5210:303] =

我的主要文件:

int main (int argc, char * argv[])
{
   @autoreleasepool {

       //declare Class
       Fraction *myFraction = [[Fraction alloc]init];
       Fraction *anotherFraction = [[Fraction alloc]init];
       Fraction *resultFraction;

       //set the value of the class

       [myFraction setTo:2 over:3];    //set numerator to 2 and denominator to 3
       [anotherFraction setTo:3 over:3]; //set numerator to 3 and denominator to 3


       //display fraction's numerator and denominator

       [myFraction printsot: NO];

       NSLog(@"+");

       [anotherFraction printsot: NO];

       NSLog(@"=");



       resultFraction =   [myFraction add2:anotherFraction];

       [resultFraction printsot:YES];
   }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

你的病情

else if (resultreduce.denominator > resultreduce.numerator)

是错误的,应该是另一种方式

else if (resultreduce.numerator >= resultreduce.denominator)

涵盖5/3之类的案例。还计算“整个部分”

temp = resultreduce.numerator % resultreduce.denominator;

应该是

temp = resultreduce.numerator / resultreduce.denominator;

并且不要忘记添加最终else部分来涵盖案例 “分子<分母”。

最后,如果你想使用负分数,那么你必须进行比较 分子和分母的绝对值