#include<stdio.h>
#define Maxsize 20
void push(int num);
int pop();
int top=-1;
int stack [Maxsize];
void main()
{
char exp[50];
int i=0,op1,op2,result;
printf("Enter the expression:");
gets (exp);
while(exp[i]!='\0')
{
if (isdigit(exp[i]))
push(exp[i]-'0');
else
{ switch (exp[i])
{
case '+' :
op2 = pop();
op1 = pop();
result = op1+op2;
break;
case '-' :
op2 = pop();
op1 = pop();
result= op1-op2;
break;
case '*' :
op2 = pop();
op1 = pop();
result = op1*op2;
break;
case '/' :
op2 = pop();
op1 = pop();
result = op1/op2;
break;
}
push (result);
}
i++;
}
if(top>0)
{
printf("error");
}
else
{
printf("result = %d",pop());
}
}
void push(int num)
{
top++;
stack[top]=num;
}
int pop()
{
int x = stack[top];
top--;
return x;
}
Note: Need to be arranged in compiler after copied
OutPut:
Enter the expression:34+5*
result = 35