-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cpp
More file actions
42 lines (42 loc) · 719 Bytes
/
eval.cpp
File metadata and controls
42 lines (42 loc) · 719 Bytes
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
#include <iostream>
using namespace std;
int s[50];
int top=-1;
push(int elem)
{
s[++top]=elem;
}
int pop()
{
return(s[top--]);
}
int main()
{
char pofx[50],ch;
int i=0,op1,op2;
cout<<"Read the Postfix Expression ";
cin>>pofx;
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch))
push(ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
}
}
}
cout<<"\n Given Postfix Expn: \n"<<pofx;
cout<<"\n Result after Evaluation: \n"<<s[top];
}