- (IBAction)calAction:(UIButton *)sender {
self.number1 = [self.resultLabel.textfloatValue];
self.operator = sender.tag;
self.resultLabel.text = @"0";
}
- (IBAction)clear:(id)sender {
self.resultLabel.text = @"0";
// self.resultLabel.font = [UIFont fontWithName:@"Zapfino" size:70];
self.resultLabel.font = [UIFontsystemFontOfSize:70];
}
- (IBAction)resultAction:(id)sender {
float number2 = [self.resultLabel.text floatValue];
float result = 0;
switch (self.operator) {
case 0://+
result = self.number1 + number2;
break;
case 1://-
result = self.number1 - number2;
break;
case 2://*
result = self.number1 * number2;
break;
case 3:///
result = self.number1 / number2;
break;
}
// 55+3*2-3/1
//控制小数点的位数
self.resultLabel.text = [NSString stringWithFormat:@"%.2f",result];
}
- (IBAction)number:(UIButton *)sender {
//把数字取出来 让label去显示
// 取出按钮的title
NSString *title = [sender titleForState:UIControlStateNormal];
//判断字符串是否以***开头或结尾
// 判断如果是以0开头 并且当前点击的按钮是数字不是小数点需要把0去掉
if ( self.resultLabel.text.length==1 &&![title isEqualToString:@"."]&& [self.resultLabel.text hasPrefix:@"0"]) {
self.resultLabel.text = @"";
}
//避免连续输入小数点
if ([title isEqualToString:@"."]) {
//判断是否包含某个字符串
if ([self.resultLabel.text rangeOfString:@"."].length>0) {
//如果包含了. 就不能再点 . 了
return;
}
}
// 把title显示到lable中
self.resultLabel.text = [self.resultLabel.textstringByAppendingString:title];
//如果输入的内容过多则把字体变小把行数变成无数行
if (self.resultLabel.text.length>=8) {
self.resultLabel.font = [UIFont systemFontOfSize:30];
//
self.resultLabel.numberOfLines = 0;
}
}