Tuesday, September 29, 2020

Evaluator Postfix expression: 6 5 2 + 3 8 7 / * 2 $ 3 *

 https://www.free-online-calculator-use.com/postfix-evaluator.html#


6 5 2 + 3 8 7 /  * 2 $ 3 *

According to my calculations, the postfix expression 6 5 2 + 3 8 7 / * 2 ^ 3 * evaluates to 35.274.


$ is also known as Power (^)




Below is my attempt to show the color-coded, step-by-step process I used to evaluate the postfix expression using the stack method.

6 5 2 + 3 8 7 / * 2 $ 3 *

The first character scanned is "6", which is an operand, so push it to the stack.

6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "5", which is an operand, so push it to the stack.

5
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "2", which is an operand, so push it to the stack.

2
5
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the right operand and then pop 5 from the stack to make the left operand.

6
 5 + 2 = 7 
StackExpression

Next, push the result of 5 + 2 (7) to the stack.

7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "3", which is an operand, so push it to the stack.

3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "8", which is an operand, so push it to the stack.

8
3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "7", which is an operand, so push it to the stack.

7
8
3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "/", which is an operator, so pop its two operands from the stack. Pop 7 from the stack for the right operand and then pop 8 from the stack to make the left operand.

3
7
6
 8 / 7 = 1.143 
StackExpression

Next, push the result of 8 / 7 (1.143) to the stack.

1.143
3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 1.143 from the stack for the right operand and then pop 3 from the stack to make the left operand.

7
6
 3 * 1.143 = 3.429 
StackExpression

Next, push the result of 3 * 1.143 (3.429) to the stack.

3.429
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "2", which is an operand, so push it to the stack.

2
3.429
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "$", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the right operand and then pop 3.429 from the stack to make the left operand.

7
6
 3.429 $ 2 = 11.758 
StackExpression

Next, push the result of 3.429 $ 2 (11.758) to the stack.

11.758
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "3", which is an operand, so push it to the stack.

3
11.758
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 3 from the stack for the right operand and then pop 11.758 from the stack to make the left operand.

7
6
 11.758 * 3 = 35.274 
StackExpression

Next, push the result of 11.758 * 3 (35.274) to the stack.

35.274
7
6
  
StackExpression

Since we are done scanning characters, the remaining element in the stack (35.274) becomes the result of the postfix evaluation.

Postfix notation: 6 5 2 + 3 8 7 / * 2 $ 3 *
Result: 35.274


No comments:

Post a Comment