forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix.java
More file actions
17 lines (17 loc) · 542 Bytes
/
Postfix.java
File metadata and controls
17 lines (17 loc) · 542 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Postfix {
// example of using a stack
public static void main(String[] args) {
Stack<Integer> stacky = new Stack<>();
for (char ch : "123+45*6-+-".toCharArray()) {
if (ch == '+')
stacky.push(stacky.pop() + stacky.pop());
else if (ch == '*')
stacky.push(stacky.pop() * stacky.pop());
else if (ch == '-')
stacky.push(-stacky.pop() + stacky.pop());
else
stacky.push(ch-'0');
}
System.out.println(stacky.pop());
}
}