Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stack Assignment #11 will be the construction of a program that converts an infi

ID: 3533894 • Letter: S

Question

Stack

Assignment #11 will be the construction of a program that converts an infix expression into an equivalent postfix expression.
The program reads a string ( option "E"), and prints out its corresponding postfix expression if there is no error in the input string, and outputs a message if there is an error.

The PostfixConversion class is a class that has a utility that converts an infix expression to its postfix expression. It must have the following two methods:

public static boolean precedence(char first, char second)

The precedence method determines the precedence between two operators. If the first operator is of higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false. This method will be called from the convertToPostfix method described below.

public static String convertToPostfix(String infixExp)

The convertToPostfix method converts the infix expression (the parameter string for this method) into a postfix expression.

1. If all parentheses are matching in the input infix expression string, then computes its postfix expression, and the method should return the string as:

"The Postfix Expression is: ABC++DHTY++R-*-"

Here, ABC++DHTY++R-*- is an example of postfix expression and the correct one for each input should be displayed instead of it.

2. If there is a left parenthesis that does not have its corresponding right parenthesis,
then for the first such character, return a string with the message:

"There is no matching right parenthesis."

For instance, if the input infix expression string is:(A+(B+C)
then for the first left parenthesis, there is no corresponding right parenthesis. In this case, the above message should be returned instead of any post fix expression.

3. If there is a right parenthesis that does not have its corresponding left parenthesis,
then for the first such character, return a string with the message as:

"There is no matching left parenthesis."

For instance, if the input infix expression string is:
A)+(B+C)
then for the first right parenthesis, there is no corresponding left parenthesis. In this case, the above message should be returned instead of any post fix expression.

a. Scan each character of a given infix expression (a string) from left to right. (One pass is sufficient.)

b. If the next scanned symbol (character) is an operand (here only alphabet letters - both upper cases and lower cases are used.), append it to the postfix expression.

c. If the next scanned symbol (character) is a left parenthesis '(', then put it onto the stack.

If the next scanned symbol (character) is a right parenthesis ')', then pop and append all the symbols from the stack until the most recent matching left parenthesis. i.e., pop and append all the symbols above the left parenthesis that is located in the highest position in the stack. Then pop and discard that left parenthesis at the highest position in the stack.

d. If the next scanned symbol (character) is an operator (here, they are either '+', '-', '*', or '/'),

Explanation / Answer

//stack class also containing the intopostfix method import java.util.*; public class Stack { int i,j; char postfix[]; char stack[]; int top; String post; public Stack(int n) { stack=new char[n]; top=-1; } public void push(char item) { if(top>=stack.length) System.out.println("Stack overflow"); else { stack[++top]=item; } } public char pop() { if(top==-1) { System.out.println("Stack underflow"); return 0; } else return stack[top--]; } boolean isAlpha(char ch) { if((ch>='a'&&ch=0&&ch
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote