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

You are to write a program name lnfixToPostfix.java that converts an infix expre

ID: 3577347 • Letter: Y

Question

You are to write a program name lnfixToPostfix.java that converts an infix expression entered by the user to a postfix expression The expression may contain the following tokens: Integer constants (a series of decimal digits), x (representing a value to be supplied later). Binary operators (+, -, *, /and %). Parentheses Spaces between tokens are allowed but not required. The program will convert the expression to postfix form and display the converted expression. Upload the file into A7 folder of icollege. Enter infix expression: (x + 1) middot (x - 2)/4 Converted expression: x1 + x2 - 4/Enter infix expression: 1 2 + Error in expression!! No operator between operands. Also last token must be an operand. Enter infix expression: 10.4 Error in expression!! Cannot accept floating point numbers. Enter infix expression: 11(+ 2) Error in expression!! No operator between operand and left parentheses. Enter infix expression: 5 - (x - 2)) Error in expression!! No matching left parentheses for a right parentheses. Enter infix expression: 1 2 Error in expression!! The * operator cannot be preceded by a " operator.

Explanation / Answer

Solution:

Code:

package intopost;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class InfixToPostfix
{
String inf="";
Queue<Character> que;
  
public InfixToPostfix(String inf)
{
this.inf=inf;
que=new LinkedList<Character>();
}
  
public void inf_to_post() throws Exception
{
  
Stack<Character> st=new Stack<Character>();
int parnth=0;
int var=0;
int ops=0;
boolean lastvar=false;
boolean lastWasleftpar=false;
boolean nextrightpar=false;
for(int i=0;i<inf.length();i++)
{
char c=inf.charAt(i);
if (Character.isLetter(c)||Character.isDigit(c))
{
var++;
que.add(c);   
if(lastvar==true)
throw new Exception("Error sc expression!!No operand between operands");
lastvar=true;
lastWasleftpar=false;
nextrightpar=false;
  
}
  
else
{
lastvar=false;

switch (c)
{
case '(':
parnth++;
st.push(c);
lastWasleftpar=true;
break;
case ')':
if(nextrightpar==true)
throw new Exception("Error sc expression!! No opeartpr between operand and right parenthesis");
nextrightpar=false;
parnth--;
if(parnth>=0)
{
while(st.peek()!='(')
{
que.add(st.pop());
}
}
st.pop();
lastWasleftpar=false;
break;
case '+':
case '-':
case '*':
case '/':
if(lastWasleftpar==true)
throw new Exception("Error sc expression!! No opeartpr between operand and left parenthesis");
ops++;
while(!st.isEmpty() && st.peek()!='(' &&
oper_prec(c)<=oper_prec(st.peek()))
{
que.add(st.pop());
}
st.push(c); //save operator
nextrightpar=true;
break;
}
}

}
if(parnth!=0)
throw new Exception("Parentheses Error");
if(var<=ops)
throw new Exception("Missing Operand Error");
while(!st.isEmpty())
que.add(st.pop());


}

  
public void display()
{
Iterator it=que.iterator();
System.out.println("Converted Expression:");
while(it.hasNext())
System.out.print(it.next());
System.out.println();
}
  
public String get_infix()
{
return inf;
}
  
  
public void set_infix(String inf)
{
this.inf=inf;
que=new LinkedList<Character>();
}
  
private int oper_prec (char operator)
{
switch (operator)
{
case '(':
case ')':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return -1;
}
  
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
  
while(true)
{
System.out.println("Enter infix expression ");
  
if(sc.hasNextFloat())
{
System.out.println("Error sc expresiion!!Cannot accept floating point number");
break;
}
String inp=sc.nextLine();
Intopost c=new Intopost(inp);
try
{
c.inf_to_post();
c.display();
}
catch (Exception ex)
{
System.out.println(ex.getMessage()+" on "+c.get_infix());
}
  
}
}
}

Output:

run:
Enter infix expression
(x+1)*(x-2)/4
Converted Expression:
x1+x2-*4/
Enter infix expression
12+
Error sc expression!!No operand between operands on 12+
Enter infix expression
1(+2)
Error sc expression!! No opeartpr between operand and left parenthesis on 1(+2)
Enter infix expression
5-(x-2))
Parentheses Error on 5-(x-2))
Enter infix expression
1**2
Missing Operand Error on 1**2
Enter infix expression

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