Question: Write a Java program that evaluates expressions containing parenthesis ('(', ), addition, subtraction, multiplication, and division. Hint. Here is a possible pseudo-code to achieve that:
Write a Java program that evaluates expressions containing parenthesis ('(', ), addition, subtraction, multiplication, and division. Hint. Here is a possible pseudo-code to achieve that: The program scans the expression from left to right to extract operands, operators, and the parentheses. 1. 1. If the extracted item is an operand, push it to operandStack. 2. If the extracted item is a +or - operator, process all the operators at the top of operatorStack, push the extracted operator to operatorStack. 3. If the extracted item is a or ' operator, process theor operators at the top of operatorStack, push the extracted operator to operatorStack. 4. If the extracted item is a symbol, push it to operatorStack. 5. If the extracted item is a ' symbol, repeatedly process the operators from the top of operatorStack until seeing the (" symbol on the stack. 2. Repeatedly process the operators from the top of operatorStack until operatorStack is empty Note. "process' above means evaluate the operator from the operatorStack by doing the operation] between the top two operands from the operandStack. The table below shows how the algorithm is applied to evaluate the expression (1 + 2) 4-3. Scan Action peradStack 1+2) 4-3 Phase 1.4 (1+2) 4-3 1Phase 1.11 (1+2)*4-3Phase 1.2 1 (1+2)*4-3 2 Phase1.1 2 1 +2*4.3 Phase 153 (1+2) 4-3Phase 1.3 (1+2) 4-34Phase 1.1 4 3 Phase 1,21 a+2)*4. 3 Phase 1.1 12
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
