Question: PLEASE GUYES EXPLAIN TO ME THIS ASSIGNMENT USING JAVA PROGRAM - ECLIPSE. Problem: You will implement a calculator program based on the stack-based algorithms given

PLEASE GUYES EXPLAIN TO ME THIS ASSIGNMENT USING JAVA PROGRAM - ECLIPSE.

Problem: You will implement a calculator program based on the stack-based algorithms given in class. These algorithms are reproduced below.

Your program prompts the user to enter an arithmetic expression (as a string) that can contain the symbols +, -, *, /, (, ) and the digits 0-9. To keep the problem (relatively) simple, numbers in the expression can only be one digit long.

Example:

5 + (6 2) * 8 9 / 3

is a valid input string.

Your program solves the arithmetic expression using the correct order of operations and prints the answer. (The answer to the expression above is 34.)

You may assume that the input string is always valid. Note that the input string may contain spaces.

Converting Infix to Postfix: The first task your program must perform is the conversion of the given infix expression into a postfix expression. Recall that infix means that the operator appears between two numbers whereas postfix means that the operator appears after the numbers.

Example:

5 + 3 2

is an infix expression. The equivalent postfix expression is:

5 3 + 2

Here is the algorithm for converting infix to postfix:

Start with an empty postfix expression and an empty stack.

Traverse the infix expression left to right:

If the current character is a digit,

Append digit to postfix expression.

If the current character is a ) symbol,

Pop the stack and append popped symbols to postfix expression until the ( symbol is at the top.

Pop the ( symbol.

If the current character is any other symbol (but not a space),

Pop the stack and append popped symbols to postfix expression until a symbol of lower priority is at the top, the ( symbol is at the top, or the stack is empty.

Push current character onto stack.

Pop the rest of the stack and append popped symbols to postfix expression.

Evaluating the Postfix Expression: Once you have a postfix expression, your program can evaluate it to find the answer using the following algorithm:

Start with an empty stack.

Traverse the postfix expression left to right:

If the current character is a digit,

Push digit onto stack.

If the current character is an operator,

Pop two digits off the stack and apply the operator to them in the proper order to get a result. (For example, if the symbol is and you pop 4 and 7, in that order, then compute 7 4 to get the result 3.)

Push the result onto stack.

Pop the stack to get the answer.

Extra Credit: Enhance the program so that the arithmetic expression can contain any integer instead of only single-digit integers.

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!