Amazon.com Widgets Exercise 7.5
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 27, 2014, 11:15:16 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 7
| | | |-+ Exercise 7.5
Pages: [1] Go Down
Print
Author Topic: Exercise 7.5 (Read 890 times)
CloudDweller
Newbie
*
Posts: 17






on: February 25, 2012, 07:15:40 AM

Okay this one took me a long time but I think I kinda got there.  I would however love some feedback as my code looks very long compared to some others.

Fraction.m

Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator, whole;

-(void) print: (BOOL) reduce {
    Fraction *resultReduced = [[Fraction alloc] init];
    [resultReduced setTo:numerator over:denominator];
    
    if (denominator < 0) {
        numerator = -numerator;
        denominator = -denominator;
    }    
    if (reduce) {
        [resultReduced reduce];
        if (resultReduced.denominator < 0) {
            resultReduced.denominator = -resultReduced.denominator;
            resultReduced.numerator = -resultReduced.numerator;
        }
        else if (resultReduced.denominator == 1)
            NSLog (@"%i", resultReduced.numerator);
        else if (resultReduced.numerator == 0)
            NSLog (@"0");
        else if (resultReduced.numerator == resultReduced.denominator)
            NSLog (@"1");
        if (resultReduced.numerator > resultReduced.denominator || resultReduced.numerator < 0) {
            whole = resultReduced.numerator / resultReduced.denominator;
            resultReduced.numerator -= (whole * resultReduced.denominator);
            if (whole == 0)
                NSLog (@"%i/%i", resultReduced.numerator, resultReduced.denominator);
            else if (whole < 0 && resultReduced.numerator < 0) {
                resultReduced.numerator = -resultReduced.numerator;
                NSLog (@"%i %i/%i", whole, resultReduced.numerator, resultReduced.denominator);            
            }
            else
                NSLog (@"%i %i/%i", whole, resultReduced.numerator, resultReduced.denominator);
        }
        else
            NSLog (@"%i/%i", resultReduced.numerator, resultReduced.denominator);
    }
    else
        NSLog (@"%i/%i", numerator, denominator);
}

-(void) setTo: (int) n over: (int) d {
    numerator = n;
    denominator = d;
}

-(double) convertToNum {
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}

-(Fraction *) add: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = (numerator * f.denominator) + (denominator * f.numerator);
    result.denominator = denominator * f.denominator;
    
    return result;
}

-(Fraction *) subtract: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = (numerator * f.denominator) - (denominator * f.numerator);
    result.denominator = denominator * f.denominator;
    
    return result;
}

-(Fraction *) multiply: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    return result;
}

-(Fraction *) divide: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.denominator;
    result.denominator = denominator * f.numerator;
    
    return result;
}

-(void) reduce {
    int u = numerator;
    int v = denominator;
    int temp;
    
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    numerator /= u;
    denominator /= u;
}
@end

main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Fraction.h"

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
        Fraction *resultFraction;
        
        [aFraction setTo:-2 over:3];
        [bFraction setTo:-2 over:4];
        
        NSLog(@"Add fractions");        
        [aFraction print:NO];
        NSLog(@"+");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction add:bFraction];
        [resultFraction print:NO];
        [resultFraction print:YES];
        
        NSLog(@" ");
        NSLog(@"Subtract fractions");
        [aFraction print:NO];
        NSLog(@"-");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction subtract:bFraction];
        [resultFraction print:NO];
        [resultFraction print:YES];
        
        NSLog(@" ");
        NSLog(@"Multiply fractions");
        [aFraction print:NO];
        NSLog(@"*");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction multiply:bFraction];
        [resultFraction print:NO];
        [resultFraction print:YES];
        
        NSLog(@" ");
        NSLog(@"Divide fractions");
        [aFraction print:NO];
        NSLog(@"/");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction divide:bFraction];
        [resultFraction print:NO];
        [resultFraction print:YES];
    }
    return 0;
}

And finally here's my output
Code: (Objective-C)
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov  3 21:59:02 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
[Switching to process 79666 thread 0x0]
2012-02-25 14:05:21.844 Exercise 7[79666:707] Add fractions
2012-02-25 14:05:21.847 Exercise 7[79666:707] -2/3
2012-02-25 14:05:21.848 Exercise 7[79666:707] +
2012-02-25 14:05:21.849 Exercise 7[79666:707] -2/4
2012-02-25 14:05:21.850 Exercise 7[79666:707] =
2012-02-25 14:05:21.851 Exercise 7[79666:707] -14/12
2012-02-25 14:05:21.851 Exercise 7[79666:707] -1 1/6
2012-02-25 14:05:21.852 Exercise 7[79666:707]  
2012-02-25 14:05:21.853 Exercise 7[79666:707] Subtract fractions
2012-02-25 14:05:21.854 Exercise 7[79666:707] -2/3
2012-02-25 14:05:21.855 Exercise 7[79666:707] -
2012-02-25 14:05:21.856 Exercise 7[79666:707] -2/4
2012-02-25 14:05:21.857 Exercise 7[79666:707] =
2012-02-25 14:05:21.857 Exercise 7[79666:707] -2/12
2012-02-25 14:05:21.858 Exercise 7[79666:707] -1/6
2012-02-25 14:05:21.859 Exercise 7[79666:707]  
2012-02-25 14:05:21.860 Exercise 7[79666:707] Multiply fractions
2012-02-25 14:05:21.861 Exercise 7[79666:707] -2/3
2012-02-25 14:05:21.862 Exercise 7[79666:707] *
2012-02-25 14:05:21.862 Exercise 7[79666:707] -2/4
2012-02-25 14:05:21.864 Exercise 7[79666:707] =
2012-02-25 14:05:21.865 Exercise 7[79666:707] 4/12
2012-02-25 14:05:21.866 Exercise 7[79666:707] 1/3
2012-02-25 14:05:21.866 Exercise 7[79666:707]  
2012-02-25 14:05:21.867 Exercise 7[79666:707] Divide fractions
2012-02-25 14:05:21.870 Exercise 7[79666:707] -2/3
2012-02-25 14:05:21.872 Exercise 7[79666:707] /
2012-02-25 14:05:21.873 Exercise 7[79666:707] -2/4
2012-02-25 14:05:21.874 Exercise 7[79666:707] =
2012-02-25 14:05:21.875 Exercise 7[79666:707] 8/6
2012-02-25 14:05:21.876 Exercise 7[79666:707] 1 1/3
Program ended with exit code: 0

Like I said, some feedback would be very much appreciated.  I've really found this one hard so any criticism/tips no matter how harsh are encouraged.  I've tried to cover every eventuality but I'm sure I've missed something even though my code looks really, really long.

Thanks Steve for giving my brain a workout.  I haven't had to think this hard since I was at school  Grin
Last Edit: February 25, 2012, 07:22:46 AM by CloudDweller Logged
CloudDweller
Newbie
*
Posts: 17






Reply #1 on: February 26, 2012, 10:23:22 AM

I realised I wasn't checking for divide by zero so I've added this now as well as creating an improper method to deal with improper fractions. .  Here's my revised Fraction implementation file.

Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator, whole;

-(void) print: (BOOL) reduce {
    Fraction *resultReduced = [[Fraction alloc] init];
    [resultReduced setTo:numerator over:denominator];
    
    if (denominator == 0) {
        NSLog (@"Division by zero is not allowed.");
        exit(0);
    }
    if (denominator < 0) {
        numerator = -numerator;
        denominator = -denominator;
    }
    if (reduce) {
        [resultReduced reduce];
        if (resultReduced.denominator < 0) {
            resultReduced.denominator = -resultReduced.denominator;
            resultReduced.numerator = -resultReduced.numerator;
        }
        else if (resultReduced.denominator == 1)
            NSLog (@"%i", resultReduced.numerator);
        else if (resultReduced.numerator == 0)
            NSLog (@"0");
        else if (resultReduced.numerator == resultReduced.denominator)
            NSLog (@"1");
        if (resultReduced.numerator > resultReduced.denominator || resultReduced.numerator < 0) {
            [resultReduced improper];
            if (resultReduced.whole == 0)
                NSLog (@"%i/%i", resultReduced.numerator, resultReduced.denominator);
            else if (resultReduced.whole < 0 && resultReduced.numerator < 0) {
                resultReduced.numerator = -resultReduced.numerator;
                NSLog (@"%i %i/%i", resultReduced.whole, resultReduced.numerator, resultReduced.denominator);            
            }
            else
                NSLog (@"%i %i/%i", resultReduced.whole, resultReduced.numerator, resultReduced.denominator);
        }
        else if (resultReduced.numerator != 0)
            NSLog (@"%i/%i", resultReduced.numerator, resultReduced.denominator);
    }
    else
        NSLog (@"%i/%i", numerator, denominator);
}

-(void) setTo: (int) n over: (int) d {
    numerator = n;
    denominator = d;
}

-(double) convertToNum {
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}

-(Fraction *) add: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = (numerator * f.denominator) + (denominator * f.numerator);
    result.denominator = denominator * f.denominator;
    
    return result;
}

-(Fraction *) subtract: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = (numerator * f.denominator) - (denominator * f.numerator);
    result.denominator = denominator * f.denominator;
    
    return result;
}

-(Fraction *) multiply: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    return result;
}

-(Fraction *) divide: (Fraction *) f {
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.denominator;
    result.denominator = denominator * f.numerator;
    
    return result;
}

-(void) reduce {
    int u = numerator;
    int v = denominator;
    int temp;
    
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    numerator /= u;
    denominator /= u;
}

-(void) improper {
    int u = numerator;
    int v = denominator;
    int w;
    
    w = u / v;
    u -= w * v;
    
    numerator = u;
    denominator = v;
    whole = w;
}
@end

I'd still really like some feedback though as my print method code looks very messy compared to some others and I'd love to know if there are ways in which I could simplify/optimise it.
Logged
johnyorke
Newbie
*
Posts: 18


Email




Reply #2 on: March 06, 2012, 01:48:25 PM

Code: (Objective-C)
- (void) print: (bool) reduceFraction
{
    int tempNumerator = numerator;
    int tempDenominator = denominator;
    int tempConvertToNumber;
    int fullNumber;
   
    if (tempDenominator != 0) {
        if (reduceFraction == NO){
            tempConvertToNumber = tempNumerator / tempDenominator;
            if (tempConvertToNumber >= 1){
                fullNumber = tempNumerator / tempDenominator;
                tempNumerator = tempNumerator % tempDenominator;
                if (tempNumerator == 0){
                    NSLog(@"%i", fullNumber);
                }
                else NSLog(@"%i %i/%i", fullNumber, tempNumerator, tempDenominator);
            }
           
            else NSLog(@"%i/%i", numerator, denominator);
        }
       
        else {
            int u = tempNumerator;
            int v = tempDenominator;
            int temp;
           
            while (v != 0) {
                temp = u % v;
                u = v;
                v = temp;
            }
            tempNumerator /= u;
            tempDenominator /= u;
           
            tempConvertToNumber = tempNumerator / tempDenominator;
            if (tempConvertToNumber >= 1){
                fullNumber = tempNumerator / tempDenominator;
                tempNumerator = tempNumerator % tempDenominator;
                if (tempNumerator == 0){
                    NSLog(@"%i", fullNumber);
                }
                else NSLog(@"%i %i/%i", fullNumber, tempNumerator, tempDenominator);
            }
            else NSLog(@"%i/%i", tempNumerator, tempDenominator);
        }
    }
    else NSLog(@"Division by zero is not allowed.");
}
Feedback welcome!
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.