博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS:制作一个简易的计算器
阅读量:5242 次
发布时间:2019-06-14

本文共 5403 字,大约阅读时间需要 18 分钟。

   初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉。

1 //  2 //  ViewController.m  3 //  计算器  4 //  5 //  Created by ma c on 15/8/25.  6 //  Copyright (c) 2015年 bjsxt. All rights reserved.  7 //  8   9 #import "ViewController.h" 10  11 @interface ViewController () 12 @property (weak, nonatomic) IBOutlet UITextField *ResultField; 13 @property(nonatomic,assign)CGFloat temp; 14 @property(nonatomic,assign)CGFloat num1; 15 @property(nonatomic,assign)CGFloat num2; 16 @property(nonatomic,strong)NSMutableString *string; 17 @property(nonatomic,strong)NSArray *arr; 18 @end 19 @implementation ViewController 20 - (IBAction)buttonClear:(UIButton *)sender 21 { 22     [_string setString:@""];         //重新开始计算,文本框置空 23     self.ResultField.text = _string; 24 } 25 - (IBAction)button7:(UIButton *)sender 26 { 27     [_string appendString:@"7"]; 28     self.ResultField.text = _string; 29 } 30 - (IBAction)button8:(UIButton *)sender 31 { 32     [_string appendString:@"8"]; 33     self.ResultField.text = _string; 34 } 35 - (IBAction)button9:(UIButton *)sender 36 { 37     [_string appendString:@"9"]; 38     self.ResultField.text = _string; 39 } 40 - (IBAction)button4:(UIButton *)sender 41 { 42     [_string appendString:@"4"]; 43     self.ResultField.text = _string; 44 } 45 - (IBAction)button5:(UIButton *)sender 46 { 47     [_string appendString:@"5"]; 48     self.ResultField.text = _string; 49 } 50 - (IBAction)button6:(UIButton *)sender 51 { 52     [_string appendString:@"6"]; 53     self.ResultField.text = _string; 54 } 55 - (IBAction)button1:(UIButton *)sender 56 { 57     [_string appendString:@"1"]; 58     self.ResultField.text = _string; 59 } 60 - (IBAction)button3:(UIButton *)sender 61 { 62     [_string appendString:@"3"]; 63     self.ResultField.text = _string; 64 } 65 - (IBAction)button2:(UIButton *)sender 66 { 67     [_string appendString:@"2"]; 68     self.ResultField.text = _string; 69 } 70 - (IBAction)button0:(UIButton *)sender 71 { 72     [_string appendString:@"0"]; 73     self.ResultField.text = _string; 74 } 75  76 - (IBAction)buttonPoint:(UIButton *)sender 77 { 78     [_string appendString:@"."]; 79     self.ResultField.text = _string; 80 } 81  82 //触发算数运算事件 83 - (IBAction)buttonDiv:(UIButton *)sender 84 { 85     [_string appendString:@"/"]; 86     self.ResultField.text = _string; 87 } 88  89 - (IBAction)buttonMul:(UIButton *)sender 90 { 91     [_string appendString:@"*"]; 92     self.ResultField.text = _string; 93 } 94  95 - (IBAction)buttonSub:(UIButton *)sender 96 { 97     [_string appendString:@"-"]; 98     self.ResultField.text = _string; 99 }100 101 - (IBAction)buttonAdd:(UIButton *)sender102 {103     [_string appendString:@"+"];104     self.ResultField.text = _string;105 }106 107 //做结果运算操作108 - (IBAction)buttonEqual:(UIButton *)sender109 {110 111     for(int i=0; i<[_string length]; i++)112     {113         114         self.arr = [[NSArray alloc]init];115         116         //只输入一个数,不做运算117         if([_string length] == 1)118         {119             self.temp = [_string doubleValue];120             break;121         }122         123         124         //做加法运算125         if([_string characterAtIndex:i] == '+')126         {127             self.arr = [_string componentsSeparatedByString:@"+"];128             129             self.num1 = [self.arr[0] doubleValue];130             self.num2 = [self.arr[1] doubleValue];131             self.temp = self.num1 + self.num2;132             break;133         }134         135 136         //做减法运算137         if([_string characterAtIndex:(i+1)] == '-')138         {139             self.arr = [_string componentsSeparatedByString:@"-"];140            141             if([self.arr count] == 2)142             {143                 self.num1 = [self.arr[0] doubleValue];144                 self.num2 = [self.arr[1] doubleValue];145                 self.temp = self.num1 - self.num2;146             }147             else148             {149                 self.num1 = [self.arr[1] doubleValue];150                 self.num2 = [self.arr[2] doubleValue];151                 self.temp = -(self.num1 + self.num2);152             }153             break;154         }155         156         157         //做除法运算158         if([_string characterAtIndex:i] == '/')159         {160             self.arr = [_string componentsSeparatedByString:@"/"];161             self.num1 = [self.arr[0] doubleValue];162             self.num2 = [self.arr[1] doubleValue];163             self.temp = self.num1 / self.num2;164             break;165         }166         167         //做乘法运算168         if([_string characterAtIndex:i] == '*')169         {170             self.arr = [_string componentsSeparatedByString:@"*"];171             self.num1 = [self.arr[0] doubleValue];172             self.num2 = [self.arr[1] doubleValue];173             self.temp = self.num1 * self.num2;174             break;175         }176         177     }178     179     //输出结果180     [_string setString:[NSString stringWithFormat:@"%.2f",self.temp]];181     self.ResultField.text = _string;182 }183 184 - (void)viewDidLoad {185     [super viewDidLoad];186 187     //创建一个可变的字符串188     _string = [NSMutableString stringWithCapacity:20];189 }190 191 - (void)didReceiveMemoryWarning {192     [super didReceiveMemoryWarning];193     // Dispose of any resources that can be recreated.194 }195 196 @end

 

转载于:https://www.cnblogs.com/XYQ-208910/p/4761733.html

你可能感兴趣的文章
nodejs的事件驱动机制与传统webserver的多线程处理机制对比
查看>>
HDU 5387 Clock(分数类+模拟)
查看>>
基于JQuery实现表单元素值的回写
查看>>
jmap命令
查看>>
jQuery插件之ajaxFileUpload
查看>>
第三次作业
查看>>
Python的classmethod和staticmethod区别
查看>>
Ubuntu12.04 英文环境下使用ibus输入中文并自动启动输入法
查看>>
SpringMVC 拦截器HandlerInterceptor(一)
查看>>
(BFS)poj2935-Basic Wall Maze
查看>>
BZOJ3879: SvT
查看>>
GO语言学习——LiteIDE
查看>>
PHP+MySQL中字符集问题分析
查看>>
java的反射机制和javassist、asm
查看>>
imageNamed 、imageWithContentsOfFile、 initWithContentsFile区别
查看>>
【codeforces 749A】Bachgold Problem
查看>>
json_3层格式_数据源DataSet
查看>>
MD5密钥的加密,解密 生成密钥
查看>>
curl 查看一个web站点的响应时间
查看>>
DC综合简单总结(1)
查看>>