Stanford CS193P Initial Calculator Code
So I completed the Calculator Code from Stanford CS193P Lecture 2, and got my Calculator to work correctly, only to accidentally delete it from my computer. I then had to re-write the whole code. I also found the RPN Calculator to be a little confusing, and had a hard time getting to work properly at times.
I am therefore posting the initial code for it here. And I’ll post the add-ons as I go through CS193P Assignment 1.
So here is the code…
CalculatorViewController.h
#import <UIKit/UIKit.h> @interface CalculatorViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *display; @end
CalculatorViewController.m
#import "CalculatorViewController.h" #import "CalculatorBrain.h" @interface CalculatorViewController() @property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber; @property (nonatomic, strong) CalculatorBrain *brain; @end @implementation CalculatorViewController @synthesize display = _display; @synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber; @synthesize brain = _brain; - (CalculatorBrain *) brain { if(!_brain) _brain = [[CalculatorBrain alloc] init]; return _brain; } - (IBAction)digitPressed:(UIButton *)sender { NSString *digit = [sender currentTitle]; if(self.userIsInTheMiddleOfEnteringANumber) { self.display.text = [self.display.text stringByAppendingString:digit]; } else { self.display.text = digit; self.userIsInTheMiddleOfEnteringANumber = YES; } } - (IBAction)enterPressed { [self.brain pushOperand:[self.display.text doubleValue]]; self.userIsInTheMiddleOfEnteringANumber = NO; } - (IBAction)operationPressed:(UIButton *)sender { if(self.userIsInTheMiddleOfEnteringANumber) [self enterPressed]; double result = [self.brain performOperation:sender.currentTitle]; NSString *resultString = [NSString stringWithFormat:@"%g", result]; self.display.text = resultString; } @end
CalculatorBrain.h
#import <Foundation/Foundation.h> @interface CalculatorBrain : NSObject - (void)pushOperand:(double)operand; - (double)performOperation:(NSString *)operation; @end
CalculatorBrain.m
#import "CalculatorBrain.h" @interface CalculatorBrain() @property (nonatomic, strong) NSMutableArray *operandStack; @end @implementation CalculatorBrain @synthesize operandStack = _operandStack; - (NSMutableArray *)operandStack { if(_operandStack == nil) _operandStack = [[NSMutableArray alloc] init]; return _operandStack; } - (void)pushOperand:(double)operand { [self.operandStack addObject:[NSNumber numberWithDouble:operand]]; } - (double) popOperand { NSNumber *operandObject = [self.operandStack lastObject]; if(operandObject != nil) [self.operandStack removeLastObject]; return [operandObject doubleValue]; } - (double)performOperation:(NSString *)operation { double result = 0; if([operation isEqualToString:@"+"]) { result = [self popOperand] + [self popOperand]; } else if([@"*" isEqualToString:operation]) { result = [self popOperand] * [self popOperand]; } else if ([operation isEqualToString:@"-"]) { double subtrahend = [self popOperand]; result = [self popOperand] - subtrahend; } else if ([operation isEqualToString:@"/"]) { double divisor = [self popOperand]; result = [self popOperand] / divisor; } [self pushOperand:result]; return result; } @end