STEP 2: Program to evaluate a given postfix expression .her the stack using arrays is
implemented in separate file named stack.h
#include
#include
#include
#include
#include
/*included the header file as stack using array*/
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<”enter the postfix expression\n”;
cin>>exp;
len=strlen(exp);
exp[len]=’$’;/*append $ at the end a end marker*/
result=post(exp);
cout<<”the value of the expression is”<
getch();
exit(0);
}
double post(char exp[])
{
stk_class obj;
char ch,*type;
double result ,val,op1,op2;
int i;
i=0;
ch=exp[i];
while(ch!=’$’)
{
if(ch>=’0’&&ch<=’9’)
type=””operand”:
else if(ch==’+’||ch==’-‘||ch==’^’)
type=”operator”;
if(strcmp(type,”operand”)==0)/* if the character is
operator*/
{
val=ch-48;
obj.push(val);
}
else
if(strcmp(type,”operator”)==0)/* if it is operator*/
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case ‘+’:result=op1+op2;
break;
case ‘-‘:result=op1-op2;
break;
case ‘*;:result=op1*op2;
break;
case ‘/’: result=op1/op2;
break;
case ‘^’:result=pow(op1,op2)
break;
}/*switch*/
obj.push(result);
}
i++;
ch=exp[i];
}/*while*/
result-obj.pop();/*pop the result*/
return(result);
}