iOS实现简易的计算器
更新时间:2022年01月27日 14:50:58 作者:XYQ全哥
这篇文章主要为大家详细介绍了iOS实现简易的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
本文实例为大家分享了iOS实现简易的计算器的具体代码,供大家参考,具体内容如下
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉。
// // ?ViewController.m // ?计算器 // // ?Created by ma c on 15/8/25. // ?Copyright (c) 2015年 bjsxt. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *ResultField; @property(nonatomic,assign)CGFloat temp; @property(nonatomic,assign)CGFloat num1; @property(nonatomic,assign)CGFloat num2; @property(nonatomic,strong)NSMutableString *string; @property(nonatomic,strong)NSArray *arr; @end @implementation ViewController - (IBAction)buttonClear:(UIButton *)sender { ? ? [_string setString:@""]; ? ? ? ? //重新开始计算,文本框置空 ? ? self.ResultField.text = _string; } - (IBAction)button7:(UIButton *)sender { ? ? [_string appendString:@"7"]; ? ? self.ResultField.text = _string; } - (IBAction)button8:(UIButton *)sender { ? ? [_string appendString:@"8"]; ? ? self.ResultField.text = _string; } - (IBAction)button9:(UIButton *)sender { ? ? [_string appendString:@"9"]; ? ? self.ResultField.text = _string; } - (IBAction)button4:(UIButton *)sender { ? ? [_string appendString:@"4"]; ? ? self.ResultField.text = _string; } - (IBAction)button5:(UIButton *)sender { ? ? [_string appendString:@"5"]; ? ? self.ResultField.text = _string; } - (IBAction)button6:(UIButton *)sender { ? ? [_string appendString:@"6"]; ? ? self.ResultField.text = _string; } - (IBAction)button1:(UIButton *)sender { ? ? [_string appendString:@"1"]; ? ? self.ResultField.text = _string; } - (IBAction)button3:(UIButton *)sender { ? ? [_string appendString:@"3"]; ? ? self.ResultField.text = _string; } - (IBAction)button2:(UIButton *)sender { ? ? [_string appendString:@"2"]; ? ? self.ResultField.text = _string; } - (IBAction)button0:(UIButton *)sender { ? ? [_string appendString:@"0"]; ? ? self.ResultField.text = _string; } - (IBAction)buttonPoint:(UIButton *)sender { ? ? [_string appendString:@"."]; ? ? self.ResultField.text = _string; } //触发算数运算事件 - (IBAction)buttonDiv:(UIButton *)sender { ? ? [_string appendString:@"/"]; ? ? self.ResultField.text = _string; } - (IBAction)buttonMul:(UIButton *)sender { ? ? [_string appendString:@"*"]; ? ? self.ResultField.text = _string; } - (IBAction)buttonSub:(UIButton *)sender { ? ? [_string appendString:@"-"]; ? ? self.ResultField.text = _string; } - (IBAction)buttonAdd:(UIButton *)sender { ? ? [_string appendString:@"+"]; ? ? self.ResultField.text = _string; } //做结果运算操作 - (IBAction)buttonEqual:(UIButton *)sender { ? ? for(int i=0; i<[_string length]; i++) ? ? { ? ? ? ? self.arr = [[NSArray alloc]init]; ? ? ? ? //只输入一个数,不做运算 ? ? ? ? if([_string length] == 1) ? ? ? ? { ? ? ? ? ? ? self.temp = [_string doubleValue]; ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? //做加法运算 ? ? ? ? if([_string characterAtIndex:i] == '+') ? ? ? ? { ? ? ? ? ? ? self.arr = [_string componentsSeparatedByString:@"+"]; ? ? ? ? ? ? self.num1 = [self.arr[0] doubleValue]; ? ? ? ? ? ? self.num2 = [self.arr[1] doubleValue]; ? ? ? ? ? ? self.temp = self.num1 + self.num2; ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? //做减法运算 ? ? ? ? if([_string characterAtIndex:(i+1)] == '-') ? ? ? ? { ? ? ? ? ? ? self.arr = [_string componentsSeparatedByString:@"-"]; ? ? ? ? ? ? if([self.arr count] == 2) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? self.num1 = [self.arr[0] doubleValue]; ? ? ? ? ? ? ? ? self.num2 = [self.arr[1] doubleValue]; ? ? ? ? ? ? ? ? self.temp = self.num1 - self.num2; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? self.num1 = [self.arr[1] doubleValue]; ? ? ? ? ? ? ? ? self.num2 = [self.arr[2] doubleValue]; ? ? ? ? ? ? ? ? self.temp = -(self.num1 + self.num2); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? //做除法运算 ? ? ? ? if([_string characterAtIndex:i] == '/') ? ? ? ? { ? ? ? ? ? ? self.arr = [_string componentsSeparatedByString:@"/"]; ? ? ? ? ? ? self.num1 = [self.arr[0] doubleValue]; ? ? ? ? ? ? self.num2 = [self.arr[1] doubleValue]; ? ? ? ? ? ? self.temp = self.num1 / self.num2; ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? //做乘法运算 ? ? ? ? if([_string characterAtIndex:i] == '*') ? ? ? ? { ? ? ? ? ? ? self.arr = [_string componentsSeparatedByString:@"*"]; ? ? ? ? ? ? self.num1 = [self.arr[0] doubleValue]; ? ? ? ? ? ? self.num2 = [self.arr[1] doubleValue]; ? ? ? ? ? ? self.temp = self.num1 * self.num2; ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? //输出结果 ? ? [_string setString:[NSString stringWithFormat:@"%.2f",self.temp]]; ? ? self.ResultField.text = _string; } - (void)viewDidLoad { ? ? [super viewDidLoad]; ? ? //创建一个可变的字符串 ? ? _string = [NSMutableString stringWithCapacity:20]; } - (void)didReceiveMemoryWarning { ? ? [super didReceiveMemoryWarning]; ? ? // Dispose of any resources that can be recreated. } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。
相关文章
iOS封装倒计时按钮HLCountDownButton示例详解
这篇文章主要为大家介绍了iOS封装倒计时按钮HLCountDownButton示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-07-07iOS App开发中通过UIDevice类获取设备信息的方法
UIDevice最常见的用法就是用来监测iOS设备的电量了,然后再实现电池状态通知非常方便,除此之外还有传感器等信息的获取,这里我们就来总结一下iOS App开发中通过UIDevice类获取设备信息的方法:2016-07-07详解iOS开发中解析JSON中的boolean类型的数据遇到的问题
这篇文章主要介绍了详解iOS开发中解析JSON中的boolean类型的数据遇到的问题,具有一定的参考价值,有兴趣的可以了解一下。2016-12-12
最新评论