first ios app
calculatorViewController.m
1 #import "CalculatorViewController.h" 2 #import "CalculatorBrain.h" 3 4 @interface CalculatorViewController ()//create private varible 5 @property (nonatomic) BOOL useIsInTheMiddleOfEnteringANumber; 6 7 @property (nonatomic, strong) CalculatorBrain *brain; 8 @end 9 10 @implementation CalculatorViewController 11 12 @synthesize display = _display; 13 @synthesize useIsInTheMiddleOfEnteringANumber = _useIsInTheMiddleOfEnteringANumber; 14 @synthesize brain = _brain; 15 16 - (CalculatorBrain *)brain //setter 17 { 18 if(!_brain) _brain = [[CalculatorBrain alloc] init]; 19 return _brain; 20 } 21 22 23 - (IBAction)digitPressed:(UIButton *)sender 24 { 25 NSString *digit = sender.currentTitle; 26 if(self.useIsInTheMiddleOfEnteringANumber) { 27 self.display.text = [self.display.text stringByAppendingString:digit]; 28 } else { 29 self.display.text = digit; 30 self.useIsInTheMiddleOfEnteringANumber = YES; 31 } 32 33 34 /* 35 UILabel *myDisplay = self.display; //[self display]; getter 36 NSString *CurrentText = [myDisplay text]; 37 NSString *newText = [CurrentText stringByAppendingString:digit]; 38 myDisplay.text = newText;//[myDisplay setText:newText]; setter 39 */ 40 41 42 43 } 44 45 - (IBAction)doublePressed:(UIButton *)sender { 46 47 double result = [self.brain performOperation:sender.currentTitle]; 48 NSString *resultString = [NSString stringWithFormat:@"%g", result]; 49 self.display.text = resultString; 50 } 51 52 - (IBAction)operationPressed:(UIButton *)sender 53 { 54 55 if(self.useIsInTheMiddleOfEnteringANumber) [self enterPressed]; 56 double result = [self.brain performOperation:sender.currentTitle]; 57 NSString *resultString = [NSString stringWithFormat:@"%g", result]; 58 self.display.text = resultString; 59 60 } 61 62 - (IBAction)enterPressed 63 { 64 [self.brain pushOperand:[self.display.text doubleValue]]; 65 self.useIsInTheMiddleOfEnteringANumber = NO; 66 } 67 68 69 @end
viewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface CalculatorViewController : UIViewController 4 5 @property (weak, nonatomic) IBOutlet UILabel *display; 6 7 @end
Brain.h
1 #import <Foundation/Foundation.h> 2 3 @interface CalculatorBrain : NSObject// model 4 5 6 - (void)pushOperand: (double)operand; 7 8 - (double) performOperation:(NSString *)operation; 9 10 @end
Brain.m
1 #import "CalculatorBrain.h" 2 @interface CalculatorBrain() 3 @property (nonatomic, strong) NSMutableArray *operandStack; 4 @end 5 6 7 @implementation CalculatorBrain 8 9 10 @synthesize operandStack = _operandStack; 11 12 - (NSMutableArray *)operandStack//getter 13 { 14 if(_operandStack == nil) _operandStack = [[NSMutableArray alloc] init]; 15 return _operandStack; 16 } 17 18 19 - (void)pushOperand: (double)operand 20 { 21 22 [self.operandStack addObject:[NSNumber numberWithDouble:operand]];//must add double 23 24 } 25 26 - (double) popOperand 27 { 28 NSNumber *operandObject = [self.operandStack lastObject]; 29 if(operandObject != nil) [self.operandStack removeLastObject]; 30 return [operandObject doubleValue]; 31 } 32 33 - (double) performOperation:(NSString *)operation 34 { 35 double result = 0; 36 if([operation isEqualToString:@"+"]) { 37 result = [self popOperand] + [self popOperand]; 38 } else if ([@"*" isEqualToString:operation]) { 39 result = [self popOperand] * [self popOperand]; 40 } else if ([@"^" isEqualToString:operation]) { 41 double d = [self popOperand]; 42 result = d * d; 43 } 44 [self pushOperand:result]; 45 return result; 46 } 47 48 @end
screen