本文实例为大家分享了swift实现简单计算器的具体代码,供大家参考,具体内容如下
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// // ViewController.swift // Calculator // // Created by tutujiaw on 15/4/25. // Copyright (c) 2015年 tutujiaw. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var display: UILabel! var sumInMemory: Double = 0.0 var sumSoFar: Double = 0.0 var factorSoFar: Double = 0.0 var pendingAdditiveOperator = "" var pendingMultiplicativeOperator = "" var waitingForOperand = true var displayValue: Double { set { let intValue = Int(newValue) if (Double(intValue) == newValue) { display.text = "\(intValue)" } else { display.text = "\(newValue)" } } get { return (display.text! as NSString).doubleValue } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func calculate(rightOperand: Double, pendingOperator: String) -> Bool { var result = false switch pendingOperator { case "+" : sumSoFar += rightOperand result = true case "-" : sumSoFar -= rightOperand result = true case "*" : factorSoFar *= rightOperand result = true case "/" : if rightOperand != 0.0 { factorSoFar /= rightOperand result = true } default : break } return result } func abortOperation() { clearAll() display.text = "####" } @IBAction func digitClicked(sender: UIButton) { let digitValue = sender.currentTitle?.toInt() if display.text!.toInt() == 0 && digitValue == 0 { return } if waitingForOperand { display.text = "" waitingForOperand = false } display.text = display.text! + sender.currentTitle! } @IBAction func changeSignClicked() { displayValue *= -1 } @IBAction func backspaceClicked() { if waitingForOperand { return } var strValue = display.text! display.text = dropLast(strValue) if display.text!.isEmpty { displayValue = 0.0 waitingForOperand = true } } @IBAction func clear() { if waitingForOperand { return } displayValue = 0 waitingForOperand = true } @IBAction func clearAll() { sumSoFar = 0.0 factorSoFar = 0.0 pendingAdditiveOperator = "" pendingMultiplicativeOperator = "" displayValue = 0.0 waitingForOperand = true } @IBAction func clearMemory() { sumInMemory = 0.0 } @IBAction func readMemory() { displayValue = sumInMemory waitingForOperand = true } @IBAction func setMemory() { equalClicked() sumInMemory = displayValue } @IBAction func addToMemory() { equalClicked() sumInMemory += displayValue } @IBAction func multiplicativeOperatorClicked(sender: UIButton) { var clickedOperator = sender.currentTitle! var operand = displayValue if !pendingMultiplicativeOperator.isEmpty { if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) { abortOperation() return } displayValue = factorSoFar } else { factorSoFar = operand } pendingMultiplicativeOperator = clickedOperator waitingForOperand = true } @IBAction func additiveOperatorClicked(sender: UIButton) { let clickedOperator = sender.currentTitle! var operand = displayValue if !pendingMultiplicativeOperator.isEmpty { if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) { abortOperation() return } displayValue = factorSoFar factorSoFar = 0.0 pendingMultiplicativeOperator = "" } if !pendingAdditiveOperator.isEmpty { if !calculate(operand, pendingOperator: pendingAdditiveOperator) { abortOperation() return } displayValue = sumSoFar } else { sumSoFar = operand } pendingAdditiveOperator = clickedOperator waitingForOperand = true } @IBAction func unaryOperatorClicked(sender: UIButton) { let clickedOperator = sender.currentTitle! var result: Double = 0 if clickedOperator == "Sqrt" { if displayValue < 0 { abortOperation() return } result = sqrt (displayValue) } else if clickedOperator == "x^2" { result = pow (displayValue, 2) } else if clickedOperator == "1/x" { if displayValue == 0 { abortOperation() return } result = 1.0 / displayValue } displayValue = result waitingForOperand = true } @IBAction func equalClicked() { var operand = displayValue if !pendingMultiplicativeOperator.isEmpty { if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) { abortOperation() return } operand = factorSoFar factorSoFar = 0.0 pendingMultiplicativeOperator = "" } if !pendingAdditiveOperator.isEmpty { if !calculate(operand, pendingOperator: pendingAdditiveOperator) { abortOperation() return } pendingAdditiveOperator = "" } else { sumSoFar = operand } displayValue = sumSoFar sumSoFar = 0.0 waitingForOperand = true } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/tujiaw/article/details/44132419