Exp No 3 Evaluate Postfix Expression Using ADt
Exp No 3 Evaluate Postfix Expression Using ADt
h>
#include<ctype.h>
# define MAX 100
# define POSTFIXSIZE 100
int stack[MAX];
int top = -1 ;
void push(int n)
{
if(top >= MAX-1)
{
printf("stack overflow");
return;
}
else
{
top = top + 1 ;
stack[top]=n;
}
}
int pop()
{
int n;
if(top <0)
{
printf("stack underflow");
}
else
{
n = stack[top];
top = top - 1;
return n;
}
}
push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
push(val);
}
}
printf( " \n Result of expression evaluation : %d \n", pop()) ;
}
int main()
{
int i ;
char postfix[POSTFIXSIZE];
printf("ASSUMPTION: There are only four operators(*, /, +, -) in an
expression and operand is single digit only.\n");
printf( " \nEnter postfix expression,\n press right parenthesis ')' for end
expression : ");
EvalPostfix(postfix);
return 0;
}