总结计算器demo
用到的方法及思路
- 使用MVC设计模式,具体可以看:iOS——MVC设计模式
- 使用第三方库Masonry进行布局,首先我们必须先配置CocoaPods具体可以看iOS安装CocoaPods详细过程,安装好之后我们就可以运用Masonry进行布局了,可以看:iOS——Masonry的简单使用
- 使用的算法——栈,关于栈的内容具体可以看:栈
关于计算器我们的思路是,将所有会用到的方法在Model中进行定义以及实现,然后在View中定义Button,然后使用Masonry进行布局Button,在Controller初始化Model和View对象然后对View中的按钮添加点击事件,然后调用Model中的方法来实现对运算式的求值。
用法及核心代码
首先在View里我们建立Button,按理说我们可以看见
除了0的尺寸与其他按钮不一样,所以我们可以利用循环来创建前四行的按钮,然后再将“0”,“.”,“=”三个按钮单独创建,然后每一个Button加一个tag值,后续我们在VC中通过点击的Button的tag值可以分辨出点击的哪个Button。但是由于刚开始写没有注意到可以循环创建,导致代码相比之下比较的繁琐。
部分创建Button的代码:
_addButton = [[UIButton alloc] init];
[self addSubview:_addButton];
_subtractButton = [[UIButton alloc] init];
[self addSubview:_subtractButton];
_multiplyButton = [[UIButton alloc] init];
[self addSubview:_multiplyButton];
_divisionButton = [[UIButton alloc] init];
[self addSubview:_divisionButton];
- (void)layoutSubviews {
_zeroButton.tag = 0;
_zeroButton.titleLabel.textAlignment = NSTextAlignmentLeft;
[_zeroButton setTitle:@"0" forState:UIControlStateNormal];
[_zeroButton setTintColor:[UIColor whiteColor]];
_zeroButton.backgroundColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f];
_zeroButton.layer.cornerRadius = 35;
_zeroButton.titleLabel.font = [UIFont systemFontOfSize:40];
_oneButton.tag = 1;
[_oneButton setTitle:@"1" forState:UIControlStateNormal];
[_oneButton setTintColor:[UIColor whiteColor]];
_oneButton.backgroundColor = [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f];
_oneButton.layer.cornerRadius = 35;
_oneButton.titleLabel.font = [UIFont systemFontOfSize:40];
}
在Model里。我们先定义两个NSMutableArray类型的数组。
@property NSMutableArray *tempArray;
@property NSMutableArray *allArray;
然后我们将定义的方法进行实现:
- (void)PushInto:(NSString *)str {
if (([str isEqualToString:@"("]) || ([str isEqualToString:@")"])) {
return;
}
if ([str isEqualToString:@"nil"]) {
[_allArray addObject:_tempArray[[_tempArray count] - 1]];
[_tempArray removeObjectAtIndex:([_tempArray count] - 1)];
} else {
[_allArray addObject:str];
}
}
优先级比较:
将字符串转化为数字:
然后在Controller中,将Model和View定义成属性值:
@property(nonatomic , strong)CalculatorModel *calculatorModel;
@property(nonatomic , strong)CalculatorView *calculatorView;
将对象初始化:
_calculatorView = [[CalculatorView alloc] init];
[self.view addSubview:_calculatorView];
[_calculatorView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view);
make.left.equalTo(self.view);
make.width.equalTo(self.view);
make.bottom.equalTo(self.view);
}];
对运算符的操作:
if ((btn.tag >= 0) && (btn.tag <= 10)) {
NSString *str = btn.titleLabel.text;
_calculatorView.numbleString = [_calculatorView.numbleString stringByAppendingString:str];
if ([btn.titleLabel.text isEqualToString:@"."]) {
_count++;
}
}
if ((btn.tag >= 11) && (btn.tag < 18)) {
if ([_calculatorView.numbleString length] != 0) {
if (([_calculatorView.numbleString characterAtIndex:[_calculatorView.numbleString length] - 1] == '.') || ([_calculatorView.numbleString characterAtIndex:0] == '.') || (_count > 1)) {
_count = 0;
[self error];
} else {
[_calculatorModel PushInto:_calculatorView.numbleString];
_calculatorView.numbleString = @"";
_count = 0;
}
}
if ([_calculatorModel.tempArray count] == 0) {
if ([btn.titleLabel.text isEqualToString:@")"]) {
[self error];
} else {
[_calculatorModel PushIntoSymol:btn.titleLabel.text];
}
} else {
if ([_calculatorModel Compare:btn.titleLabel.text] == 0) {
if ([btn.titleLabel.text isEqualToString:@"("]) {
[_calculatorModel PushIntoSymol:btn.titleLabel.text];
} else {
[_calculatorModel PushInto:@"nil"];
[_calculatorModel PushIntoSymol:btn.titleLabel.text];
}
} else {
if ([btn.titleLabel.text isEqualToString:@")"]) {
for (int i = (int)[_calculatorModel.tempArray count] - 1; ; i--){
if ([_calculatorModel.tempArray[i] isEqualToString:@"("]) {
while (1) {
if ([_calculatorModel.tempArray count] == i + 1) {
break;
}
[_calculatorModel.allArray addObject:_calculatorModel.tempArray[i + 1]];
[_calculatorModel.tempArray removeObjectAtIndex:i + 1];
}
[_calculatorModel.tempArray removeObjectAtIndex:i];
break;
}
if (i == 0) {
[self error];
break;
}
}
} else {
[_calculatorModel PushIntoSymol:btn.titleLabel.text];
}
}
}
}
通过Button的tag值同时我们需要判断是否存在错误的情况,比如括号不匹配,首位为符号。
同时我们都知道分母不能为0所以我们还必须还要判断如果执行除法,那么我们需要判断是不是分母为0 :
if ([_calculatorModel.allArray[i] isEqualToString:@"/"]) {
if (number2 == 0) {
[self error];
flag = 1;
break;
}
_calculatorModel.allArray[i] = [NSString stringWithFormat: @"%f", number1 / number2];
}
然后排除特殊情况之后就可以进行运算了: