Hi all,
I've been going through Kochan's objectivive c book, and I ran into some code that I have a quick question about. In the code below, there is a reduce function that reduces a fraction. I am currently calling it from inside the add function. In an earlier book example, we did the same thing and it reduced the fraction without trouble... in fact, that was the point of the example... to show that that worked. Then we added a few things (the new resultFraction object mostly) in the program section, and now when I use the add function, the fraction is not reduced. If I call the reduce function from the program section of the code, then it works fine, but I would like to know why it isn't working from inside the add function anymore. Anyone have any thoughts? here is the code... it is code right from the book, so things are meant to be done the way they are done for this example (however, it was broken up into 3 different files... I hope I have not missed anything when I put them together for this posting). Thanks in advance for your thoughts!
Beck
I've been going through Kochan's objectivive c book, and I ran into some code that I have a quick question about. In the code below, there is a reduce function that reduces a fraction. I am currently calling it from inside the add function. In an earlier book example, we did the same thing and it reduced the fraction without trouble... in fact, that was the point of the example... to show that that worked. Then we added a few things (the new resultFraction object mostly) in the program section, and now when I use the add function, the fraction is not reduced. If I call the reduce function from the program section of the code, then it works fine, but I would like to know why it isn't working from inside the add function anymore. Anyone have any thoughts? here is the code... it is code right from the book, so things are meant to be done the way they are done for this example (however, it was broken up into 3 different files... I hope I have not missed anything when I put them together for this posting). Thanks in advance for your thoughts!
Beck
Code:
#import <objc/Object.h>
#import <stdio.h>
//--------------------@interface section
// Define the Fraction class
@interface Fraction : Object
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) setTo: (int) n over: (int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;
-(Fraction *) add: (Fraction *) f;
-(void) reduce;
@end
//----------@implementation section
@implementation Fraction;
-(void) print
{
printf (" %i/%i ", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
-(int) numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
-(Fraction *) add: (Fraction *) f
{
//to add two fraction together using
//a/b + c/d = ((a*d) + (b*c)) / (b*d) yikes! ****ing math.
Fraction *result = [Fraction new];
int resultNum, resultDenom;
resultNum = (numerator * [f denominator]) + (denominator * [f numerator]);
resultDenom = denominator * [f denominator];
[result setTo:resultNum over:resultDenom];
[self reduce];
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
//--------------------program section
int main (int argc, char *argv[])
{
Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];
Fraction *resultFraction;
//set the fractions to be used
[aFraction setTo: 1 over: 4];
[bFraction setTo: 1 over: 2];
// add result
[aFraction print];
printf(" + ");
[bFraction print];
printf(" = ");
//fraction is reduced in the add function
resultFraction = [aFraction add: bFraction];
[resultFraction print];
printf ("\n");
[aFraction free];
[bFraction free];
[resultFraction free];
return 0;
}