Problem

Repeat Programming Problem 5, but use the following algorithm to evaluate an infix express...

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)

Step-by-Step Solution

Request Professional Solution

Request Solution!

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.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 7
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT