首页 > 代码库 > [ios]
[ios]
说明:
前面的RPN计算器是按照课程做的,是后缀表达式的计算。现在这个计算器是中缀表达式的计算,而且把计算过程也显示在屏幕上,是一般的计算器。
设计方法:
在Model里用了两个栈,一个是数字栈,一个是操作符栈,如果压入数字的话,检查操作符栈的最顶端元素是不是乘号和除号,如果是乘号和除号则出栈计算计算结果,最终,在按下“等号键”计算结果时,操作符栈中只有加号和减号。
Final “View” as follows:
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==0) { _numberStack = [[NSMutableArray alloc]init]; } return _numberStack;}-(NSMutableArray*)operationStack{ if(_operationStack==0){ _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 = 0; 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==0){ self.display.text = @""; self.IsNotFirstPressed = 1; } 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 = 1; } }- (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 = 0; }- (IBAction)ClearPressed { [self.brain clearAll]; self.userIsInTheMiddleOfEnteringANumber = 0; self.IsNotFirstPressed = 0; self.display.text = @"0";}- (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 = 0;}@end
[ios]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。