|
@@ -24,10 +24,10 @@ public class CalcSkin extends Application {
|
24
|
24
|
launch(args);
|
25
|
25
|
}
|
26
|
26
|
private static final String[][] template = {
|
27
|
|
- { "7", "8", "9", "/" },
|
28
|
|
- { "4", "5", "6", "*" },
|
29
|
|
- { "1", "2", "3", "-" },
|
30
|
|
- { "0", "c", "=", "+" }
|
|
27
|
+ { "7", "8", "9", "/", "x²" },
|
|
28
|
+ { "4", "5", "6", "*", "√x" },
|
|
29
|
+ { "1", "2", "3", "-", "x⁻¹" },
|
|
30
|
+ { "0", "c", "=", "+", "xⁿ" }
|
31
|
31
|
};
|
32
|
32
|
|
33
|
33
|
private final Map<String, Button> accelerators = new HashMap<>();
|
|
@@ -36,7 +36,7 @@ public class CalcSkin extends Application {
|
36
|
36
|
private DoubleProperty currentValue = new SimpleDoubleProperty();
|
37
|
37
|
private CalcEngine calcEngine = new CalcEngine();
|
38
|
38
|
|
39
|
|
- private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
|
|
39
|
+ private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, EXPONENT }
|
40
|
40
|
|
41
|
41
|
private Op curOp = Op.NOOP;
|
42
|
42
|
private Op stackOp = Op.NOOP;
|
|
@@ -114,6 +114,12 @@ public class CalcSkin extends Application {
|
114
|
114
|
makeClearButton(button);
|
115
|
115
|
} else if ("=".equals(s)) {
|
116
|
116
|
makeEqualsButton(button);
|
|
117
|
+ } else if ("x²".equals(s)) {
|
|
118
|
+ makeSingleIntegerButton(button);
|
|
119
|
+ } else if ("x⁻¹".equals(s)) {
|
|
120
|
+ makeSingleIntegerButton(button);
|
|
121
|
+ } else if ("√x".equals(s)) {
|
|
122
|
+ makeSingleIntegerButton(button);
|
117
|
123
|
}
|
118
|
124
|
}
|
119
|
125
|
|
|
@@ -127,12 +133,16 @@ public class CalcSkin extends Application {
|
127
|
133
|
case "-": triggerOp.set(Op.SUBTRACT); break;
|
128
|
134
|
case "*": triggerOp.set(Op.MULTIPLY); break;
|
129
|
135
|
case "/": triggerOp.set(Op.DIVIDE); break;
|
|
136
|
+ case "x²": triggerOp.set(Op.SQUARE); break;
|
|
137
|
+ case "√x": triggerOp.set(Op.SQRROOT); break;
|
|
138
|
+ case "x⁻¹": triggerOp.set(Op.INVERSE); break;
|
|
139
|
+ case "xⁿ": triggerOp.set(Op.EXPONENT); break;
|
130
|
140
|
}
|
131
|
141
|
return triggerOp;
|
132
|
142
|
}
|
133
|
143
|
|
134
|
144
|
private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
|
135
|
|
- button.setStyle("-fx-base: lightgray;");
|
|
145
|
+ button.setStyle("-fx-base: azure;");
|
136
|
146
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
137
|
147
|
@Override
|
138
|
148
|
public void handle(ActionEvent actionEvent) {
|
|
@@ -175,6 +185,19 @@ public class CalcSkin extends Application {
|
175
|
185
|
});
|
176
|
186
|
}
|
177
|
187
|
|
|
188
|
+ private void makeSingleIntegerButton(Button button) {
|
|
189
|
+ button.setStyle("-fx-base: azure");
|
|
190
|
+ button.setOnAction(new EventHandler<ActionEvent>() {
|
|
191
|
+ @Override
|
|
192
|
+ public void handle(ActionEvent actionEvent) {
|
|
193
|
+ switch (stackOp) {
|
|
194
|
+ case SQUARE: currentValue.set(calcEngine.squ(currentValue.get())); break;
|
|
195
|
+ case INVERSE: currentValue.set(calcEngine.inv(currentValue.get())); break;
|
|
196
|
+ case SQRROOT: currentValue.set(calcEngine.sqrt(currentValue.get())); break;
|
|
197
|
+ }
|
|
198
|
+ }});
|
|
199
|
+ }
|
|
200
|
+
|
178
|
201
|
private void makeEqualsButton(Button button) {
|
179
|
202
|
button.setStyle("-fx-base: ghostwhite;");
|
180
|
203
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
|
@@ -185,6 +208,7 @@ public class CalcSkin extends Application {
|
185
|
208
|
case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
|
186
|
209
|
case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
|
187
|
210
|
case DIVIDE: currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
|
|
211
|
+ case EXPONENT: currentValue.set(calcEngine.exp(previousValue.get(), currentValue.get())); break;
|
188
|
212
|
}
|
189
|
213
|
}
|
190
|
214
|
});
|