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

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

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

Swift实现简单计算器

2022-07-27 10:12qq_29284809 Swift

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

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

使用Storyboard

快速而又方便的进行控件的布局,功能操作简单的进行一些运算;

代码实现

?
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
//
//  ViewController.swift
//  Swift_Calculator
//
//  Created by 周文春 on 16/3/2.
//  Copyright © 2016年 周文春. All rights reserved.
//
 
import UIKit
 
class ViewController: UIViewController {
 
    @IBOutlet weak var lableResult: UILabel!
 
    //第一操作
    var firstOperand: Double = 0.0
    //第二操作
    var secondOperand: Double = 0.0
    //标记是否输入了小数点
    var decimalPointFlag: Bool = false
    //是否输入第二操作数
    var isSecond: Bool = false
    //操作符
    var operatorFlag: String = ""
 
    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.
    }
 
 
    @IBAction func buttonTap(sender: UIButton) {
        //lableResult 中默认是0,如果开始输入数字,则先清除0
        if lableResult.text == "0" || (isSecond && secondOperand == 0.0) {
 
            lableResult.text = ""
        }
        //将用户录入的数添加到lableResult中
        lableResult.text = lableResult.text! + sender.titleLabel!.text!
 
        if isSecond {
//            secondOperand = (lableResult.text! as NSString).doubleValue
            secondOperand = NSString(string: lableResult.text!).doubleValue
        }else {
            //将lableResult中的字符串转化为双精度数
//            firstOperand = (lableResult.text! as NSString).doubleValue
            firstOperand = NSString(string: lableResult.text!).doubleValue
        }
        print(firstOperand)
    }
 
 
    @IBAction func decimalPointTap() {
 
        if !decimalPointFlag {
            lableResult.text = lableResult.text! + "."
            if isSecond {
                secondOperand = (lableResult.text! as NSString).doubleValue
            }else {
                firstOperand = (lableResult.text! as NSString).doubleValue
            }
 
            decimalPointFlag = !decimalPointFlag
        }
    }
 
 
    @IBAction func operatorTap(sender: UIButton) {
        if firstOperand != 0 {
            isSecond = true
            decimalPointFlag = false
            switch sender.titleLabel!.text! {
                case "+":
                      operatorFlag = "+"
                case "-":
                      operatorFlag = "-"
                case "*":
                      operatorFlag = "*"
                case "÷":
                      operatorFlag = "/"
            default:
                      operatorFlag = " "
            }
        }
    }
 
 
    @IBAction func resultTap(sender: UIButton) {
        //确保第二操作数有值
        if isSecond {
            //除数不能为0
            if operatorFlag == "/" && secondOperand == 0 {
                print("Error: 除数不能为0")
                return
            }
 
            var result: Double = 0.0
            switch operatorFlag {
                case "+":
                result = firstOperand + secondOperand
                case "-":
                result = firstOperand - secondOperand
                case  "*":
                result = firstOperand * secondOperand
                case  "/":
                result = firstOperand / secondOperand
            default :
                result = 0.0
            }
 
            lableResult.text = result.description
            print("第一操作: \(firstOperand)")
            print("操作符: \(operatorFlag)")
            print("第二操作: \(secondOperand)")
            print("结果: \(result)")
 
        }
 
    }
 
    @IBAction func clear(sender: UIButton) {
 
        //lable对象显示0
        lableResult.text = "0"
 
        //第一操作数清零
        firstOperand = 0.0
 
        //第二操作数清零
        secondOperand = 0.0
 
        //小数点标记设置为假
        decimalPointFlag = false
 
        //第二操作数标记设置为假
        isSecond = false
 
        //操作清空
        operatorFlag = ""
 
    }
}

Swift实现简单计算器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_29284809/article/details/50803536

延伸 · 阅读

精彩推荐