//Evaluating Postfix Expression
#include <stdio.h>
#include <ctype.h>
#define MAX 20
typedef struct
{
int data[MAX];
int top;
}STACK;
void initstack(STACK *ps)
{
ps->top = -1;
}
void push(STACK *ps, int num)
{
ps->top++;
ps->data[ps->top]=num;
}
int pop(STACK *ps)
{
return(ps->data[ps->top--]);
}
int isempty(STACK *ps)
{
return(ps->top == -1);
}
int isfull(STACK *ps)
{
return(ps->top == MAX-1);
}
void main()
{
void Eval(char post[]);
char post[20];
printf(">>Evaluating Postfix Expression<<\n");
printf("Enter The Postfix Expression:\t");
scanf("%s", post);
Eval(post);
}
void Eval(char post[])
{
int value, i, op1, op2;
STACK s1;
initstack(&s1);
for(i=0; post[i]!='\0'; i++)
{
if(isalpha(post[i]))
{
printf("Enter The Value Of %c: \t", post[i]);
scanf("%d", &value);
push(&s1, value);
}
else
{
op2=pop(&s1);
op1=pop(&s1);
switch(post[i])
{
case '+':
push(&s1, op1+op2);
break;
case '-':
push(&s1, op1-op2);
break;
case '*':
push(&s1, op1*op2);
break;
case '/':
push(&s1, op1/op2);
break;
case '%':
push(&s1, op1%op2);
break;
default:
printf("Enter Value The Value Of");
scanf("%d", &value);
push(&s1, value);
break;
}
}
}
printf("The Result Is: %d \n", pop(&s1));
}