Hello, I do not receive any errors beforehand, but when I try to press an operator the app crashes and it takes me to a screen which i have attached below. Help would be greatly appreciated I just can't seem to catch the mistake!
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;
@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];
}
-(void) processDigit: (int) digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat:@"%i", digit]];
display.text = displayString;
}
-(IBAction)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;
}
else {
myCalculator.operand1.denominator = currentNumber;
}
}
else if (isNumerator){
myCalculator.operand2.numerator = currentNumber;
myCalculator.operand2.denominator = 1;
}
else{
myCalculator.operand2.numerator = currentNumber;
firstOperand = YES;
}
currentNumber = 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:'/'];
}
// Misc keys
-(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;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@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
Fraction.m#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;
}
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