C++ PROGRAM FOR EVALUATION OF EXPRESSION

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
class post
{
public:
char st[20],ch;
int c[20],n,top;
void eval(void);
int res(char,int,int);
};
int post::res(char ch,int c,int c1)
{
int r;
switch(ch)
{
case'+':
r=c+c1;
break;
case'-':
r=c-c1;
break;
case'*':
r=c*c1;
break;
case'/':
r=c/c1;
break;
case'^':
r=c^c1;
break;
}
return(r);
}
void post::eval(void)
{
cout<<endl<<"EVALUATION OF POSTFIX EXPRESSION";
cout<<endl<<"enter the postfix expression";
cin>>st;
cout<<st;
n=strlen(st);
for(int i=0;i<n;i++)
{
ch=st[i];
if(isdigit(ch))
{
c[++top]=ch-48;
}
else
if(isalpha(ch))
{
cout<<endl<<"enter the value"<<ch<<"";
cin>>ch;
cout<<ch;
c[++top]=ch-48;}
else
{
top=top-2;
c[top+1]=res(ch,c[top+1],c[top+2]);
top++;
}
}
cout<<endl<<"THE RESULT POF EVALUATION:"<<c[top];
}
int main()
{
clrscr();
post p;
p.eval();
getch();
return(0);
}

OUTPUT:

EVALUATION OF POSTFIX EXPRESSION
enter the postfix expression
12+
12+
THE RESULT POF EVALUATION:3












































No comments:

Post a Comment