title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
Algorithm4 Java Solution 1.3.11 |
2019-07-04 05:47:10 +0800 |
false |
|
|
Write a program EvaluatePostfix that takes a postfix expression from standard input, evaluates it, and prints the value. (Piping the output of your program from the previous exercise to this program gives equivalent behavior to Evaluate.
create a stack stores doubles
read expression from left to right
push number into stack
if reads an operator
then pop two numbers from the stack, then compute the result
op1 op op2, push the result back to stack, until to the end of string expr
finally, pop the last number in the stack, it is the final result.