Postfix Program
Description
This program uses a stack to evaluate a postfix expression and calculate the result.Associated Concepts:
Stack
Algorithm:
main method:
calculate method definition:
Create a stack object.
Input a postfix expression with a blank space to separate each token.
Split each token into an array.
In a loop, retrieve each token from the array.
Input a postfix expression with a blank space to separate each token.
Split each token into an array.
In a loop, retrieve each token from the array.
If the token is an operator (+, -, *, /):
Otherwise (the token is an operand):
After the loop is ended, print the result.
Pop two objects from the stack.
Call calculate() method to get the result.
Push the result back to the top of the stack.
Call calculate() method to get the result.
Push the result back to the top of the stack.
Otherwise (the token is an operand):
Push it to the top of the stack.
calculate method definition:
Use a switch structure to calculate the result
based on the operator.
Return the result.
Return the result.