-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackMathematicsInterpreter.java
115 lines (102 loc) · 3.31 KB
/
StackMathematicsInterpreter.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* @Name: My Mathematics Interpreter Implementation In Java
* @Author: Max Base
* @Date: 2022-10-31
* @Repository: https://github.com/BaseMax/StackMathematicsInterpreter
*/
import java.util.Stack;
class StackMathematicsInterpreter {
private String input;
private Stack<Character> stack;
private Stack<Integer> tokens;
public Interpreter(String str) {
this.input = str;
this.stack = new Stack<Character>();
this.tokens = new Stack<Integer>();
this.parse();
}
private void parse() {
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == ' ') {
continue;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (stack.peek() != '(') {
int b = tokens.pop();
int a = tokens.pop();
char op = stack.pop();
int result = calculate(a, b, op);
tokens.push(result);
}
stack.pop();
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (!stack.isEmpty() && hasPrecedence(c, stack.peek())) {
int b = tokens.pop();
int a = tokens.pop();
char op = stack.pop();
int result = calculate(a, b, op);
tokens.push(result);
}
stack.push(c);
} else {
// number
int number = 0;
while (i < input.length() && Character.isDigit(input.charAt(i))) {
number = number * 10 + (input.charAt(i) - '0');
i++;
}
i--;
tokens.push(number);
}
}
while (!stack.isEmpty()) {
int b = tokens.pop();
int a = tokens.pop();
char op = stack.pop();
int result = calculate(a, b, op);
tokens.push(result);
}
}
private boolean hasPrecedence(char op1, char op2) {
if (op2 == '(' || op2 == ')') {
return false;
}
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return false;
} else {
return true;
}
}
private int calculate(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0) {
throw new UnsupportedOperationException("Cannot divide by zero");
}
return a / b;
}
return 0;
}
public int getResult() {
return tokens.pop();
}
}
public class HelloWorld {
public static void main(String []args){
System.out.println("Hello, World!");
Interpreter interpreter = new StackMathematicsInterpreter("1+2*3");
System.out.println(interpreter.getResult());
interpreter = new Interpreter("1+2*3+4");
System.out.println(interpreter.getResult());
interpreter = new Interpreter("1+2*3+4*5");
System.out.println(interpreter.getResult());
}
}