Amazon.com Widgets 17-3 Simple way
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 19, 2013, 12:22:10 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 17
| | | |-+  17-3 Simple way
Pages: [1]   Go Down
Print
Author Topic: 17-3 Simple way  (Read 874 times)
tadej5553
Full Member
***
Posts: 145


Email




« on: January 22, 2010, 12:04:47 PM »

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

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

@property int numerator, denominator;

-(Fraction *) init;
-(void) print;
-(double)  convertToNum;
-(void) setTo: (int) num over: (int) denum;
-(Fraction *) reduce;

@end

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

@implementation Fraction

@synthesize numerator, denominator;

-(Fraction *) init
{
    self = [super init];

    if(self)
        [self setTo:0 over:1];
   
    return self;
}

-(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 = numerator, v = denominator;
    int temp;
   
    while (v) {
        temp = u % v;
        u = v;
        v = temp;
    }
   
    numerator /= u;
    denominator /= u;
    return self;
}

@end 

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

@interface Fraction (MathOps)

-(Fraction *) add: (Fraction *) f;
-(Fraction *) substract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;

@end

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

@implementation Fraction (MathOps)

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

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

-(Fraction *) multiply: (Fraction *) f
{
    Fraction *result = [[[Fraction alloc] init] autorelease];

    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    return [result reduce];
}

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

@end

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

@interface Fraction (Random)  
+(Fraction *) randomFraction;  
@end  

@implementation Fraction (Random)  
+(Fraction *) randomFraction  
{  
    Fraction *f = [[Fraction alloc] init];  
    
    [f setTo: arc4random() % LIMIT + 1  
        over: arc4random() % LIMIT + 1];  
    
    return [[f reduce] autorelease];  
}  
@end  

int main (int argc, char *argv[])  
{  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    
    Fraction *f1 = [Fraction randomFraction];
    Fraction *f2 = [Fraction randomFraction];  
    
    [f1 print];
    [f2 print];
    
    [[f1 add: f2] print];       //Having a little fun :)
    [[f1 substract: f2] print];  
    [[f1 multiply: f2] print];  
    [[f1 divide: f2] print];  
 
    [pool drain];  
    return 0;  
}  

Yes, you can write statement like [[f1 add: f2] print] without having a memory leak because even thought that you didn't obtain the instance the object was added to the autorelease pool and will be released together with the autorelease pool.
« Last Edit: May 23, 2010, 12:42:16 PM by tadej5553 » 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.