-
Notifications
You must be signed in to change notification settings - Fork 17
/
EvalPostfix.java
41 lines (39 loc) · 1.1 KB
/
EvalPostfix.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
package SummerTrainingGFG.Stack;
import java.util.Stack;
/**
* @author Vishal Singh
*/
public class EvalPostfix {
static int calculate(char ch, int a, int b){
if (ch == '/'){
return a/b;
}else if (ch == '*'){
return a*b;
}else if (ch == '+'){
return a+b;
}else{
return a-b;
}
}
static int evaluatePostFix(String exp){
Stack<Integer> temp = new Stack<>();
int eval = 0;
for (int i = 0; i < exp.length(); i++) {
char currChar = exp.charAt(i);
// Not from these then -1
if ("+-*/".indexOf(currChar) == -1){
temp.push(Integer.parseInt(String.valueOf(exp.charAt(i))));
}else {
int first = temp.pop();
int second =temp.pop();
int res = calculate(currChar,second,first);
temp.push(res);
}
}
return temp.pop();
}
public static void main(String[] args) {
String exp = "231*+9-";
System.out.println(evaluatePostFix(exp));
}
}