Problem

Stack-based Evaluation of Prefix Expressions Write a program that reads prefix ex...

Stack-based Evaluation of Prefix Expressions

Write a program that reads prefix expressions and prints their values. Each input expression should be entered on its own line, and the program should terminate when the user enters a blank line. Assume that there are only binary operators and that the expressions contain no variables. Your program should use a stack. Here are sample input–output pairs:

Hint : Go through the prefix expression from left to right, pushing operators and values onto a stack. Whenever you have two values on the stack with an operator just underneath them, pop the two values, pop the operator, apply the operator to the value, and push the result (which is a value) onto the stack. The tricky part is how to store both operators of type char and values of type int on the same stack. Think about a type such as the following:

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
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