-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostfixNotationCalculator.java
58 lines (52 loc) · 2.02 KB
/
PostfixNotationCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package calculator;
import java.math.BigInteger;
import java.util.Stack;
/*
* Calculate value of postfix notation expression.
*/
public class PostfixNotationCalculator {
protected static String calculatePostfixNotation(String postfixData) {
Stack<String> stack = new Stack<>();
for (String s : postfixData.split(" ")) {
if (s.matches("^([+\\-])?[A-Za-z\\d]+")) {
stack.push(s);
} else {
BigInteger num1;
String stackValue1 = stack.pop();
try {
num1 = new BigInteger(stackValue1);
} catch (Exception e) {
num1 = Main.data.getOrDefault(stackValue1, InputProcessor.defaultValue);
if (num1 == InputProcessor.defaultValue) {
System.out.println(InputProcessor.unknownVariable);
break;
}
}
BigInteger num2;
String stackValue2 = stack.pop();
try {
num2 = new BigInteger(stackValue2);
} catch (Exception e) {
num2 = Main.data.getOrDefault(stackValue2, InputProcessor.defaultValue);
if (num2 == InputProcessor.defaultValue) {
System.out.println(InputProcessor.unknownVariable);
break;
}
}
stack.push(calculate(num1, num2, s));
}
}
return stack.pop();
}
private static String calculate(BigInteger num1, BigInteger num2, String operator) {
BigInteger result = BigInteger.ZERO;
switch (operator) {
case "+" -> result = num2.add(num1);
case "-" -> result = num2.subtract(num1);
case "*" -> result = num2.multiply(num1);
case "/" -> result = num2.divide(num1);
case "^" -> result = num2.pow(num1.intValue());
}
return String.valueOf(result);
}
}