-
Notifications
You must be signed in to change notification settings - Fork 0
/
EVALPOST.CPP
87 lines (73 loc) · 1.18 KB
/
EVALPOST.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
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
#include <iostream.h>
#include <conio.h>
#include <string.h>
class MyStack
{
int a[100];
int top, m;
public:
MyStack(int max)
{
m=max;
top=-1;
}
void push(int key)
{
a[++top]=key;
}
int pop()
{
return(a[top--]);
}
};
class Evaluation
{
public:
int calculate(char s[]);
};
int Evaluation::calculate(char s[])
{
int n,r=0;
n=strlen(s);
MyStack a(n);
for(int i=0;i<n; i++)
{
char ch=s[i];
if(ch>='0'&& ch<='9')
a.push((int)(ch-'0'));
else
{
int x=a.pop();
int y=a.pop();
switch(ch)
{
case '+':r=x+y;
break;
case '-':r=y-x;
break;
case '*':r=x*y;
break;
case '/':r=y/x;
break;
default:r=0;
}
a.push(r);
}
}
r=a.pop();
return(r);
}
void main()
{
clrscr();
Evaluation e;
char input[99];
while(1)
{
cout<<"\nEnter the postfix expresion. To escape, enter the word'exit'."<<endl;
cin>>input;
if(strcmpi(input,"exit")==0)
break;
cout<<"\nResult: "<<e.calculate(input)<<endl;
}
}