-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfixevaluation.cpp
More file actions
82 lines (80 loc) · 1.25 KB
/
Copy pathpostfixevaluation.cpp
File metadata and controls
82 lines (80 loc) · 1.25 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
#include<iostream>
using namespace std;
class stack
{
public:
int s[10],top=-1,max=10,val;
void push(int);
int pop();
int isoperator(char);
};
void stack::push(int value)
{
if(top==max)
cout<<"overflow condition\n";
else
top++;
s[top]=value;
val=s[top];
}
int stack::isoperator(char element)
{
if(element=='+'||element=='-'||element=='*'||element=='/'||element=='^'||element=='%')
return 1;
else
return 0;
}
int stack::pop()
{
int item;
if(top==-1)
cout<<"underflow condition\n";
else
item=s[top];
top--;
return(item);
}
int solve(int a, int b, char operator1)
{
int c;
switch(operator1)
{
case '+': c=b+a;
break;
case '-': c=b-a;
break;
case '*': c=b*a;
break;
case '/': c=b/a;
break;
case '^': c=b^a;
break;
case '%': c=b%a;
break;
default: cout<<"invalid operator present\n";
break;
}
return(c);
}
int main()
{
stack st;
string expression;
cout<<"enter arithmetic expression\n";
getline(cin,expression);
int x,y,i=0;
while(expression[i]!='\0')
{
if(st.isoperator(expression[i])!=0)
{
x=st.pop();
y=st.pop();
st.push(solve(x,y,expression[i]));
}
else
st.push(int(expression[i])-48);
i++;
}
cout<<"answer= "<<st.val<<"\n";
return 0;
}