-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculatorViewController.swift
175 lines (152 loc) · 5.09 KB
/
calculatorViewController.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
//
// calculatorViewController.swift
// calculatorApp
//
// Created by 陳秉軒 on 2022/4/10.
//
import UIKit
class calculatorViewController: UIViewController {
@IBOutlet var numberOutlets: [UIButton]!
@IBOutlet var operatorOutlets: [UIButton]!
//
@IBOutlet weak var resultLabel: UILabel!
var dialFirstDigit = true
var decimals = false
var equation = ""
override func viewDidLoad() {
super.viewDidLoad()
for numberOutlet in numberOutlets{
numberOutlet.layer.cornerRadius = numberOutlet.frame.height/2
numberOutlet.backgroundColor = .gray
numberOutlet.tintColor = .white
}
for operatorOutlet in operatorOutlets{
operatorOutlet.layer.cornerRadius = operatorOutlet.frame.height/2
operatorOutlet.backgroundColor = .orange
operatorOutlet.tintColor = .white
}
operatorOutlets[3].backgroundColor = .gray
view.backgroundColor = .black
resultLabel.text = "0"
resultLabel.adjustsFontSizeToFitWidth = true
}
@IBAction func numberButton(_ sender: UIButton) {
let inPutNum = sender.tag
//判斷是否輸入第一位數
if dialFirstDigit == true{
resultLabel.text = String(inPutNum)
dialFirstDigit = false
}else{
//判斷第一位數是否為0
if resultLabel.text != "0" {
resultLabel.text! += String(inPutNum)
} else {
resultLabel.text! = String(inPutNum)
}
}
}
@IBAction func dotButton(_ sender: UIButton) {
if decimals == false{
resultLabel.text! += "."
decimals = true
}
}
@IBAction func clearAll(_ sender: UIButton) {
resultLabel.text = "0"
decimals = false
equation = ""
}
@IBAction func calculatingSigns(_ sender: UIButton) {
var doubleNum: String
if decimals == false {
doubleNum = ".0"
} else {
doubleNum = ""
}
if let inputNum = resultLabel.text{
switch sender.tag{
case 0:
equation += inputNum + doubleNum + "/"
case 1:
equation += inputNum + doubleNum + "*"
case 2:
equation += inputNum + doubleNum + "-"
case 3:
equation += inputNum + doubleNum + "+"
default:
return
}
}
print(equation)
dialFirstDigit = true
decimals = false
}
@IBAction func calculatingButton(_ sender: UIButton) {
if let inputNum = resultLabel.text {
equation += inputNum
print(equation)
}
let expression = NSExpression(format: equation)
if let calculateResult = expression.expressionValue(with: nil, context: nil) as? Double {
print(calculateResult)
if calculateResult.truncatingRemainder(dividingBy: 1) == 0 {
resultLabel.text = String(calculateResult.floor(toInteger: 1))
//resultLabel.text = String(Int(calculateResult))
print(calculateResult)
} else {
resultLabel.text = String(calculateResult.rounding(toDecimal: 6))
decimals = true
}
}
print(decimals)
dialFirstDigit = true
equation = ""
//decimals = false
}
@IBAction func PositiveAndNegative(_ sender: UIButton) {
if let inPutnum = resultLabel.text{
if inPutnum != "0" {
if let doubleInputNum = Double(inPutnum){
if doubleInputNum > 0 {
if decimals == true{
resultLabel.text = "-\(doubleInputNum)"
}else{
resultLabel.text = "-\(Int(doubleInputNum))"
}
}else{
if decimals == true{
resultLabel.text = "\(fabs(doubleInputNum))"
}else{
resultLabel.text = "\(abs(Int(doubleInputNum)))"
}
}
}
}
}
}
}
// 後退鍵
// @IBAction func deleteNum(_ sender: UIButton) {
// if resultLabel.text!.count > 1{
// resultLabel.text?.removeLast()
// }else if resultLabel.text != "0"{
// resultLabel.text = "0"
// }
// if resultLabel.text!.count < 2{
// isDecimals = false
// }
// }
//階乘
// result = 1
// if decimals == true {
// showAlert()
// } else {
// if let factorialNum = Int(numberLabel.text!) {
// if factorialNum != 0 {
// for num in 1...factorialNum {
// result *= num
// }
// }
// }
// numberLabel.text = String(result)
// }