Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then

Write a java program for the following:

Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen ). Your program is required to handle at least five mathematic operations, +, -, * and /, and ^(exponential). When you perform division, please do integer division. For example, 7 / 2 is 3. Note that in this program, the input infix expression contains one-digit operands and produces one-digit result. In other words, you do not worry about the infix expressions that involve multiple-digit operands and produce multiple-digit results.

Note that you have to implement your own Stack class in this program. The built-in Java Stack is not allowed in this program.

Please use Java Object type or the generic type E for the data element in your stack implementation. Please catch any potential errors, so that your program will not crash. You do not need to check whether all parentheses in the input infix expression are correctly matched or not. You can safely assume the format of the infix expression is correct. Each run of your program will evaluate only one infix expression.

Sample Run One

Please enter the infix expression to process: (((1+2)-(3-4))/(6-5))

The postfix expression for the input infix is: 1 2 + 3 4 - - 6 5 - /

The final result after evaluating the postfix is: 4

Sample Run Two

Please enter the infix expression to process: 2 * 4 - 2 ^ 2 ^ 1

The postfix expression for the input infix is: 2 4 * 2 2 1 ^ ^ -

The final result after evaluating the postfix is: 4

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

Step: 3

blur-text-image

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

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

Prepare a flowchart of Dr. Mahalees service encounters.

Answered: 1 week ago