Week 7 programs based on implementation of stack
//C Program to Evaluate POSTFIX Expression Using Stack
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
Stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf(“Enter the expression :: “);
Scanf(“%s”,exp);
E = exp;
while(*e != ‘\\0’)
{
if(isdigit(*e))
{
Num = *e – 48;
Push(num);
}
else
{
N1 = pop();
N2 = pop();
Switch(*e)
{
case ‘+’:
{
N3 = n1 + n2;
Break;
}
case ‘-‘:
{
N3 = n2 – n1;
Break;
}
case ‘*’:
{
N3 = n1 * n2;
Break;
}
case ‘/’:
{
N3 = n2 / n1;
Break;
}
}
Push(n3);
}
E++;
}
printf(“\\nThe result of expression %s = %d\\n\\n”,exp,pop());
return 0;
}