Guest User

Untitled

a guest
Sep 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CalculatorBrain.h
  3. //  Calculator
  4. //
  5. //  Created by   on 7/28/12.
  6. //  Copyright (c) 2012   All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface CalculatorBrain : NSObject
  12.  
  13. @property (strong,nonatomic) NSMutableArray* operandStack;
  14.  
  15. -(void)setOperandStack:(NSMutableArray *)operandStack;
  16. -(void) pushOperand:(double) operand;
  17. -(double)popOperand;
  18. -(double) performOperation:(NSString*) operation;
  19.  
  20.  
  21. @end
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. //
  31. //  CalculatorBrain.m
  32. //  Calculator
  33. //
  34. //  Created by  on 7/28/12.
  35. //  Copyright (c) 2012 . All rights reserved.
  36. //
  37.  
  38. #import "CalculatorBrain.h"
  39.  
  40. @implementation CalculatorBrain
  41.  
  42. @synthesize operandStack = _operandStack;
  43.  
  44. -(NSMutableArray*) operandStack{
  45.     if(self.operandStack == NULL){
  46.         _operandStack = [[NSMutableArray alloc] init];
  47.     }
  48.     return _operandStack;
  49. }
  50.  
  51. -(void)setOperandStack:(NSMutableArray *)operandStack{
  52.     _operandStack = operandStack;
  53. }
  54.  
  55. -(void) pushOperand:(double) operand{
  56.     [_operandStack addObject:[NSNumber numberWithDouble:operand]];
  57.     int count = [_operandStack count];
  58.     NSLog(@"Number pushed is %g", [[_operandStack objectAtIndex:count - 1] doubleValue]);
  59. }
  60.  
  61. -(double)popOperand{
  62.     int count = [_operandStack count];
  63.     NSNumber* value = [_operandStack objectAtIndex:count - 1];
  64.     double val = 0;
  65.     if(value){
  66.         val = [value doubleValue];
  67.         [_operandStack removeLastObject];
  68.     }
  69.     NSLog(@"popped %g",val);
  70.     return val;
  71. }
  72.  
  73. -(double) performOperation:(NSString*) operation{
  74.     double result = 0;
  75.     if([@"+" isEqualToString:operation]){
  76.         result = [self popOperand] + [self popOperand];
  77.     }else if([@"*" isEqualToString:operation]){
  78.         result = [self popOperand] * [self popOperand];
  79.     }
  80.     [self pushOperand:result];
  81.     return result;
  82. }
  83.  
  84. @end
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. //
  94. //  ViewController.h
  95. //  Calculator
  96. //
  97. //  Created by   on 7/28/12.
  98. //  Copyright (c) 2012   All rights reserved.
  99. //
  100.  
  101. #import <UIKit/UIKit.h>
  102.  
  103. @interface ViewController : UIViewController
  104.  
  105. @property (weak, nonatomic) IBOutlet UILabel *display;
  106.  
  107. @end
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. //
  120. //  ViewController.m
  121. //  Calculator
  122. //
  123. //  Created by   on 7/28/12.
  124. //  Copyright (c) 2012 AJ Gauravdeep. All rights reserved.
  125. //
  126.  
  127. #import "ViewController.h"
  128. #import "CalculatorBrain.h"
  129.  
  130. @interface ViewController ()
  131.  
  132. @property (nonatomic) Boolean amIInMiddleOfNumber;
  133. @property (strong, nonatomic) CalculatorBrain* brain;
  134.  
  135. @end
  136.  
  137. @implementation ViewController
  138.  
  139. @synthesize brain = _brain;
  140. @synthesize display = _display;
  141. @synthesize amIInMiddleOfNumber = _amIInMiddleOfNumber;
  142.  
  143. -(CalculatorBrain *)Brain{
  144.     if(self.Brain == NULL){
  145.         _brain = [[CalculatorBrain alloc] init];
  146.     }
  147.     return _brain;
  148. }
  149.  
  150. - (IBAction)numberPressed:(UIButton*)sender {
  151.     NSString* digit = [sender currentTitle];
  152.     if(_amIInMiddleOfNumber)
  153.         _display.text = [_display.text stringByAppendingString:digit];
  154.     else{
  155.         _display.text = digit;
  156.         _amIInMiddleOfNumber =YES;
  157.     }
  158. }
  159.  
  160. - (IBAction)enterPressed {
  161.     double mynumber= [self.display.text doubleValue];
  162.     NSLog(@"enter pressed....Number is %g", mynumber);
  163.     [_brain pushOperand:mynumber];
  164.     int count = [_brain.operandStack count];
  165.     NSLog(@"enter pressed... pusehed operand is %g", [[_brain.operandStack objectAtIndex:count - 1] doubleValue]);
  166.     _amIInMiddleOfNumber = NO;
  167. }
  168.  
  169. - (IBAction)operationPressed:(UIButton *)sender {
  170.     NSLog(@"Operation pressed is '%@'", sender.currentTitle);
  171.     if(_amIInMiddleOfNumber)
  172.         [self enterPressed];
  173.     double result = [_brain performOperation:sender.currentTitle];
  174.     self.display.text = [NSString stringWithFormat:@"%g",result];
  175. }
  176.  
  177.  
  178. @end
Add Comment
Please, Sign In to add comment