Hi there! So I'm learning Objective-C with the book: Programming in Objective-C 2.0 by Stephen Kochan. One of the programs use in the book is a program for adding fractions. Well, I'm the kind of guy that likes to take things to the next level, so I edited the program so that it also subtracts, and makes all improper fraction mixed numbers. One of the problems I'm having is that when doing subtraction I always get the same answer. Here's my code. (I'll add comments so you can keep track of what's going on)
The @interface file:
The @implementation file:
The main file
I know that's a TON so if ANYONE can offer words of advice, or a basic way to clean up the coding, please help! Thanks!!!
-iMaster
The @interface file:
Code:
//you shouldn't need much explanation here
#import <Foundation/Foundation.h>
@interface Fraction: NSObject {
int numerator;
int denominator;
}
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) add: (Fraction *) f;
-(void) simplify;
-(void) getInput;
-(void) subtract: (Fraction *) f;
@end
The @implementation file:
Code:
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print //print the fraction
{
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum //not sure what this does, but he used it in the book
{
if(denominator != 0)
return (double) numerator/denominator;
else
return 1.0;
}
-(void) setTo: (int) n over: (int) d //manually set the value of the numerator and denominator
{
numerator = n;
denominator = d;
}
-(void) add: (Fraction *) f //add 2 fractions
{
//algorithm (it is correct)
numerator = numerator * f.denominator + denominator * f.numerator;
denominator = denominator * f.denominator;
[self simplify];
}
-(void) simplify //simplifies the fractions
{
int d = 0;
int e = numerator;
double wholeNumber;
int n = denominator;
while (n != 1) {
//simplifies fractions
if(denominator != 1 && denominator % n == 0 && numerator % n == 0) {
denominator = denominator / n;
numerator = numerator / n;
} else --n;
}
//identifies improper fractions and makes them mixed numbers
if(numerator > denominator) {
NSLog(@"Improper fraction");
numerator = numerator % denominator;
do{
e = numerator - denominator;
++wholeNumber;
} while(e - denominator > denominator);
do{
++d;
}
while(d < wholeNumber);
wholeNumber = d;
NSLog(@"%f %i/%i", wholeNumber, numerator, denominator);
} else NSLog(@"%i/%i", numerator, denominator);
}
-(void) getInput
{
//gets fraction input from user
int usrNum, usrDen;
scanf("%i/%i", &usrNum, &usrDen);
numerator = usrNum;
denominator = usrDen;
}
-(void) subtract: (Fraction *) f //subtracts fraction PROBLEM HERE!
{
//makes fractions have same denominator
if(denominator != f.denominator) {
denominator = denominator * f.denominator;
f.denominator = f.denominator * denominator;
numerator = numerator * denominator;
f.numerator = f.numerator * f.denominator;
}
//checks if solution will be a negative (still working on this this is what I'm, trying to figure out!!)
if(numerator / denominator < f.numerator / f.denominator) {
numerator = f.numerator - numerator;
denominator = denominator;
} else {
numerator = numerator - f.numerator;
denominator = denominator;
}
[self simplify];
}
@end
The main file
Code:
#import "Fraction.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction = [[Fraction alloc] init];
Fraction *myFraction2 = [[Fraction alloc] init];
char operator;
NSLog(@"Would you like to add or substract? (enter '-' for subtraction and '+' for addition)");
scanf("%c", &operator);
//checks whether user wants to add or subtract
switch (operator) {
case '+':
//adds
NSLog(@"Please print your first fraction in the format num/den.");
[myFraction getInput];
NSLog(@"Please print your second fraction in the format num/den.");
[myFraction2 getInput];
[myFraction print];
NSLog(@"+");
[myFraction2 print];
NSLog(@"=");
[myFraction add: myFraction2];
break;
case '-':
//subtracts
NSLog(@"Please print your first fraction in the format num/den.");
[myFraction getInput];
NSLog(@"Please print your second fraction in the format num/den.");
[myFraction2 getInput];
[myFraction print];
NSLog(@"-");
[myFraction2 print];
NSLog(@"=");
[myFraction subtract: myFraction2];
break;
}
[myFraction release];
[myFraction2 release];
[pool drain];
return 0;
}
I know that's a TON so if ANYONE can offer words of advice, or a basic way to clean up the coding, please help! Thanks!!!
-iMaster