evalution of reverse polish formulas
- examine each symbol in the reverse polish formula, starting at the extreme left untill you come to an operator.
- write down the operator and the two operands immediately to its left on a piece of scratch paper.
- erase the operator and the operands from the formula,creating a hole.
- perform the operation on the operands and write the result in the hole.
- if the formula now consist of one value , that is the answer and the algorithm is finished otherwise goto step 1depicts the evaluation of a reverse polish formula. the order of the operator is the order in which they are actually evaluated.
- reverse polish is the ideal notation for evaluting formulas on a computer with stack.the formulas comsist os n symbols each one either a variable or an operator.the algorithm for evaluting a reverse polish formula using a stack is as follows.
- set k to 1
- examine the kth symbol. of it is a variable, push it onto the stack. if it is an operator pop the top 2 iteams off the stack, perform the operation and push the result back onto the stack.
- if k=n the algorithm terminates and the answer is on the stack; other wise add 1 to k and goto step 2.