#31 tkhong102

Open
tkhong102 wants to merge 1 commits from tkhong102/CalcSkin:master into master

+ 20
- 6
src/main/java/rocks/zipcode/calcskin/CalcEngine.java View File

5
     CalcEngine() {
5
     CalcEngine() {
6
     }
6
     }
7
 
7
 
8
-    public double add(double v, double v1) {
9
-        return Double.NaN;
10
-    }
8
+    public double add(double v, double v1) { return v + v1; }
11
 
9
 
12
     public double subtract(double v, double v1) {
10
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
11
+        return v-v1;
14
     }
12
     }
15
 
13
 
16
     public double multiply(double v, double v1) {
14
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
15
+        return v*v1;
18
     }
16
     }
19
 
17
 
20
     public double divide(double v, double v1) {
18
     public double divide(double v, double v1) {
21
-        return Double.NaN;
19
+        return v/v1;
22
     }
20
     }
21
+
22
+    public double square(double v) {return v*v;}
23
+
24
+    public double squareroot(double v) { return Math.sqrt(v);}
25
+
26
+    public double exponent(double v, double v1) { return Math.pow(v, v1); }
27
+
28
+    public double inverse(double v) { return (1 / v); }
29
+
30
+    public double invert(double v) { return (-v); }
31
+
32
+    public double findSin(double v) { return Math.sin(v); }
33
+
34
+    public double findCos(double v) { return Math.cos(v); }
35
+
36
+    public double findTan(double v) { return Math.tan(v); }
23
 }
37
 }

+ 52
- 2
src/main/java/rocks/zipcode/calcskin/CalcSkin.java View File

24
         launch(args);
24
         launch(args);
25
     }
25
     }
26
     private static final String[][] template = {
26
     private static final String[][] template = {
27
+            {"sin", "cos", "tan", "-x"},
28
+            { "x²", "√", "xⁿ", "1/x"},
27
             { "7", "8", "9", "/" },
29
             { "7", "8", "9", "/" },
28
             { "4", "5", "6", "*" },
30
             { "4", "5", "6", "*" },
29
             { "1", "2", "3", "-" },
31
             { "1", "2", "3", "-" },
36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
38
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37
     private CalcEngine calcEngine = new CalcEngine();
39
     private CalcEngine calcEngine = new CalcEngine();
38
 
40
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
41
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE, SQUAREROOT, EXPONENT,
42
+    INVERSE, INVERT, FINDSIN, FINDCOS, FINDTAN}
40
 
43
 
41
     private Op curOp   = Op.NOOP;
44
     private Op curOp   = Op.NOOP;
42
     private Op stackOp = Op.NOOP;
45
     private Op stackOp = Op.NOOP;
127
             case "-": triggerOp.set(Op.SUBTRACT); break;
130
             case "-": triggerOp.set(Op.SUBTRACT); break;
128
             case "*": triggerOp.set(Op.MULTIPLY); break;
131
             case "*": triggerOp.set(Op.MULTIPLY); break;
129
             case "/": triggerOp.set(Op.DIVIDE);   break;
132
             case "/": triggerOp.set(Op.DIVIDE);   break;
133
+            case "x²": triggerOp.set(Op.SQUARE);   break;
134
+            case "√": triggerOp.set(Op.SQUAREROOT); break;
135
+            case "xⁿ": triggerOp.set(Op.EXPONENT); break;
136
+            case "1/x": triggerOp.set(Op.INVERSE); break;
137
+            case "sin": triggerOp.set(Op.FINDSIN); break;
138
+            case "cos": triggerOp.set(Op.FINDCOS); break;
139
+            case "tan": triggerOp.set(Op.FINDTAN); break;
140
+            case "-x": triggerOp.set(Op.INVERT); break;
141
+
130
         }
142
         }
131
         return triggerOp;
143
         return triggerOp;
132
     }
144
     }
136
         button.setOnAction(new EventHandler<ActionEvent>() {
148
         button.setOnAction(new EventHandler<ActionEvent>() {
137
             @Override
149
             @Override
138
             public void handle(ActionEvent actionEvent) {
150
             public void handle(ActionEvent actionEvent) {
139
-                curOp = triggerOp.get();
151
+                if (triggerOp.get() == Op.SQUARE) {
152
+                    currentValue.set(calcEngine.square(currentValue.get()));
153
+                    stackOp = curOp;
154
+                    curOp = Op.NOOP;
155
+                }else if(triggerOp.get() ==  Op.SQUAREROOT){
156
+                    currentValue.set(calcEngine.squareroot(currentValue.get()));
157
+                    stackOp = curOp;
158
+                    curOp = Op.NOOP;
159
+                }else if(triggerOp.get() ==  Op.FINDCOS){
160
+                    currentValue.set(calcEngine.findCos(currentValue.get()));
161
+                    stackOp = curOp;
162
+                    curOp = Op.NOOP;
163
+                }else if(triggerOp.get() ==  Op.FINDSIN){
164
+                    currentValue.set(calcEngine.findSin(currentValue.get()));
165
+                    stackOp = curOp;
166
+                    curOp = Op.NOOP;
167
+                }else if(triggerOp.get() ==  Op.FINDTAN){
168
+                    currentValue.set(calcEngine.findTan(currentValue.get()));
169
+                    stackOp = curOp;
170
+                    curOp = Op.NOOP;
171
+                }else if(triggerOp.get() ==  Op.INVERSE){
172
+                    currentValue.set(calcEngine.inverse(currentValue.get()));
173
+                    stackOp = curOp;
174
+                    curOp = Op.NOOP;
175
+                }else if(triggerOp.get() ==  Op.INVERT){
176
+                    currentValue.set(calcEngine.invert(currentValue.get()));
177
+                    stackOp = curOp;
178
+                    curOp = Op.NOOP;
179
+                } else {
180
+                    curOp = triggerOp.get();
181
+                }
140
             }
182
             }
141
         });
183
         });
142
     }
184
     }
185
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
227
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
228
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
229
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
230
+                    case SQUARE:   currentValue.set(calcEngine.square(currentValue.get())); break;
231
+                    case SQUAREROOT: currentValue.set(calcEngine.squareroot(previousValue.get())); break;
232
+                    case INVERSE: currentValue.set(calcEngine.inverse(currentValue.get())); break;
233
+                    case INVERT: currentValue.set(calcEngine.invert(currentValue.get())); break;
234
+                    case FINDSIN: currentValue.set(calcEngine.findSin(currentValue.get())); break;
235
+                    case FINDCOS: currentValue.set(calcEngine.findCos(currentValue.get())); break;
236
+                    case FINDTAN: currentValue.set(calcEngine.findTan(currentValue.get())); break;
237
+                    case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
188
                 }
238
                 }
189
             }
239
             }
190
         });
240
         });