-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPractical 3-1.java
46 lines (40 loc) · 1.15 KB
/
Practical 3-1.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
import java.util.Stack;
public class Solution
{
static int EvalPost(String expr)
{
Stack<Integer> stk=new Stack<>();
for(int i=0;i<expr.length();i++)
{
char c=expr.charAt(i);
if(Character.isDigit(c))
stk.push(c - '0');
else
{
int a = stk.pop();
int b = stk.pop();
switch(c)
{
case '+':
stk.push(b+a);
break;
case '-':
stk.push(b- a);
break;
case '/':
stk.push(b/a);
break;
case '*':
stk.push(b*a);
break;
}
}
}
return stk.pop();
}
public static void main(String[] args)
{
String exp="231*+9-"; //Test Case
System.out.println("postfix evaluation: "+EvalPost(exp));
}
}