-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVALUATI.BAK
77 lines (75 loc) · 1.16 KB
/
EVALUATI.BAK
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
#include<iostream.h>
#include<conio.h>
#define max 20
#include<string.h>
float stack[max];
int top=-1;
void push(float x)
{
if(top==(max-1) )
cout<<"Overflow";
else
{
stack[++top]=x;
}
}
float pop()
{
if(top==-1)
{ cout<<"Invalid expression";
return -1;
}
else
return (stack[top--]);
}
void main()
{
char a[]={"231*+9-d"};
int i,j,p1,p2;
float e1,e2,sum;
int s=sizeof(a)/sizeof(a[0]);
clrscr();
s=s-1;
for(i=0;i<s;i++)
{
if(a[i]>='0' && a[i]<='9')
{
push(a[i]-'0');
}
else if(a[i]=='*')
{
e2=pop();
e1=pop();
push(e1*e2);
}
else if(a[i]=='+')
{
e2=pop();
e1=pop();
sum=e1+e2;
push(sum);
}
else if(a[i]=='-')
{
e2=pop();
e1=pop();
push(e1-e2);
}
else if(a[i]=='/')
{
e2=pop();
e1=pop();
push(e1/e2);
}
else if(a[i]=='^')
{
p1=pop();
p2=pop();
push(p2^p1);
}
else
cout<<"Error in expression\n";
}
cout<<"Value="<<stack[top];
getch();
}