|
@@ -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();
|
|
char[] exp = str.toCharArray();
|
|
Stack<Character> ops = new Stack<>();
|
|
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 (i == exp.length) {
|
|
if (ops.isEmpty()) break;
|
|
if (ops.isEmpty()) break;
|
|
if (ops.peek() == '(') throw new Exception("Invalid expression!");
|
|
if (ops.peek() == '(') throw new Exception("Invalid expression!");
|
|
- num.push(operate(ops.pop(), num.pop(), num.pop()));
|
|
|
|
|
|
+ operate(ops, num);
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
switch (exp[i]) {
|
|
switch (exp[i]) {
|
|
@@ -34,14 +34,14 @@ public class Calculator {
|
|
if (ops.isEmpty() || ops.peek() == '(') {
|
|
if (ops.isEmpty() || ops.peek() == '(') {
|
|
ops.push(exp[i++]);
|
|
ops.push(exp[i++]);
|
|
} else {
|
|
} else {
|
|
- num.push(operate(ops.pop(), num.pop(), num.pop()));
|
|
|
|
|
|
+ operate(ops, num);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
case '*': case '/':
|
|
case '*': case '/':
|
|
if (ops.isEmpty() || ops.peek() == '(' || ops.peek() == '+' || ops.peek() == '-') {
|
|
if (ops.isEmpty() || ops.peek() == '(' || ops.peek() == '+' || ops.peek() == '-') {
|
|
ops.push(exp[i++]);
|
|
ops.push(exp[i++]);
|
|
} else {
|
|
} else {
|
|
- num.push(operate(ops.pop(), num.pop(), num.pop()));
|
|
|
|
|
|
+ operate(ops, num);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
case '(':
|
|
case '(':
|
|
@@ -53,13 +53,13 @@ public class Calculator {
|
|
ops.pop();
|
|
ops.pop();
|
|
i++;
|
|
i++;
|
|
} else {
|
|
} else {
|
|
- num.push(operate(ops.pop(), num.pop(), num.pop()));
|
|
|
|
|
|
+ operate(ops, num);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
int j;
|
|
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);
|
|
num.push(n);
|
|
i = j;
|
|
i = j;
|
|
}
|
|
}
|
|
@@ -68,18 +68,24 @@ public class Calculator {
|
|
return num.peek();
|
|
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) {
|
|
switch (op) {
|
|
case '+':
|
|
case '+':
|
|
- return n1 + n2;
|
|
|
|
|
|
+ num.push(n1 + n2);
|
|
|
|
+ break;
|
|
case '-':
|
|
case '-':
|
|
- return n1 - n2;
|
|
|
|
|
|
+ num.push(n1 - n2);
|
|
|
|
+ break;
|
|
case '*':
|
|
case '*':
|
|
- return n1 * n2;
|
|
|
|
|
|
+ num.push(n1 * n2);
|
|
|
|
+ break;
|
|
case '/':
|
|
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;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|