-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEvaluateExpression.java
48 lines (42 loc) · 1.09 KB
/
EvaluateExpression.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
package Stacks;
import java.util.ArrayList;
import java.util.Stack;
/**
* Author - archit.s
* Date - 24/10/18
* Time - 11:58 PM
*/
public class EvaluateExpression {
public int evalRPN(ArrayList<String> A) {
Stack<Integer> s = new Stack<>();
for (String aA : A) {
int t2;
int t1;
switch (aA) {
case "+":
t2 = s.pop();
t1 = s.pop();
s.push(t1 + t2);
break;
case "-":
t2 = s.pop();
t1 = s.pop();
s.push(t1 - t2);
break;
case "*":
t2 = s.pop();
t1 = s.pop();
s.push(t1 * t2);
break;
case "/":
t2 = s.pop();
t1 = s.pop();
s.push(t1 / t2);
break;
default:
s.push(Integer.valueOf(aA));
}
}
return s.pop();
}
}