#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define max 100
float st[max];
int top=-1;
void push(float st[],float val)
{
if(top==max-1)
{
printf("\nstack overflow");
}
else
{
top++;
st[top]=val;
}
}
float pop(float st[])
{
float val=-1;
if(top==-1)
{
printf("\nStack Underflow - missing operands / insufficient postfix exp");
exit(0);
}
else
{
val=st[top];
top--;
}
return val;
}
float evaluatepostfixexp(char exp[])
{
int i=0;
float op1,op2,val;
while(exp[i] != '\0')
{
if(isdigit(exp[i]))
{
push(st,(float)(exp[i]-'0'));
}
else if(isalpha(exp[i]))
{
float a;
scanf("%f",&a);
push(st,a);
}
if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/' || exp[i]=='%')
{
op2=pop(st);
op1=pop(st);
switch(exp[i])
{
case '+':
val=op1+op2;
break;
case '-':
val=op1-op2; break;
case '*':
val=op1*op2; break;
case '/':
val=op1/op2; break;
case '%':
val=(int)op1%(int)op2; break;
}
push(st,val);
}
i++;
}
double res=pop(st);
if(top != -1)
{
printf("Insufficient POSTFIX Expression");
exit(0);
}
return res;
}
int main()
{
float val;
char exp[100];
scanf("%s",exp);
val=evaluatepostfixexp(exp);
printf("The result is : %.2f",val);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: