forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_postfix_expression.cpp
52 lines (46 loc) · 1.05 KB
/
evaluate_postfix_expression.cpp
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
#include<bits/stdc++.h>
using namespace std;
int main(){
stack <int> stack1;
char s[100];
cout<<"enter a postfix expression";
cin>>s;
int i=0;
int n1,n2,a;
while(s[i])
{
a=s[i];
if(isdigit(a))
{
a=s[i]-48;
stack1.push(a);
}
else{
n1=stack1.top();
stack1.pop();
n2=stack1.top();
stack1.pop();
switch(a)
{
case '+':
a=n1+n2;
stack1.push(a);
break;
case '-':
a=n2-n1;
stack1.push(a);
break;
case '*':
a=n1*n2;
stack1.push(a);
break;
case '/':
a=n2/n1;
stack1.push(a);
break;
}
}
i++;
}
cout<<stack1.top();
}