iOS:制作一个简易的计算器

   初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些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 *)sender
102 {
103     [_string appendString:@"+"];
104     self.ResultField.text = _string;
105 }
106 
107 //做结果运算操作
108 - (IBAction)buttonEqual:(UIButton *)sender
109 {
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             else
148             {
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

 

posted @ 2015-08-26 21:34  XYQ全哥  阅读(853)  评论(0编辑  收藏  举报