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

Hey every I need help with a java project. If you can leave comments itll help a

ID: 3801887 • Letter: H

Question

Hey every I need help with a java project. If you can leave comments itll help a lot! thanks!

Infix – Postfix Consider the infix-postfix conversion algorithm given in your textbook (pages 174-176).

Implement the algorithm but do not assume the infix expressions are error free. For each infix expression (your program must handle any number of them) convert and display the correct postfix equivalent. If the infix expression is syntactically incorrect display a message which indicates the type of error. Your program must place the postfix expression into a queue and when conversion is complete display the queue. The algorithm in the book uses a String for the postfix expression, you must use a queue.

Operands are single character identifiers. Consider the binary operators of +,-, * and /, with normal operator precedence, parentheses are used to change normal precedence rules. Expressions may contain blanks (any number of them) as delimiters. Your error checking must be incorporated into the translation algorithm given in the book; it cannot be handled in a separate pre-translation pass of the expression. You should be able to recognize at least the following three errors: mismatched parentheses, missing operand, and missing operator. You are to use exceptions as the method of alerting of the error, and must raise a different exception for each of the three errors.

A sample run might be:

(a + b) * c -> a b + c *

(a + b * c ->mismatched parentheses

a + + b ->missing operand

a + -> missing operand

throws symtaxE //Process each operatorsti Prek the har topop Postfix String nfis) indin LinedPATTERN) CVraatched epesing pal Except the top is empty Faye

Explanation / Answer

import java.lang.sysytem
import java.lang.string
import java.io.IOException;

public class InfixToPostfix
{
private Stack theStack;
private String iput;
private String oput = "";
public InfixToPostfix(String in)
{
iput = in;
int stackSize = iput.length();
theStack = new Stack(stackSize);
}
public String doTran() {
for (int y = 0; j < iput.length(); y++)
{
char ch = iput.charAt(y);
switch (ch)
{
case '-':
case '+':
gotOper(ch, 1);
break;
case '/':
case '*':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
oput = oput + ch;
break;
}
}
while (!theStack.isEmpty())
{
output = output + theStack.pop();
}
System.out.println(oput);
return oput;
}
public void gotOperation(char opThis, int prec1)
{
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') {
theStack.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
theStack.push(opTop);
break;
}
else oput = oput + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch) {
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(')
break;
else output = output + chx;
}
}
public static void main(String[] args) throws IOException {
String iput = "1+2*4/5-7+3/6";
String oput;
InfixToPostfix theTrans = new InToPost(iput);
oput = theTrans.doTrans();
System.out.println("Postfix is " + oput + ' ');
}
class Stack
{
private int maximumSize;
private char[] stackArray;
private int top;
  
public Stack(int maximum)
{
maxSize = maximum;
stackArray = new char[maximumSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = y;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}