Hey everyone,
I'm reading "Programming in Objective-C (6th edition)" and one of the exercises is to display the mixed numbers from a fraction, so 5/3 would be
1 2/3.
I'm adding 1/2 and 4/2, which results in 10/4, then I have a reduce method which changes 10/4 into 5/2, and this is where I loose track.
The actual exercise:
Modify the Fractions print method to display fractions greater than 1 as mixed numbers. For example, the fraction 5/3 should be displayed as 1 2/3.
I made a new method:
Fraction.h file
Fraction.m file
and my main.m file
Ok, so the import part is where it (I think) goes wrong is this:
When numerator -= denominator; is removed, the program will print "2 5/2", when it is left in, the program will print "0 1/2" and I can't figure out why.
I've tried numerator -= v;, I've taken it out of the while loop and put it right before and after return mixNum;, I've tried a do-while statement, it just won't work.
If anyone can point out what I've done wrong, I would greatly appreciate it.
Thanks in advance.
I'm reading "Programming in Objective-C (6th edition)" and one of the exercises is to display the mixed numbers from a fraction, so 5/3 would be
1 2/3.
I'm adding 1/2 and 4/2, which results in 10/4, then I have a reduce method which changes 10/4 into 5/2, and this is where I loose track.
The actual exercise:
Modify the Fractions print method to display fractions greater than 1 as mixed numbers. For example, the fraction 5/3 should be displayed as 1 2/3.
I made a new method:
Fraction.h file
Code:
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
@property int numerator, denominator;
-(void)printReduced: (bool)b;
-(void)setNum: (int)n setDi: (int)d;
-(void)reduce;
[B]-(int)mixedNumbers;[/B]
-(double)convertToNum;
-(Fraction *)add: (Fraction *)f;
-(Fraction *)subtract: (Fraction*)f;
-(Fraction *)multiply: (Fraction *)f;
-(Fraction *)divide: (Fraction *)f;
@end
Fraction.m file
Code:
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator; // Name the variables numerator and denominator, instead of _numerator and _denominator, like the @property does automatically
-(void)printReduced: (bool)b {
if (b == YES) {
[self reduce];
}
if ([self mixedNumbers] > 0) {
NSLog(@"%i %i/%i", [self mixedNumbers], numerator, denominator);
} else {
NSLog(@"%i/%i", numerator, denominator);
}
}
-(void)setNum: (int)n setDi: (int)d {
numerator = n;
denominator = d;
}
-(void)reduce {
int u = numerator, v = denominator, temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
[B]-(int)mixedNumbers {
int u = numerator, v = denominator, mixNum = 0;
while (u > v) {
mixNum += 1;
u -= v;
numerator -= denominator;
}
return mixNum;
}[/B]
-(double)convertToNum {
if (denominator != 0) {
return (double)numerator / denominator;
} else {
return NAN;
}
}
-(Fraction *)add: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
-(Fraction *)subtract: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator - denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
-(Fraction *)multiply: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
-(Fraction *)divide: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator;
result.denominator = denominator * f.numerator;
return result;
}
@end
and my main.m file
Code:
#import <Foundation/Foundation.h>
#import "Fraction.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Fraction *myFraction = [[Fraction alloc] init];
Fraction *myFractionB = [[Fraction alloc] init];
Fraction *resultFraction;
// Set fraction to 1/3
[myFraction setNum: 1 setDi: 2];
[myFractionB setNum: 4 setDi: 2];
// Display the fraction
NSLog(@"%i/%i + %i/%i is:", myFraction.numerator, myFraction.denominator, myFractionB.numerator,
myFractionB.denominator);
resultFraction = [myFraction add: myFractionB];
[resultFraction printReduced: YES];
}
return 0;
}
Ok, so the import part is where it (I think) goes wrong is this:
Code:
-(int)mixedNumbers {
int u = numerator, v = denominator, mixNum = 0;
while (u > v) {
mixNum += 1;
u -= v;
[B]numerator -= denominator;[/B]
}
return mixNum;
}
When numerator -= denominator; is removed, the program will print "2 5/2", when it is left in, the program will print "0 1/2" and I can't figure out why.
I've tried numerator -= v;, I've taken it out of the while loop and put it right before and after return mixNum;, I've tried a do-while statement, it just won't work.
If anyone can point out what I've done wrong, I would greatly appreciate it.
Thanks in advance.