-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve_PostFix_STACK.java
More file actions
44 lines (34 loc) · 1.04 KB
/
Copy pathsolve_PostFix_STACK.java
File metadata and controls
44 lines (34 loc) · 1.04 KB
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
import java.util.Stack;
public class solve_PostFix_STACK {
static void stackPostfix(String pf) {
Stack<Integer> st = new Stack<>();
for (int i = 0; i < pf.length(); i++) {
char c = pf.charAt(i);
if (Character.isDigit(c))
st.push(c - '0');
else {
int v1 = st.pop();
int v2 = st.pop();
switch (pf.charAt(i)) {
case '+': {
st.push(v2 + v1);
break;
}
case '-': {
st.push(v2 - v1);
break;
}
case '*': {
st.push(v2 * v1);
break;
}
case '/': {
st.push(v2 / v1);
break;
}
}
}
}
System.out.println(st);
}
}