Tuesday, September 27, 2016

Postfix Evaluation

Tags

Program To evaluate Postfix Expression:

#include<iostream>
#include<ctype.h>  //for isalnum function
using namespace std;
int stack[100];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{

return (stack[top--]);
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
if(isdigit(c))
push(c-'0');  //for converting datatype
else
{
x=pop();
y=pop();
switch(c)
{
case'+':
push(x+y);
    break;
    case '-':
    push(y-x);
    break;
   case '*':
push(y*x);
break;
case '/':
push(y/x);
break;
}
}
}
cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}