We normally write arithmetical expressions using infix notat
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in \"4 + 5\". In postfix notation, the operator appears after its operands, as in \"4 5 +\". Here is a slightly more complex postfix expression: \"25 12 7 - 2 * /\". The equivalent infix expression is: \"25 / ((12 - 7) * 2)\". The result of that expression should be 2.5 (beware integer division). Postfix expressions don\'t require parentheses.
Write a function named postfixEval that uses a stack<double> (from the Standard Template Library) to evaluate postfix expressions. It should take a C-style string parameter that represents a postfix expression. The only symbols in the string will be +, -, *, /, digits and spaces. \'+\' and \'-\' will only appear in the expression string as binary operators - not as unary operators that indicate the sign of a number. The return type should be double. You may find the isdigit() function useful in parsing the expression. You may also use strtok() and atof().
Solution
 #include <stdio.h>
 #include <ctype.h>
 #define MAX 100
 
 float stack[MAX];
 int TOP=-1;
 int f=0;
 float pop()
 {   
 return(stack[TOP--]);
 }
 
 float push(float elem)
 {
 if(f==1){
 float num;
 num=pop();
 stack[++TOP]=elem+10*num;
 }
 else if(f==0){
 stack[++TOP]=elem;
 f=1;
 }
 }
 
 
 int main()
 {   
 char postfixExp[50],ch;
 int i=0;float a,b;
 printf(\"Enter the Postfix Expression :\");
 fgets(postfixExp,100,stdin);
 while( (ch=postfixExp[i++]) != \'\ \')
 {
 if(isdigit(ch)) push(ch-\'0\');
 else if(ch==\' \')
 f=0;
 else
 {   
 f=0;
 b=pop();
 a=pop();
 switch(ch)
 {
 case \'+\':push(a+b);break;
 case \'-\':push(a-b);break;
 case \'*\':push(a*b);break;
 case \'/\':push(a/b);break;
 default:
 printf(\"Error\ \");
 return 0;
 }
 }
 }
 printf(\"Result after evaluation: %f\ \",stack[TOP]);
 }
===============================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ gcc postfix.c
 akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
 Enter the Postfix Expression :25 12 7 - 2 * /
 Result after evaluation: 2.500000


