Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To design and implement a Java program that converts an infix expression to a postfix expression and then evaluates the postfix expression and prints out

To design and implement a Java program that converts an infix expression to a postfix expression and then evaluates the postfix expression and prints out the result.

We have all learned how to evaluate simple infix expressions that involve the basic binary operators: addition, subtraction, multiplication, and division. (These are called binary operators because they each operate on two operands.). For example 5 + 5 is a simple infix expression.

As we all know not all mathematical expression are this simple. Most are more complex such as ((10 (3 + 2)) * 5 / (4 + 1). These more complex expressions use precedence rules to determine out how the expression will be evaluated. The more complex the expression the greater the risk for error when evaluating the expression.

Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operator. Postfix notation removes the need for precedence rules which are required, when necessary, for infix expressions. This allows for a simpler mathematical expression, thus reducing calculation errors.

The above infix expression when converted to postfix would look as follows: 10 3 2 + - 5 * 4 1 + /.

The rules for evaluating postfix expressions with multiple operators is much simpler than those for evaluating infix expressions, you simply need to evaluate the operations from left to right. When scanning you will ignore operands UNTIL you scan an operator. Once an operator is scanned it will be applied to the two previous operands.

In the expression above we evaluate the expression by scanning left to right. The first item scanned is the operand 10 so we move on. The next item scanned is the operand 3 and we move on. The next item scanned the operand 2 and we move on. Finally, we scan the operator + at which time we apply the addition operator (+) to the previous two operands which were 2 and 3 giving us a value of 5. This result then replaces the operands 2 and 3. The scanning of the expression continues until you reach the end of the expression. As you can see, stack will be required to create and evaluate your postfix expressions.

NOTE: Division deciding which operand is the dividend and which is the divisor is as follows the divisor will always be the most recently scanned operand or mostly recently calculated result from an operation,

The input for this assignment will be an infix expression that will be entered via a GUI calculator which you will build. The GUI will consist of a table of buttons for the numbers (0 9), the operators (+, - ,*, /, ^), the parentheses () and an equal sign (=). The user will use this GUI to enter an infix expression. This infix expression should be displayed in the GUI. When the equal sign button is clicked, the infix expression should be converted to postfix expression and then evaluated. Both the postfix expression and its result should be displayed in the GUI.

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

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

1. Let a, b R, a Answered: 1 week ago

Answered: 1 week ago