Question: in Java - simple code please all in main method Calculator thing input inut is the token that going to like something (2 + 4
in Java - simple code please all in main method
Calculator thing
input inut is the token that going to like something (2 + 4 / 6 * 2 - (10+ (2-5) +10) * 2)
1. While there are still tokens to be read in,
1.1 Get the next token.
1.2 If the token is:
1.2.1 A number: push it onto the value stack.
1.2.2 A left parenthesis: push it onto the operator stack.
1.2.3 A right parenthesis:
1. While the thing on top of the operator stack is not a
left parenthesis,
1. Pop the operator from the operator stack.
2. Pop the value stack twice, getting two operands.
3. Apply the operator to the operands, in the correct order.
4. Push the result onto the value stack.
2. Pop the left parenthesis from the operator stack, and discard it.
1.2.4 An operator (call it thisOp):
1. While the operator stack is not empty, and the top thing on the
operator stack has the same or greater precedence as thisOp,
1. Pop the operator from the operator stack.
2. Pop the value stack twice, getting two operands.
3. Apply the operator to the operands, in the correct order.
4. Push the result onto the value stack.
2. Push thisOp onto the operator stack.
2. While the operator stack is not empty,
1 Pop the operator from the operator stack.
2 Pop the value stack twice, getting two operands.
3 Apply the operator to the operands, in the correct order.
4 Push the result onto the value stack.
3. At this point the operator stack should be empty, and the value
stack should have only one value in it, which is the final result.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
