Asked By : Matt Scott
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/4666
Answered By : Ravindra Bagale
computer uses stack for evaluate the arithmetic expressions
suppose following expression
3+4
in computer stack, it pushesh the element in following order
3 4 +
i.e. top pointer points to + symbol
+ //top pointer points here 4 3
when computer starts to evaluate, it first pop the operator +, here computer checks, whether it is operator or any other characters, if it is operator then understand that it has to do addition operation, then it pop the two operands for arithmetic addition, i.e. these are the next elements of the stack. that is 4 & 3
here pop order is + 4 3
in short for solving complex expressions reverse polish notation is used If there are multiple operations, the operator is given immediately after its second operand; so the expression written “3 − 4 + 5” in conventional infix notation would be written “3 4 − 5 +”in RPN: first subtract 4 from 3, then add 5 to that.
An advantage of RPN is that it obviates the need for parentheses that are required by infix. While “3 − 4 * 5″` can also be written “3 − (4 * 5)”, that means something quite different from “(3 − 4) * 5”. In postfix, the former could be written “3 4 5 * −”, which unambiguously means “3 (4 5 *) −” which reduces to “3 20 −”; the latter could be written “3 4 – 5 *” (or 5 3 4 – *, if you wish to keep similar formatting), which unambiguously means “(3 4 -) 5 *”.