Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate Reverse polish notation using stacks

Can this postfix expression can be evaluated?

6 2 3 + - 3 8 2 / + * 2 5 3 +
like image 745
Zeeshan Asif Avatar asked Oct 30 '16 12:10

Zeeshan Asif


People also ask

How do you interpret reverse polish notation?

In reverse Polish notation, the operators follow their operands; for instance, to add 3 and 4 together, one would write 3 4 + rather than 3 + 4.

What is meant by Polish notation in stack?

Polish notation is a notation form for expressing arithmetic, logic and algebraic equations. Its most basic distinguishing feature is that operators are placed on the left of their operands. If the operator has a defined fixed number of operands, the syntax does not require brackets or parenthesis to lessen ambiguity.

Why is reverse polish notation RPN used to carry out the evaluation of expressions?

Reverse Polish notation (RPN) is a method for conveying mathematical expressions without the use of separators such as brackets and parentheses. In this notation, the operators follow their operands, hence removing the need for brackets to define evaluation priority.


1 Answers

Yes, it can.

S = new empty stack
while not eof
    t = read token
    if t is a binary operator
        y = pop(S)
        x = pop(S)
        push(S, t(x, y))
    else
        push(S, t)
print the contents of the stack S
like image 159
Yakov Galka Avatar answered Sep 19 '22 18:09

Yakov Galka