img
Question:
Published on: 19 April, 2024

Evaluate the following postfix expression:

4, 5, 4, 2, ^, +, *, 2, 2, ^, 9, 3, /, *, -

Write a pseudo code for evaluate postfix expression.

[3+2]

Answer:
4 ,5, 4, 2, ^, +, *, 2, 2, ^, 9, 3, /, *, -
→ 4, 5, 4^2, +, *, 2^2, 9/3, *, -
→ 4, 5, 16, +, *, 4, 3, *, -
→ 4, 5+16, *, 3*4, -
→ 4, 21, *, 12, -
→ 4*21, 12, -
→ 84-12
→ 72

 

Pseeduo code:

while tokens remain:

read the next token.

if the token is an operator:

pop the top two elements of the stack.

perform the operation on the elements.

push the result of the operation onto the stack.

else:

push the token (which must be a number)onto the stack

Random questions