So, while making the fraction calculator from the book, I did the exact code in the book, and did everything correctly. And so it worked.
So thats nice, so I tweaked it. And then it didn't work.
There are no errors, but I am keep getting the answer as 0 when I try to use it. What did I do wrong?
What I tweaked it is I made it so you can enter as many fractions you want at once, rather then only two. I made an array, put the fractions in there, and for calculating I enumerated and added (or whatever) them. Its lots of code, but please help!
Fraction Class
#import <UIKit/UIKit.h>
@interface Fraction : NSObject <NSCopying>
{
int numerator;
int denominator;
}
@property int numerator, denominator;
- (void) print;
- (void) setTo: (int) n over: (int) d;
- (double) convertToNum;
- (NSString *) convertToString;
- (void) reduce;
- (Fraction*) add: (Fraction *) f;
- (Fraction*) subtract: (Fraction *) f;
- (Fraction*) multiply: (Fraction *) f;
- (Fraction*) divide: (Fraction *) f;
@end
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
- (void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
- (double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
- (NSString *) convertToString {
if (numerator == 0)
return @"0";
if (numerator == denominator) {
return @"1";
}
else if ( denominator == 1)
return [NSString stringWithFormat: @"%i", numerator];
else
return [NSString stringWithFormat: @"%i/%i", numerator, denominator];
}
- (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)
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 autorelease];
}
- (Fraction*) subtract: (Fraction *) f
{
// To subtract two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b * d)
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 autorelease];
}
- (Fraction*) divide: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;
resultNum = (numerator * f.denominator);
resultDenom = denominator * f.numerator;
[result setTo: resultNum over: resultDenom];
[result reduce];
return [result autorelease];
}
- (Fraction*) multiply: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;
resultNum = (numerator * f.numerator);
resultDenom = denominator * f.denominator;
[result setTo: resultNum over: resultDenom];
[result reduce];
return [result autorelease];
}
- (id) copyWithZone: (NSZone *) zone
{
Fraction *newFraction = [[Fraction allocWithZone: zone] init];
newFraction.numerator = self.numerator;
newFraction.denominator = self.denominator;
return [newFraction autorelease];
}
@end
View Controller
#import <UIKit/UIKit.h>
#import "Calculator.h"
@interface Fraction_CalculatorViewController : UIViewController
{
UILabel *display;
char op;
int currentNumber;
NSMutableString *displayString;
BOOL isNumerator;
Calculator *myCalculator;
}
@property (nonatomic, retain) IBOutlet UILabel *display;
@property (nonatomic, retain) NSMutableString *displayString;
-(void) processDigit: (int) digit;
-(void) processOp: (char) theOp;
-(void) storeFracPart;
// Numeric keys
-(IBAction) clickDigit: (id) sender;
//Arithmetic Operation keys
-(IBAction) clickPlus: (id) sender;
-(IBAction) clickMinus: (id) sender;
-(IBAction) clickMultiply: (id) sender;
-(IBAction) clickDivide: (id) sender;
// Misc. Keys
-(IBAction) clickOver: (id) sender;
-(IBAction) clickEquals: (id) sender;
-(IBAction) clickClear: (id) sender;
@end
#import "Fraction_CalculatorViewController.h"
@implementation Fraction_CalculatorViewController
@synthesize display, displayString;
- (void)viewDidLoad {
isNumerator = YES;
self.displayString = [NSMutableString stringWithCapacity: 40];
myCalculator = [[Calculator alloc] init];
[super viewDidLoad];
}
-(void) processDigit: (int) digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat: @"%i", digit]];
[display setText: displayString];
}
-(IBAction) clickDigit: (id) sender
{
int digit = [sender tag];
[self processDigit: digit];
}
-(void) processOp: (char) theOp
{
NSString *opStr;
op = theOp;
switch (theOp) {
case '+':
opStr = @" + ";
break;
case '-':
opStr = @" - ";
break;
case '*':
opStr = @" * ";
break;
case '/':
opStr = @" / ";
break;
}
[self storeFracPart];
isNumerator = YES;
[myCalculator.fractions addObject: [myCalculator.operand copy]];
[myCalculator.operand setTo: 0 over: 1];
[displayString appendString: opStr];
[display setText: displayString];
}
-(void) storeFracPart
{
if (isNumerator) {
myCalculator.operand.numerator = currentNumber;
myCalculator.operand.denominator = 1; // e.g. 3 * 4/5
}
else {
myCalculator.operand.denominator = currentNumber;
}
currentNumber = 0 ;
}
-(IBAction) clickOver: (id) sender
{
[self storeFracPart];
isNumerator = NO;
[displayString appendString: @"/"];
[display setText: displayString];
}
// Arithmetic Operation keys
-(IBAction) clickPlus: (id) sender
{
[self processOp: '+'];
}
-(IBAction) clickMinus: (id) sender
{
[self processOp: '-'];
}
-(IBAction) clickMultiply: (id) sender
{
[self processOp: '*'];
}
-(IBAction) clickDivide: (id) sender
{
[self processOp: '/'];
}
// Misc. Keys
-(IBAction) clickEquals: (id) sender
{
[self storeFracPart];
[myCalculator performOperation: op];
[displayString appendString: @" = "];
[displayString appendString: [myCalculator.accumulator convertToString]];
[display setText: displayString];
currentNumber = 0;
isNumerator = YES;
[displayString setString: @""];
}
-(IBAction) clickClear: (id) sender
{
isNumerator = YES;
currentNumber = 0;
[myCalculator clear];
[displayString setString: @""];
[display setText: displayString];
}
- (void) dealloc
{
[myCalculator dealloc];
[super dealloc];
}
@end
Calculator Class
#import <UIKit/UIKit.h>
#import "Fraction.h"
@interface Calculator : NSObject
{
Fraction *accumulator;
Fraction *operand;
NSMutableArray *fractions;
}
@property (retain, nonatomic) Fraction *operand, *accumulator;
@property (retain, nonatomic) NSMutableArray *fractions;
- (void) performOperation: (char) op;
- (void) clear;
@end
#import "Calculator.h"
@implementation Calculator
@synthesize operand, accumulator, fractions;
- (id) init
{
self = [super init];
operand = [[Fraction alloc] init];
accumulator = [[Fraction alloc] init];
return self;
}
- (void) clear
{
if (accumulator) {
accumulator.numerator = 0;
accumulator.denominator = 0;
}
}
- (void) performOperation: (char) op
{
Fraction *nextFrac;
[accumulator setTo: 0 over: 1];
switch (op) {
case '+':
for (nextFrac in fractions) {
accumulator = [accumulator add: nextFrac];
}
break;
case '-':
for (nextFrac in fractions) {
accumulator = [accumulator subtract: nextFrac];
}
break;
case '*':
for (nextFrac in fractions) {
accumulator = [accumulator multiply: nextFrac];
}
break;
case '/':
for (nextFrac in fractions) {
accumulator = [accumulator divide: nextFrac];
}
break;
}
}
-(void) dealloc
{
[operand release];
[accumulator release];
[super dealloc];
}
@end