Repeat Programming Problem 5, but use the following algorithm to evaluate an infix expressioninfixExp. The algorithm uses two stacks: One stack,opstack, contains operators, and the other stack,vaistack, contains values of operands and intermediate results. Note that the algoritiim treats parentheses as operators widi the lowest precedence.
for(each character ch in infixExp) {
switch (ch) {
case ch is an operand, that is, a digit
valstack.push(ch)
break
case ch is '('
opStack.push(ch)
break
case ch is an operator
if (opStack.isEmptyf)) {
opStack.push(ch)
}
else if (precedence(ch) <
precedence(top of opStack)) {
opStack.push(ch)
}
else {
while (!opStack.isEmpty() and
precedence(ch) ℁precedence(top of opStack)) {
Execute
} // endwhile
opStack.push(ch)
} // endif
break
case ch is ')'
while (top of opStack is not '(') {
Execute
} // endwhile
opStack.pop() break
} // endswitch
} // end for
while (!opStack.isEmpty()) {
Execute
} //end while
result =valstack.peek()
Note thatExecute means
operand2 =valstack.pop()
operandl =valstack.pop()
op =opStack.pop()
result =operandl op operand2
valstack.push(result)
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.