邓心一 6 anni fa
parent
commit
4ff2c23137
1 ha cambiato i file con 21 aggiunte e 15 eliminazioni
  1. 21 15
      oj/bytedance-2019-interview/Calculator.java

+ 21 - 15
oj/bytedance-2019-interview/Calculator.java

@@ -11,10 +11,10 @@ public class Calculator {
         }
     }
 
-    private static int calculate(String str) throws Exception {
+    private static double calculate(String str) throws Exception {
         char[] exp = str.toCharArray();
         Stack<Character> ops = new Stack<>();
-        Stack<Integer> num = new Stack<>();
+        Stack<Double> num = new Stack<>();
         //   + - * / ( ) $
         // + > > < < < > >
         // - > > < < < > >
@@ -26,7 +26,7 @@ public class Calculator {
             if (i == exp.length) {
                 if (ops.isEmpty()) break;
                 if (ops.peek() == '(') throw new Exception("Invalid expression!");
-                num.push(operate(ops.pop(), num.pop(), num.pop()));
+                operate(ops, num);
                 continue;
             }
             switch (exp[i]) {
@@ -34,14 +34,14 @@ public class Calculator {
                     if (ops.isEmpty() || ops.peek() == '(') {
                         ops.push(exp[i++]);
                     } else {
-                        num.push(operate(ops.pop(), num.pop(), num.pop()));
+                        operate(ops, num);
                     }
                     break;
                 case '*': case '/':
                     if (ops.isEmpty() || ops.peek() == '(' || ops.peek() == '+' || ops.peek() == '-') {
                         ops.push(exp[i++]);
                     } else {
-                        num.push(operate(ops.pop(), num.pop(), num.pop()));
+                        operate(ops, num);
                     }
                     break;
                 case '(':
@@ -53,13 +53,13 @@ public class Calculator {
                         ops.pop();
                         i++;
                     } else {
-                        num.push(operate(ops.pop(), num.pop(), num.pop()));
+                        operate(ops, num);
                     }
                     break;
                 default:
                     int j;
-                    for (j = i + 1; j < exp.length && '0' <= exp[j] && exp[j] <= '9'; j++);
-                    int n = Integer.parseInt(str.substring(i, j));
+                    for (j = i + 1; j < exp.length && (('0' <= exp[j] && exp[j] <= '9') || exp[j] == '.'); j++);
+                    double n = Double.parseDouble(str.substring(i, j));
                     num.push(n);
                     i = j;
             }
@@ -68,18 +68,24 @@ public class Calculator {
         return num.peek();
     }
 
-    private static int operate(char op, int n2, int n1) throws Exception {
+    private static void operate(Stack<Character> ops, Stack<Double> num) throws Exception {
+        if (num.size() < 2) throw new Exception("Invalid expression!");
+        char op = ops.pop();
+        double n2 = num.pop(), n1 = num.pop();
         switch (op) {
             case '+':
-                return n1 + n2;
+                num.push(n1 + n2);
+                break;
             case '-':
-                return n1 - n2;
+                num.push(n1 - n2);
+                break;
             case '*':
-                return n1 * n2;
+                num.push(n1 * n2);
+                break;
             case '/':
-                if (n2 == 0) throw new Exception("Divided by 0!");
-                return n1 / n2;
+                if (n2 == 0.0) throw new Exception("Divided by 0!");
+                num.push(n1 / n2);
+                break;
         }
-        return 0;
     }
 }