说明:
前面的RPN计算器是按照stanford课程做的,是后缀表达式的计算。现在这个计算器是自己做的。这个是一般的计算器,即中缀表达式的计算,而且把计算过程也显示在屏幕上,
设计方法:
在Model里用了两个栈,一个是数字栈,一个是操作符栈。如果压入数字的话,检查操作符栈的最顶端元素是不是乘号和除号,如果是乘号和除号则出栈计算乘除结果;如果操作符栈栈顶是加减号,直接把数字压入数字栈。最终,在按下“等号键”计算结果时,操作符栈中只有加号和减号,此时对数字栈和操作符栈依次出栈计算最终结果。
Final “View” :
the codes of the “Model”:
// OrdinaryCalculatorBrain.h
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import <UIKit/UIKit.h> @interface OrdinaryCalculatorBrain : NSObject
-(void)pushOperation:(NSString *)operation; // + - * /
-(void)pushOperand:(double)operand;
-(double)equal;
-(void)clearAll;
@end // OrdinaryCalculatorBrain.m
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import "OrdinaryCalculatorBrain.h"
@interface OrdinaryCalculatorBrain()
@property (nonatomic,strong)NSMutableArray *numberStack;
@property(nonatomic,strong)NSMutableArray *operationStack;
@end @implementation OrdinaryCalculatorBrain
@synthesize numberStack = _numberStack;
@synthesize operationStack = _operationStack; -(NSMutableArray*)numberStack{
if (_numberStack==) {
_numberStack = [[NSMutableArray alloc]init];
}
return _numberStack;
}
-(NSMutableArray*)operationStack{
if(_operationStack==){
_operationStack = [[NSMutableArray alloc]init];
}
return _operationStack;
}
-(void)pushOperation:(NSString *)operation{ // + - * /
[self.operationStack addObject:operation]; }
-(void)pushOperand:(double)operand{
NSString *lastOperation = [self.operationStack lastObject];
if (lastOperation==nil || [lastOperation isEqualToString:@"+" ]||[lastOperation isEqualToString:@"-"]){
[self.numberStack addObject:[NSNumber numberWithDouble:operand]];
}
else if([lastOperation isEqualToString:@"*" ]||[lastOperation isEqualToString:@"/"]){
[self.operationStack removeLastObject];
double num2 = operand;
NSNumber *lastNumber = [self.numberStack lastObject];
if(lastNumber)[self.numberStack removeLastObject];
double num1 = [lastNumber doubleValue];
if ([lastOperation isEqualToString:@"*" ]) {
[self.numberStack addObject:[NSNumber numberWithDouble:num1*num2]];
}else if([lastOperation isEqualToString:@"/" ]){
[self.numberStack addObject:[NSNumber numberWithDouble:num1/num2]];
}
}
}
-(double)equal{
double result = ;
while ([self.operationStack lastObject]!=nil) {
NSString *lastOperation = [self.operationStack lastObject];
if (lastOperation) [self.operationStack removeLastObject];
double num1 = result;
NSNumber *lastNumber = [self.numberStack lastObject];
if(lastNumber)[self.numberStack removeLastObject];
double num2 = [lastNumber doubleValue];
if ([lastOperation isEqualToString:@"+" ]) {
result = num1+num2;
}else if([lastOperation isEqualToString:@"-" ]){
result = num1-num2;
}
}
NSNumber *lastNumber = [self.numberStack lastObject];
if(lastNumber)[self.numberStack removeLastObject];
double num2 = [lastNumber doubleValue];
result += num2;
return result; }
-(void)clearAll{
if (self.numberStack) {
[self.numberStack removeAllObjects];
}
if(self.operationStack){
[self.operationStack removeAllObjects];
}
}
@end
the codes of the “controller”:
// CalculatorViewController.h
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import <UIKit/UIKit.h> @interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display; @end // CalculatorViewController.m
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import "CalculatorViewController.h"
#import "OrdinaryCalculatorBrain.h"
@interface CalculatorViewController ()
@property (nonatomic,strong)OrdinaryCalculatorBrain *brain;
@property(nonatomic)BOOL userIsInTheMiddleOfEnteringANumber;
@property(nonatomic)NSString *currentDigit;
@property(nonatomic)BOOL IsNotFirstPressed;
@end @implementation CalculatorViewController
@synthesize display = _display;
@synthesize brain = _brain;
@synthesize userIsInTheMiddleOfEnteringANumber=_userIsInTheMiddleOfEnteringANumber;
@synthesize IsNotFirstPressed=_IsNotFirstPressed;
-(OrdinaryCalculatorBrain*)brain{
if(_brain==nil)
_brain = [[OrdinaryCalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitPressed:(UIButton *)sender {
if(self.IsNotFirstPressed==){
self.display.text = @"";
self.IsNotFirstPressed = ;
} NSString *digit = sender.currentTitle;
self.display.text = [self.display.text stringByAppendingString:digit];
if (self.userIsInTheMiddleOfEnteringANumber) {
self.currentDigit = [self.currentDigit stringByAppendingString:digit];
}else{
self.currentDigit = digit;
self.userIsInTheMiddleOfEnteringANumber = ;
} } - (IBAction)operationPressed:(UIButton *)sender {
[self.brain pushOperand:[self.currentDigit doubleValue]];
self.currentDigit = @"";
self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
[self.brain pushOperation:sender.currentTitle];
self.userIsInTheMiddleOfEnteringANumber = ; }
- (IBAction)ClearPressed {
[self.brain clearAll];
self.userIsInTheMiddleOfEnteringANumber = ;
self.IsNotFirstPressed = ;
self.display.text = @"";
} - (IBAction)equalPressed:(UIButton *)sender {
[self.brain pushOperand:[self.currentDigit doubleValue]];
self.currentDigit = @"";
double result = [self.brain equal];
self.display.text = [[self.display.text stringByAppendingString:@"="]stringByAppendingString:[NSString stringWithFormat:@"%g",result]];
self.userIsInTheMiddleOfEnteringANumber = ;
} @end