Question: Write a Java program that reads prefix expressions and evaluates them. The expressions can contain the operators + - * / and integers, with a

Write a Java program that reads prefix expressions and evaluates them. The expressions can contain the operators + - * / and integers, with a space between each component. Expression are limited to a single line.

To evaluate a prefix expression...

1.) split the expression with space as the delimiter

2.) iterate through the tokens from last to first

3a.) If the token is an integer, push it on to an Integer stack.

3b.) If a token is an operator, pop two integers from the stack, perform the operation, and push the result onto the stack.

After processing all the tokens, the final result is on the stack.

Integer arithmetic should be used for the operations.

Use the Java Stack class for your stack

Output:

The program should display each original prefix expression, an equal sign, and the result.

Test program with expressions included:

+ 2 51

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!