Question: Convert this Algorithm to Java Code Algorithm evaluateInfix(infix) 7Evaluates an infix expression operatorStack = a new empty stack valueStack a new empty stack while (infix


Convert this Algorithm to Java Code
Algorithm evaluateInfix(infix) 7Evaluates an infix expression operatorStack = a new empty stack valueStack a new empty stack while (infix has characters left to process) nextCharacter = next nonblank character of infix switch (nextCharacter) case variable: valueStack.push (value of the variable nextCharacter) break case 'A' : operatorStack.push (nextCharacter) break case '+: case-": :case case while (!operatorStack.isEmptyO and precedence of nextCharacter precedence of operatorStack.peekO) /7Execute operator at top of operatorStack topOperator-operatorStack.pop O operandTwovalueStack.popO operandOne valueStack.pop O result the result of the operation in topOperator and its operands operandOne and operandTwo valueStack.push (result) operatorStack.push (nextCharacter) break case '(' operatorStack.push (nextCharacter) break case': // Stack is not empty if infix expression is valid topOperator-operatorStack.popO while (topope rator != '(') operandTwovalueStack.pop O operandOne valueStack.popO result-the result of the operation in topOperator and its operands operandOne and operandTwo valueStack.push (result) topOperator operatorStack.pop O break default: break // Ignore unexpected characters while CloperatorStack.isEmptyO topOperator-operatorStack.popO operandTwo valueStack.pop O operandOne valueStack.pop O result the result of the operation in topOperator and its operands operandOne and operandTwo valueStack.push(result) return valueStack.peekO Algorithm evaluateInfix(infix) 7Evaluates an infix expression operatorStack = a new empty stack valueStack a new empty stack while (infix has characters left to process) nextCharacter = next nonblank character of infix switch (nextCharacter) case variable: valueStack.push (value of the variable nextCharacter) break case 'A' : operatorStack.push (nextCharacter) break case '+: case-": :case case while (!operatorStack.isEmptyO and precedence of nextCharacter precedence of operatorStack.peekO) /7Execute operator at top of operatorStack topOperator-operatorStack.pop O operandTwovalueStack.popO operandOne valueStack.pop O result the result of the operation in topOperator and its operands operandOne and operandTwo valueStack.push (result) operatorStack.push (nextCharacter) break case '(' operatorStack.push (nextCharacter) break case': // Stack is not empty if infix expression is valid topOperator-operatorStack.popO while (topope rator != '(') operandTwovalueStack.pop O operandOne valueStack.popO result-the result of the operation in topOperator and its operands operandOne and operandTwo valueStack.push (result) topOperator operatorStack.pop O break default: break // Ignore unexpected characters while CloperatorStack.isEmptyO topOperator-operatorStack.popO operandTwo valueStack.pop O operandOne valueStack.pop O result the result of the operation in topOperator and its operands operandOne and operandTwo valueStack.push(result) return valueStack.peekO
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
