@@ -3,19 +3,51 @@ import SwiftUI
33struct ContentView : View {
44 @State private var display = " 0 "
55
6+ let buttons = [
7+ [ " 7 " , " 8 " , " 9 " , " ÷ " ] ,
8+ [ " 4 " , " 5 " , " 6 " , " × " ] ,
9+ [ " 1 " , " 2 " , " 3 " , " - " ] ,
10+ [ " C " , " 0 " , " = " , " + " ]
11+ ]
12+
613 var body : some View {
7- VStack {
14+ VStack ( spacing: 12 ) {
15+ Spacer ( )
16+ // Result Display
817 Text ( display)
9- . font ( . system( size: 70 ) )
18+ . font ( . system( size: 80 , weight: . light) )
19+ . foregroundColor ( . white)
20+ . frame ( maxWidth: . infinity, alignment: . trailing)
1021 . padding ( )
11- Button ( " Reset " ) {
12- display = " 0 "
22+
23+ // Button Grid
24+ ForEach ( buttons, id: \. self) { row in
25+ HStack ( spacing: 12 ) {
26+ ForEach ( row, id: \. self) { button in
27+ Button ( action: { self . tapped ( button) } ) {
28+ Text ( button)
29+ . font ( . title)
30+ . frame ( width: 80 , height: 80 )
31+ . background ( self . buttonColor ( button) )
32+ . foregroundColor ( . white)
33+ . clipShape ( Circle ( ) )
34+ }
35+ }
36+ }
1337 }
14- . font ( . title)
15- . padding ( )
16- . background ( Color . red)
17- . foregroundColor ( . white)
18- . cornerRadius ( 10 )
1938 }
39+ . padding ( )
40+ . background ( Color . black. edgesIgnoringSafeArea ( . all) )
41+ }
42+
43+ func tapped( _ button: String ) {
44+ if button == " C " { display = " 0 " }
45+ else { display = ( display == " 0 " ) ? button : display + button }
46+ }
47+
48+ func buttonColor( _ b: String ) -> Color {
49+ if [ " ÷ " , " × " , " - " , " + " , " = " ] . contains ( b) { return . orange }
50+ if b == " C " { return . gray }
51+ return Color ( white: 0.2 )
2052 }
2153}
0 commit comments