@@ -26,28 +26,23 @@ case division
26
26
}
27
27
28
28
private struct CalculatorState {
29
- var lhs : String = " "
30
- var rhs : String = " "
31
- var operand : WritableKeyPath < Self , String > = \. lhs
29
+ var lhs : NSDecimalNumber = . zero
30
+ var rhs : NSDecimalNumber = . zero
31
+ var operand : WritableKeyPath < Self , NSDecimalNumber > = \. lhs
32
32
var operation : CalculatorOperation = . undefined {
33
33
willSet { self . operand = ( newValue == . undefined) ? \. lhs : \. rhs }
34
34
}
35
35
36
- mutating func evaluate( ) -> String {
37
- var f : ( ( Double , Double ) -> Double ) ?
38
-
36
+ mutating func evaluate( ) -> NSDecimalNumber {
39
37
switch self . operation {
40
38
case . undefined: break
41
- case . addition: f = { $0 + $1 }
42
- case . substraction: f = { $0 - $1 }
43
- case . multiplication: f = { $0 * $1 }
44
- case . division: f = { $0 / $1 }
39
+ case . addition: lhs = lhs . adding ( rhs )
40
+ case . substraction: lhs = lhs . subtracting ( rhs )
41
+ case . multiplication: lhs = lhs . multiplying ( by : rhs )
42
+ case . division: lhs = lhs . dividing ( by : rhs )
45
43
}
46
44
47
- if let f = f {
48
- lhs = String ( f ( Double ( lhs) !, Double ( rhs) !) )
49
- }
50
- rhs = " "
45
+ rhs = . zero
51
46
operation = . undefined
52
47
53
48
return lhs
@@ -101,6 +96,10 @@ private class Calculator {
101
96
self . window. rootViewController? . title = " Calculator "
102
97
103
98
self . window. addSubview ( self . txtResult)
99
+ self . txtResult. font = Font ( name: " Consolas " , size: Font . systemFontSize)
100
+ self . txtResult. textAlignment = . right
101
+ self . txtResult. text =
102
+ self . state. evaluate ( ) . description ( withLocale: Locale . current)
104
103
105
104
self . window. addSubviews ( self . btnDigits)
106
105
self . btnDigits. forEach {
@@ -124,25 +123,21 @@ private class Calculator {
124
123
fatalError ( " invalid target: \( self ) for sender: \( sender) " )
125
124
}
126
125
127
- self . state [ keyPath: self . state. operand] += String ( input)
126
+ var operand = self . state [ keyPath: self . state. operand] as Decimal
127
+ operand *= 10
128
+ operand += Decimal ( input)
128
129
129
- self . txtResult. text =
130
- ( NSDecimalNumber ( string: self . state [ keyPath: self . state. operand] )
131
- as Decimal ) . description
130
+ self . txtResult. text = operand. description
132
131
}
133
132
134
133
private func onOperationPress( _ sender: Button , _: Control . Event ) {
135
134
switch self . btnOperations. firstIndex ( of: sender) ! {
136
135
case 0 : /* AC */
137
136
self . state = CalculatorState ( )
138
- self . txtResult. text = " 0 "
137
+ self . txtResult. text = Decimal . zero . description
139
138
case 1 : /* +/- */
140
- var value : Decimal =
141
- NSDecimalNumber ( string: self . state [ keyPath: self . state. operand] )
142
- as Decimal
143
- value. negate ( )
144
- self . state [ keyPath: self . state. operand] = value. description
145
- self . txtResult. text = value. description
139
+ var operand = self . state [ keyPath: self . state. operand] as Decimal
140
+ operand. negate ( )
146
141
case 3 : /* ÷ */
147
142
self . state. operation = . division
148
143
case 4 : /* x */
@@ -152,22 +147,21 @@ private class Calculator {
152
147
case 6 : /* + */
153
148
self . state. operation = . addition
154
149
case 2 : /* % */
155
- if Double ( self . state. lhs) == 0.0 { break }
150
+ if ( self . state. lhs as Decimal ) . isZero { break }
156
151
self . state. operation = . division
157
- self . state. rhs = " 100 "
152
+ self . state. rhs = NSDecimalNumber ( string : " 100 " )
158
153
fallthrough
159
154
case 7 : /* = */
160
155
self . txtResult. text =
161
- ( NSDecimalNumber ( string: self . state. evaluate ( ) ) as Decimal )
162
- . description
156
+ self . state. evaluate ( ) . description ( withLocale: Locale . current)
163
157
default :
164
158
fatalError ( " unknown operation \( self . btnOperations. firstIndex ( of: sender) !) " )
165
159
}
166
160
}
167
161
168
162
private func onDecimalPress( _ sender: Button , _: Control . Event ) {
169
- self . state [ keyPath : self . state . operand ] += " . "
170
- self . txtResult . text = self . state [ keyPath: self . state. operand]
163
+ self . txtResult . text =
164
+ ( self . state [ keyPath: self . state. operand] as Decimal ) . description
171
165
}
172
166
}
173
167
0 commit comments