服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - swift实现简单的计算器

swift实现简单的计算器

2022-07-21 10:40ningto.com Swift

这篇文章主要为大家详细介绍了swift实现简单的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了swift实现简单计算器的具体代码,供大家参考,具体内容如下

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

延伸 · 阅读

精彩推荐
  • SwiftSwift UIButton使用教程

    Swift UIButton使用教程

    这篇文章主要介绍了Swift UIButton的使用方法,帮助大家更好的理解和学习swift编程,感兴趣的朋友可以了解下...

    说过的玩笑9072021-01-16
  • Swift窥探Swift编程中的错误处理与异常抛出

    窥探Swift编程中的错误处理与异常抛出

    本文给大家整理些关于Swift编程中的错误处理与异常抛出,本文介绍的非常详细,基于参考价值,特此分享脚本之家平台供大家学习...

    青玉伏案3142020-12-24
  • Swift详解Swift编程中的for循环的编写方法

    详解Swift编程中的for循环的编写方法

    这篇文章主要介绍了Swift编程中的for循环的编写方法,包括相关的for...in循环,需要的朋友可以参考下...

    Swift教程网4772020-12-22
  • SwiftSwiftUI使用Paths和AnimatableData实现酷炫的颜色切换动画

    SwiftUI使用Paths和AnimatableData实现酷炫的颜色切换动画

    这篇文章主要介绍了SwiftUI使用Paths和AnimatableData实现酷炫的颜色切换动画,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考...

    CodingSuccess12272021-01-15
  • Swift一篇带给你Swift 中的反射 Mirror

    一篇带给你Swift 中的反射 Mirror

    Mirror是Swift中的反射机制,对于C#和Java开发人员来说,应该很熟悉反射这个概念。反射就是可以动态的获取类型以及成员信息,同时也可以在运行时动态的...

    Swift 社区12572021-04-21
  • SwiftSwift解决UITableView空数据视图问题的简单方法

    Swift解决UITableView空数据视图问题的简单方法

    这篇文章主要给大家介绍了关于Swift解决UITableView空数据视图问题的简单方法,文中通过示例代码介绍的非常详细,对大家学习或者使用swift具有一定的参考...

    紫色大番薯10512021-01-12
  • SwiftSwift心得笔记之集合类型

    Swift心得笔记之集合类型

    本文为大家讲解的是swift语言中的集合类型数据,这是swift开发必须掌握的知识点,感兴趣的同学参考下。...

    Swift教程网2552020-12-19
  • Swiftswift 4自定义UITableCell的方法示例

    swift 4自定义UITableCell的方法示例

    这篇文章主要给大家介绍了关于swift 4自定义UITableCell的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    朋也6422021-01-24