Sfoglia il codice sorgente

got square, square root, exponent

mpierse 6 anni fa
parent
commit
ffc20101c2

+ 11
- 1
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Vedi File

@@ -12,4 +12,14 @@ public class CalcEngine {
12 12
     public double multiply(double v, double v1) { return (v*v1);}
13 13
 
14 14
     public double divide(double v, double v1) { return (v/v1);}
15
-}
15
+
16
+        public double invert(double v) {
17
+            return (v*-1); }
18
+
19
+        public double exponent(double v, double v1){
20
+        return (Math.pow(v,v1)); }
21
+
22
+
23
+
24
+
25
+    }

+ 46
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Vedi File

@@ -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^2"},
28
+            { "4", "5", "6", "*" , "sqrt"},
29
+            { "1", "2", "3", "-" , "1/x"},
30
+            { "0", "c", "=", "+" , "x^y"}
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, SQUARED, SQUAREROOT, INVERT, 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^2".equals(s)){
118
+                makeSquareButton(button);
119
+            } else if ("sqrt".equals(s)){
120
+                makeSquareRootButton(button);
121
+            } else if ("i/x".equals(s)){
122
+                makeInvertButton(button);
117 123
             }
118 124
         }
119 125
 
@@ -127,7 +133,8 @@ 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;
130
-        }
136
+            case "x^y": triggerOp.set(Op.EXPONENT); break;
137
+}
131 138
         return triggerOp;
132 139
     }
133 140
 
@@ -165,6 +172,36 @@ public class CalcSkin extends Application {
165 172
         });
166 173
     }
167 174
 
175
+    private void makeSquareButton(Button button) {
176
+        button.setStyle("-fx-base: #48ff2f;");
177
+        button.setOnAction(new EventHandler<ActionEvent>() {
178
+            @Override
179
+            public void handle(ActionEvent actionEvent) {
180
+                currentValue.set(Math.pow(currentValue.get(), 2));
181
+            }
182
+        });
183
+    }
184
+
185
+    private void makeSquareRootButton(Button button) {
186
+        button.setStyle("-fx-base: #48ff2f;");
187
+        button.setOnAction(new EventHandler<ActionEvent>() {
188
+            @Override
189
+            public void handle(ActionEvent actionEvent) {
190
+                currentValue.set(Math.sqrt(currentValue.get()));
191
+            }
192
+        });
193
+    }
194
+
195
+    private void makeInvertButton(Button button) {
196
+        button.setStyle("-fx-base: #48ff2f;");
197
+        button.setOnAction(new EventHandler<ActionEvent>() {
198
+            @Override
199
+            public void handle(ActionEvent actionEvent) {
200
+                currentValue.set(1/currentValue.get());
201
+            }
202
+        });
203
+    }
204
+
168 205
     private void makeClearButton(Button button) {
169 206
         button.setStyle("-fx-base: mistyrose;");
170 207
         button.setOnAction(new EventHandler<ActionEvent>() {
@@ -180,11 +217,14 @@ public class CalcSkin extends Application {
180 217
         button.setOnAction(new EventHandler<ActionEvent>() {
181 218
             @Override
182 219
             public void handle(ActionEvent actionEvent) {
220
+
183 221
                 switch (stackOp) {
184 222
                     case ADD:      currentValue.set(calcEngine.add(previousValue.get(), currentValue.get())); break;
185 223
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 224
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 225
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
226
+                    case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
227
+
188 228
                 }
189 229
             }
190 230
         });