forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluationofPostFixExpression.c
76 lines (71 loc) · 1.2 KB
/
EvaluationofPostFixExpression.c
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
#include<bits/stdc++.h>
using namespace std;
struct Stack{
int *ar;
int top;
int cap;
Stack(int c){
cap=c;
ar=new int[cap];
top=-1;
}
void push(int x)
{
if(top>=cap-1)
{
cout<<"Stack Overflow"<<endl;
}
else
{
top++;
ar[top]=x;
}
}
int pop()
{
int ans=ar[top];
top--;
return ans;
}
int Top()
{
return ar[top];
}
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
};
int main(){
string st;
cout<<"Enter the string : ";
getline(cin,st);
int n=st.length();
Stack s(n);
int num=0;
for(int i=0;i<n;i++)
{
if(st[i]==' ')
{
s.push(num);
num=0;
}
else if(((st[i]-'0')>=0)&&((st[i]-'0')<=9))
num=num*10+(st[i]-'0');
else
{
int sd=s.pop();
int fd=s.pop();
if(st[i]=='+') s.push(fd+sd);
if(st[i]=='-') s.push(fd-sd);
if(st[i]=='*') s.push(fd*sd);
if(st[i]=='/') s.push(fd/sd);
if(st[i]=='%') s.push(fd%sd);
i++;
}
}
cout<<s.Top();
}