Selaa lähdekoodia

added trig and advanced functions

Michelle DiMarino 6 vuotta sitten
vanhempi
commit
917d9bfa04

BIN
.DS_Store Näytä tiedosto


BIN
src/.DS_Store Näytä tiedosto


BIN
src/main/.DS_Store Näytä tiedosto


BIN
src/main/java/.DS_Store Näytä tiedosto


BIN
src/main/java/rocks/.DS_Store Näytä tiedosto


+ 31
- 0
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Näytä tiedosto

@@ -20,4 +20,35 @@ public class CalcEngine {
20 20
     public double divide(double v, double v1) {
21 21
         return v/v1;
22 22
     }
23
+
24
+    public double squared(double v){
25
+        return Math.pow(v,2);
26
+    }
27
+
28
+    public double exponent(double v, double y){
29
+        return Math.pow(v,y);
30
+    }
31
+
32
+    public double inverse(double v){
33
+        return 1/v;
34
+    }
35
+
36
+    public double squareRoot(double v) {
37
+        return Math.sqrt(v);
38
+    }
39
+
40
+    public double calcSin(double x) {
41
+        double answer = Math.sin(x);
42
+        return answer;
43
+    }
44
+
45
+    public double calcCos(double x) {
46
+        double answer = Math.cos(x);
47
+        return answer;
48
+    }
49
+
50
+    public double calcTan(double x) {
51
+        double answer = Math.tan(x);
52
+        return answer;
53
+    }
23 54
 }

+ 21
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Näytä tiedosto

@@ -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", "SINE"},
28
+            { "4", "5", "6", "*", "√", "COSINE"},
29
+            { "1", "2", "3", "-", "1/x","TAN" },
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, SROOT, INVERSE, EXPONENT, SINE, COSINE, TAN }
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -84,7 +84,7 @@ public class CalcSkin extends Application {
84 84
         screen.setStyle("-fx-background-color: aquamarine;");
85 85
         screen.setAlignment(Pos.CENTER_RIGHT);
86 86
         screen.setEditable(false);
87
-        screen.textProperty().bind(Bindings.format("%.0f", currentValue));
87
+        screen.textProperty().bind(Bindings.format("%.2f", currentValue));
88 88
         return screen;
89 89
     }
90 90
 
@@ -127,6 +127,13 @@ public class CalcSkin extends Application {
127 127
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 128
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 129
             case "/": triggerOp.set(Op.DIVIDE);   break;
130
+            case "x^2": triggerOp.set(Op.SQUARED); break;
131
+            case "√": triggerOp.set(Op.SROOT); break;
132
+            case "1/x": triggerOp.set(Op.INVERSE); break;
133
+            case "x^y": triggerOp.set(Op.EXPONENT); break;
134
+            case "SINE": triggerOp.set(Op.SINE); break;
135
+            case "COSINE": triggerOp.set(Op.COSINE); break;
136
+            case "TAN": triggerOp.set(Op.TAN); break;
130 137
         }
131 138
         return triggerOp;
132 139
     }
@@ -181,10 +188,18 @@ public class CalcSkin extends Application {
181 188
             @Override
182 189
             public void handle(ActionEvent actionEvent) {
183 190
                 switch (stackOp) {
191
+                    case NOOP:
184 192
                     case ADD:      currentValue.set(calcEngine.add(previousValue.get(), currentValue.get())); break;
185 193
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 194
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 195
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
196
+                    case SQUARED: currentValue.set(calcEngine.squared(previousValue.get())); break;
197
+                    case SROOT: currentValue.set(calcEngine.squareRoot(currentValue.get())); break;
198
+                    case INVERSE: currentValue.set(calcEngine.inverse(previousValue.get())); break;
199
+                    case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
200
+                    case SINE: currentValue.set(calcEngine.calcSin(previousValue.get())); break;
201
+                    case COSINE: currentValue.set(calcEngine.calcCos(previousValue.get())); break;
202
+                    case TAN: currentValue.set(calcEngine.calcTan(previousValue.get())); break;
188 203
                 }
189 204
             }
190 205
         });