Adding Fraction in objective c

user3213703

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;
}
Martin R

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding Fraction to current fraction?

From Dev

Adding a Series of Fraction to the nth Power and Finding the Sum - C++

From Dev

Adding Objective-C method to class at runtime

From Dev

Adding objects to an NSMutableArray in Objective-C

From Dev

Adding constraints programmatically in Objective-C

From Dev

Adding Objective C Syntax Highlighting to Visual Studio

From Dev

Adding Objective-C method to class at runtime

From Dev

Error presentViewController - view controllers adding - objective c

From Dev

Adding Cloudinary Pod in iOS Objective C

From Dev

calculating the fraction with struct in c

From Dev

Fraction addition in C++

From Dev

Fraction reduction C program

From Dev

Adding an Objective-C++ file to a C++ Xcode 5 project

From Dev

Clarification on adding Objective C code to a swift dynamic framework

From Dev

Adding SearchBar to NavigationBar Objective-C iOS 9

From Dev

Similar way adding 2d array in Objective-C

From Dev

Adding a UINavigationController to my segue.destinationViewController in Objective-C

From Dev

tableView adding/removing to second section objective-c

From Dev

Swift Extension fails adding overloaded methods to Objective-C class

From Dev

Objective C - OS X - Issue adding NSShadow to NSImageView

From Dev

Objective-C: Adding 10 seconds to timer in SpriteKit

From Dev

Objective-C - Adding some properties without extending class, is there a way?

From Dev

Adding a marker image to Google Maps on IOS Objective-C app

From Dev

Objective-c program crashes when adding object to NSMutableArray

From Dev

iOS - Error when adding row to UITable (Objective-C)

From Dev

Adding Integer getting User Input From console In Objective-C

From Dev

MS Word: Adding small numbers above and below a fraction

From Dev

Having trouble in c# fraction calculator

From Dev

Efficiently multiplying with a constant fraction in C++

Related Related

  1. 1

    Adding Fraction to current fraction?

  2. 2

    Adding a Series of Fraction to the nth Power and Finding the Sum - C++

  3. 3

    Adding Objective-C method to class at runtime

  4. 4

    Adding objects to an NSMutableArray in Objective-C

  5. 5

    Adding constraints programmatically in Objective-C

  6. 6

    Adding Objective C Syntax Highlighting to Visual Studio

  7. 7

    Adding Objective-C method to class at runtime

  8. 8

    Error presentViewController - view controllers adding - objective c

  9. 9

    Adding Cloudinary Pod in iOS Objective C

  10. 10

    calculating the fraction with struct in c

  11. 11

    Fraction addition in C++

  12. 12

    Fraction reduction C program

  13. 13

    Adding an Objective-C++ file to a C++ Xcode 5 project

  14. 14

    Clarification on adding Objective C code to a swift dynamic framework

  15. 15

    Adding SearchBar to NavigationBar Objective-C iOS 9

  16. 16

    Similar way adding 2d array in Objective-C

  17. 17

    Adding a UINavigationController to my segue.destinationViewController in Objective-C

  18. 18

    tableView adding/removing to second section objective-c

  19. 19

    Swift Extension fails adding overloaded methods to Objective-C class

  20. 20

    Objective C - OS X - Issue adding NSShadow to NSImageView

  21. 21

    Objective-C: Adding 10 seconds to timer in SpriteKit

  22. 22

    Objective-C - Adding some properties without extending class, is there a way?

  23. 23

    Adding a marker image to Google Maps on IOS Objective-C app

  24. 24

    Objective-c program crashes when adding object to NSMutableArray

  25. 25

    iOS - Error when adding row to UITable (Objective-C)

  26. 26

    Adding Integer getting User Input From console In Objective-C

  27. 27

    MS Word: Adding small numbers above and below a fraction

  28. 28

    Having trouble in c# fraction calculator

  29. 29

    Efficiently multiplying with a constant fraction in C++

HotTag

Archive