-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_evln.java
More file actions
89 lines (67 loc) · 1.64 KB
/
post_evln.java
File metadata and controls
89 lines (67 loc) · 1.64 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.*;
class post_evln {
private int max;
private int[] arr;
private int top;
private int a;
private int b;
private int r;
public post_evln(int s){
max=s;
arr= new int[max];
top=-1;
}
public void push(int item) {
top++;
arr[top]=item;
//arr[++top];
}
public int pop() {
int item = arr[top];
top--;
return item;
//return arr[top--];
}
public void evaluvation(char x)
{
if (x=='+'||x=='-'||x=='*'||x=='/'||x=='$')
{
a=pop();
b=pop();
switch(x)
{
case '+': push(b+a);
break;
case '-': push(b-a);
break;
case '*': push(b*a);
break;
case '/' : push(b/a);
break;
case '$' :push((int) Math. round(Math.pow(b, a)));
break;
}
}
else
{
int a=Integer.parseInt(String.valueOf(x));
push(a);
}
}
public static void main(String args[])throws IOException
{
String str;
int l,i,result;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter the Postfix String");
// x = Integer.parseInt(in.readLine());
str=in.readLine();
l = str.length();
post_evln obj = new post_evln(l);
for(i=0;i<l;i++)
obj.evaluvation(str.charAt(i));
result = obj.pop();
System.out.println(result);
}
}