Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 16, 2017, 08:33:00 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: Program 10.2  (Read 3398 times)
esc
Global Moderator
Full Member
*****
Posts: 230


« on: February 25, 2009, 03:46:00 AM »

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

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

@property int numerator, denominator;

+(Fraction *) allocF;
+(int) count;
-(Fraction *) initWith: (int) n: (int) d;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) reduce;
-(Fraction *) add: (Fraction *) f;

@end

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

static int gCounter;

@implementation Fraction

@synthesize numerator, denominator;

+(Fraction *) allocF
{
extern int gCounter;
++gCounter;

return [Fraction alloc];
}

+(int) count
{
extern int gCounter;

return gCounter;
}

-(Fraction *) initWith: (int) n: (int) d
{
self = [super init];

if (self)
[self setTo: n over: d];

return self;
}

-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}

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

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

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}

-(Fraction *) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c) / (b * d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

@end

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

int main (int argc, char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Fraction *a, *b, *c;

NSLog (@"Fractions allocated: %i", [Fraction count]);

a = [[Fraction allocF] init];
b = [[Fraction allocF] init];
c = [[Fraction allocF] init];

NSLog (@"Fractions allocated: %i", [Fraction count]);
[a release];
[b release];
[c release];

[pool drain];
    return 0;
}
Logged
peoplesoft
Newbie
*
Posts: 2


« Reply #1 on: August 05, 2009, 03:42:16 AM »

shouldn't these lines:
a = [[Fraction allocF] init];
b = [[Fraction allocF] init];
c = [[Fraction allocF] init];

read:
a = [[Fraction allocF] initwith 1:3];
b = [[Fraction allocF] initwith 4:5];
c = [[Fraction allocF] initwith 7:8];

the numbers are arbitrary.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #2 on: August 05, 2009, 04:36:18 AM »

For this example, we're just calculating the number of Fractions allocated.  Of course, if you wanted to actually use the fractions in your program, you would want to set the numerators and denominators.

Cheers,

Steve Kochan
Logged
peoplesoft
Newbie
*
Posts: 2


« Reply #3 on: August 05, 2009, 09:04:58 AM »

Thank you very much!
Logged
razor
Newbie
*
Posts: 12


« Reply #4 on: December 20, 2010, 11:16:05 AM »

Thanks, I was pulling my hair out on this one.  I missed the line;

Code: (Objective-C)
static int gCounter;

in the book and couldn't figure out what the error was.  Now I know, and knowing is half the battle.   Grin
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.