Amazon.com Widgets 13-2 Simple way
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 07:35:32 PM
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 13
| | | |-+ 13-2 Simple way
Pages: [1] Go Down
Print
Author Topic: 13-2 Simple way (Read 1397 times)
tadej5553
Full Member
***
Posts: 145


Email




on: May 21, 2010, 11:48:04 AM

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

@interface Fraction : NSObject
{
    int numerator, denominator;
}

@property int numerator, denominator;

-(void) print;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
-(Fraction *) reduce;

@end

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

static int gcd (int u, int v)
{
    int temp;
   
    while (v) {
        temp = u % v;
        u = v;
        v = temp;
    }
   
    return u;
}

@implementation Fraction

@synthesize numerator, denominator;

-(void) print
{
    if(denominator < 0)
    {   
        denominator = -denominator;
        numerator = -numerator;
    }
   
    if(!denominator || !numerator)
        NSLog(@"0");
   
    else if(denominator == 1)
        NSLog(@"%i", numerator);
   
    else
    {
        if(numerator < 0)
        {
            if (-numerator > denominator)
                NSLog(@"%i %i/%i", numerator/denominator, -numerator%denominator, denominator);
            else
                NSLog(@"%i/%i", numerator, denominator);
        }
       
        else if (numerator > denominator)
            NSLog(@"%i %i/%i", numerator/denominator, numerator%denominator, denominator);
        else
            NSLog(@"%i/%i", numerator, denominator);
    }
}

-(double) convertToNum
{
    if(denominator)
        return (double) numerator/denominator;

    return 0.0;
}

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

-(Fraction *) reduce
{
    int u = gcd(numerator, denominator);

    numerator /= u;
    denominator /= u;
    return self;
}

@end

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

int main (int argc, const char * argv[])

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
   
    Fraction *f1 = [[Fraction alloc] init];
    Fraction *f2 = [[Fraction alloc] init];
   
    [f1 setTo: 25 over: 50];
    [f2 setTo: 49 over: 21];
   
    [f1 reduce];
    [f2 reduce];
   
    [f1 print];
    [f2 print];
   
    [f1 release];
    [f2 release];
   
    [pool drain]; 
    return 0; 
}


The way I see it, the only benefit from making the function static is that
it stays private to the file. Another one is thaht it doesn't create new variables every time it is called

I think that using functions is better than using the whole code.
Mainly because it provides more readable, cleaner code and because you have a "source code", so you don't have to
change every sample of the code, just the source. Afcourse in this case, it doesn't matter.
 
 
Last Edit: May 21, 2010, 12:05:34 PM by tadej5553 Logged
bamboo
Newbie
*
Posts: 14






Reply #1 on: April 19, 2011, 01:26:29 PM

"Another one is thaht it doesn't create new variables every time it is called"

I dont get it.

Does that mean making a function static  also makes the variables static?
Logged
iapplethis
Newbie
*
Posts: 13






Reply #2 on: April 28, 2011, 08:43:25 AM

Well I think it does, and since before using any of the variables they are being assinged new values so they don't contain their old ones.

One other thing I see in the code above is that reduce: returns self, why is that? You could just make the method void and delete the return line, aren't you leaking memory when you return self to nothing?
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.