Use the AStack class defined in Page 122 to implement the below expression evaluation algorithm in the main() function. In addition to the final evaluation result, you also need to print out contents of the two stacks each time when any stack's content changes. Please hard code "2 * 3 - 48/4 4 * 5" in your implementation as the input to the algorithm and generate the outputs.
Algorithm for Parsing Arithmetic Expression
Two stacks are required, one for numbers and the other for operators. The algorithm is:
For each item in the infix expression (no parenthesis) from the left to the right:
If the item is a number then push it on the number stack and go to pick a new item (back to the 'for' loop);
While the item is an operator (+,-,, or /) and the operator stack is not empty and the operator on the top of the stack is not lower in priority than the item (+ or -are lower in priority than and /), then
Pop an operator from the operator stack.
Pop two numbers off the number stack.
Calculate using second number-operator-first number.
Push the result on the number stack.
Push the item on the operator stack.
After the loop, while the operator stack is not empty
Pop an operator from the operator stack.
Pop two numbers off the number stack.
Calculate using second number-operator-first number.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.