Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
January 23, 2018, 08:58:52 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: 15-7 Simple way  (Read 1314 times)
tadej5553
Full Member
***
Posts: 145


Email
« on: January 06, 2010, 11:22:15 AM »

main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Fraction.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];
}
@end
  

int main (int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSMutableArray *myArry = [NSMutableArray array];
    Fraction *f = [[Fraction alloc] init];
    Fraction *temp;
    int i;
    
      while (--i) {      
        temp = [Fraction randomFraction];    
        [myArry addObject: temp];      
        [temp print];      
        [temp release];                       // gets retained by addObject: method    
    }
    
    NSLog(@"\n");
    for (Fraction *f1 in myArry)
    {
       temp = f;
       f = [f add: f1];
       [temp release];
    }    
    
    NSLog(@"The sum of all random Fractions is:");
    [f print];
    
    [f release];
    [pool drain];
    return 0;
}

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

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

@property int numerator, denominator;

-(Fraction *) init; //To set the fraction to 0/1 (0, but to work with add:)
-(void) print;
-(double)  convertToNum;
-(void) setTo: (int) num over: (int) denum;
-(Fraction *) reduce;

-(Fraction *) add: (Fraction *) f;

@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;
}

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

@end
« Last Edit: May 23, 2010, 12:34:51 PM by tadej5553 » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: January 06, 2010, 11:38:36 AM »

I really like this code, particularly the way you extended the Fraction class by adding a category.  Smiley

However, there is a memory leak here.  The fractions that get allocated by randomFraction are not getting released.    You could change your first for loop to handle this:

Code: (Objective-C)
    for (i = 0; i < 10; ++i)  
    {  
        temp = [Fraction randomFraction];
        [myArry addObject: temp];  
        [temp print];  
        [temp release];                       // gets retained by addObject: method
    }

You could alternatively add the Fraction to the autorelease pool when it gets created in randomFraction.  I think both of these ideas have not yet been discussed by Chapter 15 in the book, so this is getting a little ahead of the text.

Cheers,

Steve
« Last Edit: January 06, 2010, 11:42:23 AM by skochan » Logged
tadej5553
Full Member
***
Posts: 145


Email
« Reply #2 on: January 06, 2010, 11:49:06 AM »

Thanks for the warning, I've already fixed it.

Actually I am at chapter 19 right now (with reading, not exercises), so when I was making this programm I was thinking about memory leaks.
I guess I missed this one.
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.