Referred From : PRITHVIRAJ JAIN
#include<stdio.h>
#include<ctype.h>
#include<math.h>
int top=-1,x,y,i=0,ans;
char s[100],ele,symb;
void push(int ele)
{
s[++top]=ele;
}
int pop()
{
return(s[top--]);
}
void main()
{
printf("ENTER THE POSTFIX EXPRESSION : \n");
gets(s);
while((symb=s[i++])!='\0')
{
if(isdigit(symb))
push(symb-'0');
else
{
y=pop();
x=pop();
switch(symb)
{
case '+':push(x+y);
break;
case '-':push(x-y);
break;
case '*':push(x*y);
break;
case '/':push(x/y);
break;
case '%':push(x%y);
break;
case '$':case '^':push(pow(x,y));
break;
default:push(0);
}
}
}
ans=pop();
printf("THE RESULT IS : %d\n",ans);
}
Referred From : GURUPRASAD M S
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int i,top=-1;
int op1,op2,res,stack[20];
char postfix[20],symb;
void push(int item)
{
top=top+1;
stack[top]=item;
}
int pop()
{
int item;
item=stack[top];
top=top-1;
return item;
}
void main()
{
printf("\n Enter a Valid Postfix Expression\n ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
symb=postfix[i];
if(isdigit(symb))
{
push(symb-'0');
}
else
{
op2=pop();
op1=pop();
switch(symb)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
case '%':push(op1%op2);break;
case '$':
case '^':push(pow(op1,op2));break;
default: push(0);
}
}
}
res=pop();
printf("\n Result = %d\n",res);
}