Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 09:38:27 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Exercise 5  (Read 1206 times)
MCaEd
Newbie
*
Posts: 49


« on: October 10, 2011, 09:33:32 AM »

Here is my code for print method:

Code: (Objective-C)
-(void) print
{
    if (numerator > denominator)
    {
        int entire = numerator / denominator;
        int newNum = numerator % denominator;
        Fraction *newFrac = [[Fraction alloc] init];
       
        newFrac.numerator = newNum;
        newFrac.denominator = denominator;
       
        [newFrac reduce];
       
        if (!newFrac.numerator)
            NSLog (@"%i", entire);
        else
            NSLog (@"%i %i/%i", entire, newFrac.numerator, newFrac.denominator);
       
        [newFrac release];
    }
    else if (numerator == denominator)
    {
        int oneNumber = numerator / denominator;
        NSLog (@"%i (whole)", oneNumber);
    }
    else if (!denominator)
        NSLog (@"NAN");
}

And I have print: method included from the previous exercises as well:

Code: (Objective-C)
-(void) print: (BOOL) lessen
{
    if (lessen)
    {
        Fraction *testFrac = [[Fraction alloc] init];
        [testFrac setTo: numerator over: denominator];
        [testFrac reduce];
       
        if (testFrac.denominator < 0)
        {
            testFrac.denominator = -testFrac.denominator;
            testFrac.numerator = -testFrac.numerator;
        }
       
        NSLog (@"%i/%i (reduced)", testFrac.numerator, testFrac.denominator);
        [testFrac release];
    }
    else
    {
        if (denominator < 0)
        {
            numerator = -numerator;
            denominator = -denominator;
        }
        NSLog (@"%i/%i (non-reduced)", numerator, denominator);
    }
}

If it's possible to reduce the code or make it better, feel free to leave a comment/message.
Thanks
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.