Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CalculatorBrain.h
- // Calculator
- //
- // Created by on 7/28/12.
- // Copyright (c) 2012 All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface CalculatorBrain : NSObject
- @property (strong,nonatomic) NSMutableArray* operandStack;
- -(void)setOperandStack:(NSMutableArray *)operandStack;
- -(void) pushOperand:(double) operand;
- -(double)popOperand;
- -(double) performOperation:(NSString*) operation;
- @end
- //
- // CalculatorBrain.m
- // Calculator
- //
- // Created by on 7/28/12.
- // Copyright (c) 2012 . All rights reserved.
- //
- #import "CalculatorBrain.h"
- @implementation CalculatorBrain
- @synthesize operandStack = _operandStack;
- -(NSMutableArray*) operandStack{
- if(self.operandStack == NULL){
- _operandStack = [[NSMutableArray alloc] init];
- }
- return _operandStack;
- }
- -(void)setOperandStack:(NSMutableArray *)operandStack{
- _operandStack = operandStack;
- }
- -(void) pushOperand:(double) operand{
- [_operandStack addObject:[NSNumber numberWithDouble:operand]];
- int count = [_operandStack count];
- NSLog(@"Number pushed is %g", [[_operandStack objectAtIndex:count - 1] doubleValue]);
- }
- -(double)popOperand{
- int count = [_operandStack count];
- NSNumber* value = [_operandStack objectAtIndex:count - 1];
- double val = 0;
- if(value){
- val = [value doubleValue];
- [_operandStack removeLastObject];
- }
- NSLog(@"popped %g",val);
- return val;
- }
- -(double) performOperation:(NSString*) operation{
- double result = 0;
- if([@"+" isEqualToString:operation]){
- result = [self popOperand] + [self popOperand];
- }else if([@"*" isEqualToString:operation]){
- result = [self popOperand] * [self popOperand];
- }
- [self pushOperand:result];
- return result;
- }
- @end
- //
- // ViewController.h
- // Calculator
- //
- // Created by on 7/28/12.
- // Copyright (c) 2012 All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController
- @property (weak, nonatomic) IBOutlet UILabel *display;
- @end
- //
- // ViewController.m
- // Calculator
- //
- // Created by on 7/28/12.
- // Copyright (c) 2012 AJ Gauravdeep. All rights reserved.
- //
- #import "ViewController.h"
- #import "CalculatorBrain.h"
- @interface ViewController ()
- @property (nonatomic) Boolean amIInMiddleOfNumber;
- @property (strong, nonatomic) CalculatorBrain* brain;
- @end
- @implementation ViewController
- @synthesize brain = _brain;
- @synthesize display = _display;
- @synthesize amIInMiddleOfNumber = _amIInMiddleOfNumber;
- -(CalculatorBrain *)Brain{
- if(self.Brain == NULL){
- _brain = [[CalculatorBrain alloc] init];
- }
- return _brain;
- }
- - (IBAction)numberPressed:(UIButton*)sender {
- NSString* digit = [sender currentTitle];
- if(_amIInMiddleOfNumber)
- _display.text = [_display.text stringByAppendingString:digit];
- else{
- _display.text = digit;
- _amIInMiddleOfNumber =YES;
- }
- }
- - (IBAction)enterPressed {
- double mynumber= [self.display.text doubleValue];
- NSLog(@"enter pressed....Number is %g", mynumber);
- [_brain pushOperand:mynumber];
- int count = [_brain.operandStack count];
- NSLog(@"enter pressed... pusehed operand is %g", [[_brain.operandStack objectAtIndex:count - 1] doubleValue]);
- _amIInMiddleOfNumber = NO;
- }
- - (IBAction)operationPressed:(UIButton *)sender {
- NSLog(@"Operation pressed is '%@'", sender.currentTitle);
- if(_amIInMiddleOfNumber)
- [self enterPressed];
- double result = [_brain performOperation:sender.currentTitle];
- self.display.text = [NSString stringWithFormat:@"%g",result];
- }
- @end
Add Comment
Please, Sign In to add comment