清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
-(void)viewDidLoad { //初始化字符串 self.num1 = [[NSMutableString alloc]init]; self.num2 = [[NSMutableString alloc]init]; self.tempStr = [[NSMutableString alloc]init]; } //监听数字按钮0-9和点,点的tag为10 //判断输入数字前有几个0,只取一个零,小数点只取一个 -(IBAction)numbers:(id)sender { //处理0的问题 //以零开头,下次输入的非0,则清空0 //如果以0.开头,则不清空 if ([self.tempStr hasPrefix:@"0"] && [sender tag] > 0 && [sender tag] <10 && ![self.tempStr hasPrefix:@"0."]) { self.tempStr = [NSMutableString stringWithString:@""]; } else if ([self.tempStr hasPrefix:@"0"] && [sender tag] == 0 && ![self.tempStr hasPrefix:@"0."]) { //如果是以0开头,但是不是以0.开头,则直接返回 return; } //处理小数点的问题 //如果小数点是第一输入的数字 if ([sender tag] == 10 && self.tempStr.length == 0) { self.tempStr = [NSMutableString stringWithString:@"0"]; } //每输入一次,拼接一次字符串 if([sender tag] == 10 )//取小数点 { //小数点只允许输入一次 //遍历字符串tempStr,如果有小数点,则直接return for (int i = 0; i < self.tempStr.length ; i++) { char c = [self.tempStr characterAtIndex:i]; if (c == '.') { return; } } [self.tempStr appendString:@"."]; } else { [self.tempStr appendFormat:@"%ld",[sender tag]]; } _show.text = self.tempStr; } //删除按钮 -(IBAction)deleteNums:(id)sender { if (_show.text.length == 1)//删除显示屏中所有的数字 { _show.text = @"0"; //清空tempStr self.tempStr = [NSMutableString stringWithString:@""]; return; } //将显示屏中的现有数字赋值给字符串 // NSMutableString *str = [[NSMutableString alloc]init]; self.tempStr =[NSMutableString stringWithFormat:@"%@", _show.text] ; [self.tempStr deleteCharactersInRange:NSMakeRange(self.tempStr.length-1, 1)]; _show.text = self.tempStr; } //监听加减乘除 calculate "=" 11 "+" 12 "-" 13 "*" 14 "/" 15 正负为16 %为17 -(IBAction)calculate:(id)sender { //清空tempStr self.tempStr = [NSMutableString stringWithString:@""]; if(0 == _count)//如果是第一次输入 { _cal = [NSNumber numberWithLong:[sender tag]]; self.num1 = [NSMutableString stringWithFormat:@"%@",_show.text]; } else//不是第一次输入,则计算 { self.num2 = [NSMutableString stringWithFormat:@"%@", _show.text]; int calculate = [_cal intValue]; switch (calculate) { case 12://将加后的结果显示 _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] + [self.num2 intValue])]; break; case 13://将减后的结果显示 _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] - [self.num2 intValue])]; break; case 14://将乘后的结果显示 _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] * [self.num2 intValue])]; break; case 15://将除后的结果显示 _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] / [self.num2 intValue])]; break; default: break; } self.num1 = [NSMutableString stringWithFormat:@"%@", _show.text]; _cal = [NSNumber numberWithLong:[sender tag]]; } _count = @1; if ([sender tag] == 16 || [sender tag] == 17)//单目运算正负和% { switch ([sender tag]) { case 16://将乘负后的结果显示 _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] *(-1))]; break; case 17://将取百分后的结果显示 _show.text =[NSString stringWithFormat:@"%.2f",([self.num1 doubleValue] / 100)]; break; default: break; } self.num1 = [NSMutableString stringWithFormat:@"%@", _show.text]; } }