Write an algorithm to calculate a postfix expression. Execute your algorithm using the given postfix expression as your input : a b + c d +*f ↑ .
To evaluate a postfix expression by using the given values:
clear the stack
symb = next input character while(not end of input)
{
if (symb is an operand)
push onto the stack;
else
{
pop two operands from the stack;
result=op1 symb op2;
push result onto the stack;
}
symb = next input character;
}
return (pop (stack));
The input postfix expression is given as:- ab+cd+*f^
Symb Stack Evaluation a a
b a b
+ pop a and b (a + b) Push (a + b)
c (a + b) c
d (a + b) c d
+ pop c and d (c + d) Push (c + d)
* pop (a + b) and (c + d) (a + b) * (c + d)
f (a + b) * (c + d) f
^ pop (a + b) * (c + d) and f (a + b) * (c + d) ^ f
The result of evaluation is ((a + b) * (c + d)) ^ f