I am new to objective-c languange, and exercising with basic math (fractions) with (+) or add operator.
here is I wanted to make 2/3 + 3/3 = 5/3, I want to display this fraction as 1 2/3.
notes : 5/3 ==> 5 is numerator, 3 is denominator.
but somehow my code wont work, I believe my mistake is on if statement on this instance.
- (void)printsot:(BOOL)reduceFrac;
but I still can't figure it out, why output doesn't print the result out, it doesn't NSLog anything.
here are my codes related to my question above
- (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);
}
}
and here is my reduce instance method
- (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;
}
current output from 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] =
my main files :
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;
}
Your condition
else if (resultreduce.denominator > resultreduce.numerator)
is wrong, it should be the other way around
else if (resultreduce.numerator >= resultreduce.denominator)
to cover a case like 5/3
. Also the computation of the "entire part"
temp = resultreduce.numerator % resultreduce.denominator;
should be
temp = resultreduce.numerator / resultreduce.denominator;
And don't forget to add a final else
part to cover the case "numerator < denominator".
Finally, if you want to work with negative fractions as well then you have to compare the absolute values of numerator and denominator.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments