-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView.swift
177 lines (157 loc) · 5.35 KB
/
ContentView.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
//
// ContentView.swift
// Calculator
//
// Created by ojohnpepsi on 2023-04-23.
//
import SwiftUI
enum CalcButton: String {
case one = "1"
case two = "2"
case three = "3"
case four = "4"
case five = "5"
case six = "6"
case seven = "7"
case eight = "8"
case nine = "9"
case zero = "0"
case add = "+"
case subtract = "-"
case divide = "/"
case multiply = "x"
case equal = "="
case clear = "AC"
case decimal = "."
case percent = "%"
case negative = "-/+"
var buttonColor: Color {
switch self {
case .add, .subtract, .multiply, .divide, .equal:
return .orange
case .clear, .negative, .percent:
return Color(.lightGray)
default:
return Color(UIColor(red: 55/255.0, green: 55/255.0, blue:
55/255.0, alpha: 1))
}
}
}
enum Operation {
case add, subtract, multiply, divide, none
}
struct ContentView: View {
@State var value = "0"
@State var runningNumber = 0
@State var currentOperation: Operation = .none
let buttons: [[CalcButton]] = [
[.clear, .negative, .percent, .divide],
[.seven, .eight, .nine, . multiply],
[.four, .five, .six, .subtract],
[.one, .two, .three, .add],
[.zero, .decimal, .equal]
]
var body: some View {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
// Text display
HStack {
Spacer()
Text(value)
.bold()
.font(.system(size: 100))
.foregroundColor(.white)
}
.padding()
// Our buttons
ForEach(buttons, id: \.self) { row in
HStack (spacing: 12) {
ForEach(row, id: \.self) { item in
Button(action: {
self.didTap(button: item)
}, label: {
Text(item.rawValue)
.font(.system(size: 32))
.frame(
width: self.buttonWidth(item:
item),
height: self.buttonHeight()
)
.background(item.buttonColor)
.foregroundColor(.white)
.cornerRadius(self.buttonWidth(item:
item)/2)
})
}
}
.padding(.bottom, 3)
}
}
}
}
func didTap(button: CalcButton) {
switch button {
case .add, .subtract, .multiply, .divide, .equal:
if button == .add {
self.currentOperation = .add
self.runningNumber = Int(self.value) ?? 0
}
else if button == .subtract {
self.currentOperation = .subtract
self.runningNumber = Int(self.value) ?? 0
}
else if button == .multiply {
self.currentOperation = .multiply
self.runningNumber = Int(self.value) ?? 0
}
else if button == .divide {
self.currentOperation = .divide
self.runningNumber = Int(self.value) ?? 0
}
else if button == .equal {
let runningValue = self.runningNumber
let currentValue = Int(self.value) ?? 0
switch self.currentOperation {
case .add: self.value = "\(runningValue + currentValue)"
case .subtract: self.value = "\(runningValue - currentValue)"
case .multiply: self.value = "\(runningValue * currentValue)"
case .divide: self.value = "\(runningValue / currentValue)"
case .none:
break
}
}
if button != .equal {
self.value = "0"
}
case .clear:
self.value = "0"
break
case .decimal, .negative, .percent:
break
default:
let number = button.rawValue
if self.value == "0" {
value = number
}
else{
self.value = "\(self.value)\(number)"
}
}
}
func buttonWidth(item: CalcButton) -> CGFloat {
if item == .zero {
return ((UIScreen.main.bounds.width - (4*12)) / 4) * 2
}
return (UIScreen.main.bounds.width - (5*12)) / 4
}
func buttonHeight() -> CGFloat {
return (UIScreen.main.bounds.width - (5*12)) / 4
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}