m2skills

in2pre java

Mar 29th, 2017
10,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. /**
  2.  * Created by MOHIT on 13-02-2017.
  3.  */
  4.  
  5.  
  6. /* Program to perform all types of stack conversions */
  7. import java.util.Scanner;
  8.  
  9. public class inpre{
  10.  
  11.     static char[] stk = new char[20];
  12.     static int top = -1;
  13.     static int MAX = 20;
  14.  
  15.     public static void main(String arg[]){
  16.         int cont;
  17.         String infix, prefix;
  18.         Scanner sc = new Scanner(System.in);
  19.         System.out.println("Enter the Infix Expression : ");
  20.         infix = sc.next();
  21.         prefix = in2prefix(infix);
  22.     }
  23.  
  24.     // method that pushes the elements onto the character stack
  25.     static void push(char oper)
  26.     {
  27.         if(top==MAX-1)
  28.         {
  29.             System.out.println("stackfull!!!!");
  30.         }
  31.  
  32.         else
  33.         {
  34.             top++;
  35.             stk[top]=oper;
  36.         }
  37.     }
  38.  
  39.     // method that removes character from stack and returns them
  40.     static char pop()
  41.     {
  42.         char ch;
  43.         if(top==-1)
  44.         {
  45.             System.out.println("stackempty!!!!");
  46.         }
  47.         else
  48.         {
  49.             ch = stk[top];
  50.             stk[top]='\0';
  51.             top--;
  52.             return(ch);
  53.         }
  54.         return 0;
  55.     }
  56.  
  57.     // method that converts String from infix to prefix
  58.     // all the strings are assumed to be valid expressions
  59.     static String in2prefix(String infix)
  60.     {
  61.         int i=0;
  62.        
  63.         // result string variable
  64.         String prefix = "";
  65.        
  66.         infix = new StringBuffer(infix).reverse().toString();
  67.        
  68.         // loop till i is in the range of the length of string
  69.         while(i < infix.length())
  70.         {
  71.             // if an alphabet is found then copy it to the output string
  72.             if(infix.charAt(i)>='a' && infix.charAt(i)<='z')          
  73.             {
  74.                 prefix += infix.charAt(i);
  75.                 i++;
  76.             }
  77.  
  78.             // as we have reversed the string closing bracket will be found first
  79.             // if an closing bracket is found then put it in stack
  80.             else if(infix.charAt(i)==')' || infix.charAt(i)=='}'  || infix.charAt(i)==']')      
  81.             {
  82.                 push(infix.charAt(i));
  83.                 i++;
  84.             }
  85.            
  86.             // as we have reversed the string opening bracket will be found after the closing bracket
  87.             // if an opening bracket is found then
  88.             // keep removing the operators from the stack and add them to prefix string until you find the corresponding opening bracket
  89.             else if(infix.charAt(i)=='(' || infix.charAt(i)=='{'  || infix.charAt(i)=='[')
  90.             {
  91.                 if(infix.charAt(i)=='(')
  92.                 {
  93.                     while(stk[top]!=')')
  94.                     {
  95.                         prefix += pop();
  96.                     }
  97.                     pop();
  98.                     i++;
  99.                 }
  100.  
  101.                 if(infix.charAt(i)=='[')
  102.                 {
  103.                     while(stk[top]!=']')
  104.                     {
  105.                         prefix += pop();
  106.                     }
  107.                     pop();
  108.                     i++;
  109.                 }
  110.  
  111.                 if(infix.charAt(i)=='{')
  112.                 {
  113.                     while(stk[top]!='}')
  114.                     {
  115.                         prefix += pop();
  116.                     }
  117.                     pop();
  118.                     i++;
  119.                 }
  120.             }
  121.             // if none of the above cases are satisfied then we surely have an operator
  122.             else            
  123.             {
  124.                
  125.                 // if the stack if empty then we simply put the operator in stack
  126.                 if(top==-1)
  127.                 {
  128.                     push(infix.charAt(i));
  129.                     i++;
  130.                 }
  131.  
  132.                 // if the priority of current operator is less than the stack top then
  133.                 // pop the stack top and add it to the prefix string
  134.                 else if( priority(infix.charAt(i)) < priority(stk[top])) {
  135.                     prefix += pop();
  136.                    
  137.                     // now if you have an operator that has priority greater than current operator then pop
  138.                     while(priority(stk[top]) > priority(infix.charAt(i))){
  139.                         prefix += pop();
  140.                         if(top < 0) {
  141.                             break;
  142.                         }
  143.                     }
  144.                     push(infix.charAt(i));
  145.                     i++;
  146.                 }
  147.  
  148.                 // if the priority of current operator is greater than or equal to the stack top then push it onto the stack
  149.                 else if(priority(infix.charAt(i)) >= priority(stk[top])) {
  150.                     push(infix.charAt(i));
  151.                     i++;
  152.                 }
  153.             }
  154.         }
  155.  
  156.         // at the end remove all the operators from the stack
  157.         while(top!=-1)
  158.         {
  159.             prefix += pop();
  160.         }
  161.        
  162.         // reverse the final string before output
  163.         prefix = new StringBuffer(prefix).reverse().toString();
  164.        
  165.         System.out.println("The converted prefix String is : " + prefix);
  166.         return prefix;
  167.     }
  168.  
  169.     // method that returns priority for operators according to their precedence
  170.     static int priority ( char alpha )
  171.     {
  172.         if(alpha == '+' || alpha =='-')
  173.         {
  174.             return(1);
  175.         }
  176.  
  177.         if(alpha == '*' || alpha =='/')
  178.         {
  179.             return(2);
  180.         }
  181.  
  182.         if(alpha == '$')
  183.         {
  184.             return(3);
  185.         }
  186.  
  187.         return 0;
  188.     }
  189.  
  190. }
Add Comment
Please, Sign In to add comment