When i run the program everything works fine except for when pressing = when getting the result
i believe this part of code is whats giving me the wrong result but its what was showed in the book
so for example if i do 1/2 + 1/2 i get = 37/202
- (void) processDigit: (int) digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat: @"%i" , digit]];
display.text = displayString;
}
Calculator.h
#import <UIKit/UIKit.h>
#import "Fraction.h"
@interface Calculator : NSObject
@property (strong, nonatomic) Fraction *operand1, *operand2, *accumulator;
-(Fraction *) performOperation: (char) op;
-(void) clear;
@end
calculator.m
#import "Calculator.h"
@implementation Calculator
@synthesize operand1, operand2, accumulator;
-(id) init
{
self = [super init];
if(self)
{
operand1 = [[Fraction alloc] init];
operand2 = [[Fraction alloc] init];
accumulator = [[Fraction alloc] init];
}
return self;
}
-(void) clear
{
accumulator.numerator = 0;
accumulator.denominator = 0;
}
-(Fraction *) performOperation:(char)op
{
Fraction *result;
switch (op) {
case '+':
result = [operand1 add: operand2];
break;
case '-':
result = [operand1 subtract: operand2];
break;
case '*':
result = [operand1 multiply: operand2];
break;
case '/':
result = [operand1 divide: operand2];
break;
}
accumulator.numerator = result.numerator;
accumulator.denominator = result.denominator;
return accumulator;
}
@end
Fraction.h
#import <UIKit/UIKit.h>
@interface Fraction : NSObject
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) reduce;
-(double) convertToNum;
-(NSString *) convertToString;
@end
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) setTo:(int)n over:(int)d
{
numerator = n;
denominator = d;
}
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
{
return (double) numerator/denominator;
}
else
{
return NAN;
}
}
-(NSString *) convertToString
{
if (numerator == denominator)
{
if (numerator == 0)
{
return @"0";
}
else
{
return @"1";
}
}
else if (denominator == 1)
{
return [NSString stringWithFormat: @"%i", numerator];
}
else
{
return [NSString stringWithFormat: @"%i/%i", numerator, denominator];
}
}
// add a fraction to the receiver
-(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];
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
-(Fraction *) subtract:(Fraction *)f
{
// a/b - c/d = ((a*d) - (b*c)) / (b*d)
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator - denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
-(Fraction *) multiply:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
-(Fraction *) divide:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator;
result.denominator = denominator * f.numerator;
[result reduce];
return result;
}
-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;
if (u == 0)
{
return;
}
else if (u < 0)
{
u = -u;
}
while (v != 0)
{
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "Calculator.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *display;
-(void) processDigit: (int) digit;
-(void) processOp: (char) theOp;
-(void) storeFracPart;
// Numeric Keys
-(IBAction) clickDigit: (UIButton *)sender;
//Arithmetic Operation keys
-(IBAction) clickPlus;
-(IBAction) clickMinus;
-(IBAction) clickMultiply;
-(IBAction) clickDivide;
//Misc. Keys
-(IBAction) clickOver;
-(IBAction) clickEquals;
-(IBAction) clickClear;
-(IBAction) clickConvert;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
{
char op;
int currentNumber;
BOOL firstOperand, isNumerator;
Calculator *myCalculator;
NSMutableString *displayString;
}
@synthesize display;
- (void)viewDidLoad
{
firstOperand = YES;
isNumerator = YES;
displayString = [NSMutableString stringWithCapacity: 40];
myCalculator = [[Calculator alloc] init];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) processDigit: (int) digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat: @"%i" , digit]];
display.text = displayString;
}
- (void) clickDigit: (UIButton *) 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 = @" x ";
break;
case '/':
opStr = @" / ";
break;
}
[self storeFracPart];
firstOperand = NO;
isNumerator = YES;
[displayString appendString: opStr];
display.text = displayString;
}
- (void) storeFracPart
{
if (firstOperand) {
if (isNumerator) {
myCalculator.operand1.numerator = currentNumber;
myCalculator.operand1.denominator = 1; // e.g. 3 * 4/5 =
}
else
myCalculator.operand1.denominator = currentNumber;
}
else if (isNumerator) {
myCalculator.operand2.numerator = currentNumber;
myCalculator.operand2.denominator = 1; // e.g. 3/2 * 4 =
}
else {
myCalculator.operand2.denominator = currentNumber;
firstOperand = 0;
}
}
- (IBAction) clickOver
{
[self storeFracPart];
isNumerator = NO;
[displayString appendString: @"/"];
display.text = displayString;
}
// Arithmetic Operation keys
-(IBAction) clickPlus
{
[self processOp:'+'];
}
-(IBAction) clickMinus
{
[self processOp: '-'];
}
-(IBAction) clickMultiply
{
[self processOp: '*'];
}
-(IBAction) clickDivide
{
[self processOp: '/'];
}
-(IBAction) clickEquals
{
if(firstOperand == NO)
{
[self storeFracPart];
[myCalculator performOperation: op];
[displayString appendString:@" = "];
[displayString appendString:[myCalculator.accumulator convertToString]];
display.text = displayString;
currentNumber = 0;
isNumerator = YES;
firstOperand = YES;
[displayString setString: @""];
}
}
-(IBAction) clickClear
{
isNumerator = YES;
firstOperand = YES;
currentNumber = 0;
[myCalculator clear];
[displayString setString: @""];
display.text = displayString;
}
-(IBAction) clickConvert
{
display.text = [NSString stringWithFormat: @"%f", [myCalculator.accumulator convertToNum]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end