Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

/ 111 55

* + 16 4 - 3 1

+ 4 3

+ 555 + 44 3

/ / 555 44 3

- + 77777 / 6666 555 - 44 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions