JAVA You are to write a program name InfixToPostfixjava that

JAVA
You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may contain the following tokens:
(1)   Integer constants (a series of decimal digits).
(2)   x (representing a value to be supplied later).
(3)   Binary operators (+, -, *, / and %).
(4)   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.
Sample Output:
Enter infix expression: (x + 1) * (x – 2) / 4
Converted expression: x 1 + x 2 - * 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: 1 ( + 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.

Solution

Answer:

import java.util.*;
import java.lang.*;
import java.io.*;

public class InfixToPostfix
{
   static Stack<Character> infxStk = new Stack<Character>();
    static ArrayList<Character> infxAry = new ArrayList<Character>();
   public static int optPrec(char aak)
   {
        if(aak == \'/\' || aak == \'%\' || aak == \'*\'){
        return 2;
        }
       else
        return 1;
    }
    public static void main(String args[])
   {    
   
  
    // Boolean variables
    boolean lstOpt = false;
    boolean lstOpnPar = false;
    boolean lsClsdParen = false;
    boolean lstIntg = false;
    boolean pvTkn = false;
  
    // scanner to read input
    Scanner myInfxScanner = new Scanner(System.in);
  
    // read expression
    System.out.println(\"Enter infix expression : \");
    String infxStr = myInfxScanner.nextLine();
   //extract characters
    char infx[] = infxStr.toCharArray();
  
    //checking operators between parenthesis
    if(infx[infx.length-1] == \'/\' || infx[infx.length-1] == \'*\' || infx[infx.length-1] == \'%\' ||
infx[infx.length-1] == \'+\' || infx[infx.length-1] == \'-\'){
       //print error message
System.out.println(\"Error in expression! No operator between operands.Also last token must be an operand .\");
System.exit(0);
    }
  
    // checking operands
    if(infx[0] == \'/\' || infx[0] == \'*\' || infx[0] == \'%\' || infx[0] == \'+\' || infx[0] == \'-\'){
   //print error-msg
System.out.println(\"Error in expression!! \");
System.exit(0);
    }
  

    for(int kk = 0; kk < infxStr.length(); kk++)
   {
       //check for spaces
    if(infx[kk] == \' \')
       {
        continue;
    }
       //Get character at kk
    infx[kk] = infxStr.charAt(kk);
    switch(infx[kk]){
  
    // Check for integers
    case \'1\': case \'2\': case \'3\': case \'4\':
    case \'5\': case \'6\': case \'7\': case \'8\':
    case \'9\': case \'0\':
       //check if closed parentesis is encountered.
    if(lsClsdParen)
       {
    System.out.println(\"Error in expression!! An integer cannot directly follow a closed parenthesis.\");
    System.exit(0);
    }

    lstOpt = false;
    lstOpnPar = false;
    lsClsdParen = false;
    lstIntg = true;
       //add operand
    infxAry.add(infx[kk]);
    break;
  
    // check for spaces
    case \' \': continue;
       //check unknown-variable  
       case \'x\':
           //add operand
           infxAry.add(infx[kk]);
        break;

  

  
    // for operators
    case \'/\':
       case \'+\':
    case \'%\':
           //if operator is preceeded by an operator
   if (lstOpt){
   System.out.println(\"Error in expression! An operator cannot directly follow another operator.\");
               //exit
   System.exit(0);
   }
           //if operator is preceeded by paraenthesis
   if (lstOpnPar){
   System.out.println(\"Error in expression! \"+\"An operator cannot directly follow an open parenthesis.\");
               //exit
   System.exit(0);
   }
   lstOpt = true;
   lstOpnPar = false;
   lsClsdParen = false;
   lstIntg = false;
  
   while(!infxStk.isEmpty() && infxStk.peek() != \'(\' &&
optPrec(infx[kk]) <= optPrec(infxStk.peek())){
   infxAry.add(infxStk.pop());
   }
   infxStk.push(infx[kk]);
   break;
       //for multiplication symbol
    case \'*\':
           //try
           try
           {
    while(!infxStk.isEmpty() && infxStk.peek() != \'(\' &&
optPrec(infx[kk]) <= optPrec(infxStk.peek()))
               {
                   //add operand
   infxAry.add(infxStk.pop());
    }
               //push \"*\"
   infxStk.push(\'*\');
   break;
    }
           //catch
           catch(EmptyStackException expp)
           {
               //error on double \"*\"
    System.out.println(\"Error in expresion! The * operator cannot be preceded by a * operator.\");
               //exit
   System.exit(0);
    }

    //for -
    case \'-\':
           while(!infxStk.isEmpty() && infxStk.peek() != \'(\' &&
   optPrec(infx[kk]) <= optPrec(infxStk.peek()))
           {
               //add operand
       infxAry.add(infxStk.pop());
   }
           //add \"-\"
   infxStk.push(\'-\');
   break;
  
    // for parentesis
    case \'(\':
           infxStk.push(infx[kk]);
   lstOpt = false;
   lstOpnPar = true;
   lsClsdParen = false;
   lstIntg = false;
   break;
  
    case \')\':
           //check lstOpt is true
           if(lstOpt)
           {
        System.out.println(\"Error in expression! A closed parenthesis cannot directly follow an operator.\");
       System.exit(0);
        }
   lstOpt = false;
   lstOpnPar = false;
   lsClsdParen = true;
   lstIntg = false;
           //try
   try{
   while(infxStk.peek() != \'(\')
               {
                   //pop opertor and add it to operand array
       infxAry.add(infxStk.pop());
   }
               //pop operator
   infxStk.pop();
   break;
   }
           //catch
           catch(EmptyStackException expp)
           {
   System.out.println(\"Error in expression! No matching left parentheses for a right parentheses.\");
   System.exit(0);
   }
  
    // checking floating point numbers
    case \'.\':
           //print error-msg
           System.out.println(\"Error in expression! Cannot accept floating point numbers.\");
           //exit
System.exit(0);

  
    // for illegal character
    default:
           //print error-msg
        System.out.println(\"Illegal character \"); break;
  
    }
    }
   //pop all operators from infxStk
    while(!infxStk.isEmpty())
   {
       //check error in left-parentesis
    if(infxStk.peek() == \'(\'){
        System.out.println(\"Error in expression! No matching right parenthesis for left parentehesis\");
        System.exit(0);
    }  
    infxAry.add(infxStk.pop());
    }
   //Converted expression
    System.out.print(\"Converted Expression: \");
   //print each operator & operand
    for(int kk = 0; kk < infxAry.size(); kk++)
       //print operator & operand
    System.out.print(infxAry.get(kk)+\" \");

    System.out.println();

  
}
  


  

  
}

Sample Output:

sh-4.3$ javac InfixToPostfix .java                                                                                                      

sh-4.3$ java -Xmx128M -Xms16M InfixToPostfix                                                                                            

Enter infix expression :                                                                                                           

10.4                                                                                                                               

Error in expression! Cannot accept floating point numbers.                                                                         

sh-4.3$ java -Xmx128M -Xms16M InfixToPostfix   

Enter infix expression :                                                                                                           

1*2                                                                                                                                

Converted Expression: 1 2 *                                                                                                        

sh-4.3$ java -Xmx128M -Xms16M InfixToPostfix                                                                                            

Enter infix expression :                                                                                                           

1-( + 2)                                                                                                                           

Error in expression! An operator cannot directly follow an open parenthesis.                                                       

JAVA You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may conta
JAVA You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may conta
JAVA You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may conta
JAVA You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may conta
JAVA You are to write a program name InfixToPostfix.java that converts an infix expression entered by the user to a postfix expression. The expression may conta

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site