m2skills

in2post java

Mar 29th, 2017
3,540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 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 inpost{
  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, postfix;
  18.         Scanner sc = new Scanner(System.in);
  19.         System.out.println("Enter the Infix Expression : ");
  20.         infix = sc.next();
  21.         postfix = in2postfix(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 postfix
  58.     // all the strings are assumed to be valid expressions
  59.     static String in2postfix(String infix)
  60.     {
  61.         int i=0;
  62.        
  63.         // result string variable
  64.         String postfix = "";
  65.  
  66.         // loop till i is in the range of the length of string
  67.         while(i < infix.length())
  68.         {
  69.             // if an alphabet is found then copy it to the output string
  70.             if(infix.charAt(i)>='a' && infix.charAt(i)<='z')          
  71.             {
  72.                 postfix += infix.charAt(i);
  73.                 i++;
  74.             }
  75.  
  76.             // if an opening bracket is found then put it in stack
  77.             else if(infix.charAt(i)=='(' || infix.charAt(i)=='{'  || infix.charAt(i)=='[')      
  78.             {
  79.                 push(infix.charAt(i));
  80.                 i++;
  81.             }
  82.            
  83.             // if an closing bracket is found then
  84.             // keep removing the operators from the stack and add them to postfix string until you find the corresponding opening bracket
  85.             else if(infix.charAt(i)==')' || infix.charAt(i)=='}'  || infix.charAt(i)==']')
  86.             {
  87.                 if(infix.charAt(i)==')')
  88.                 {
  89.                     while(stk[top]!='(')
  90.                     {
  91.                         postfix += pop();
  92.                     }
  93.                     pop();
  94.                     i++;
  95.                 }
  96.  
  97.                 if(infix.charAt(i)==']')
  98.                 {
  99.                     while(stk[top]!='[')
  100.                     {
  101.                         postfix += pop();
  102.                     }
  103.                     pop();
  104.                     i++;
  105.                 }
  106.  
  107.                 if(infix.charAt(i)=='}')
  108.                 {
  109.                     while(stk[top]!='{')
  110.                     {
  111.                         postfix += pop();
  112.                     }
  113.                     pop();
  114.                     i++;
  115.                 }
  116.             }
  117.             // if none of the above cases are satisfied then we surely have an operator
  118.             else            
  119.             {
  120.                
  121.                 // if the stack if empty then we simply put the operator in stack
  122.                 if(top==-1)
  123.                 {
  124.                     push(infix.charAt(i));
  125.                     i++;
  126.                 }
  127.  
  128.                 // if the priority of current operator is less than or equal to the stack top then
  129.                 // pop the stack top and add it to the postfix string
  130.                 else if( priority(infix.charAt(i)) <= priority(stk[top])) {
  131.                     postfix += pop();
  132.                    
  133.                     // now if you have an operator that has equal priority as of current operator then pop
  134.                     while(priority(stk[top]) == priority(infix.charAt(i))){
  135.                         postfix += pop();
  136.                         if(top < 0) {
  137.                             break;
  138.                         }
  139.                     }
  140.                     push(infix.charAt(i));
  141.                     i++;
  142.                 }
  143.  
  144.                 // if the priority of current operator if high then push it onto the stack
  145.                 else if(priority(infix.charAt(i)) > priority(stk[top])) {
  146.                     push(infix.charAt(i));
  147.                     i++;
  148.                 }
  149.             }
  150.         }
  151.  
  152.         // at the end remove all the operators from the stack
  153.         while(top!=-1)
  154.         {
  155.             postfix += pop();
  156.         }
  157.         System.out.println("The converted postfix String is : " + postfix);
  158.         return postfix;
  159.     }
  160.  
  161.     // method that returns priority for operators according to their precedence
  162.     static int priority ( char alpha )
  163.     {
  164.         if(alpha == '+' || alpha =='-')
  165.         {
  166.             return(1);
  167.         }
  168.  
  169.         if(alpha == '*' || alpha =='/')
  170.         {
  171.             return(2);
  172.         }
  173.  
  174.         if(alpha == '$')
  175.         {
  176.             return(3);
  177.         }
  178.  
  179.         return 0;
  180.     }
  181.  
  182. }
Add Comment
Please, Sign In to add comment