fatalaa

PostFix

Apr 26th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Stack;
  2. public class PostFix {
  3.     public static Stack st;
  4.     public static String postfix;
  5.    
  6.    
  7.     public static void stackOrder( String s , int i , String postfix ) {
  8.         if( st.empty() ||
  9.           OperatorList.stronger( s.charAt( i ) , (char)st.firstElement() ) ) {
  10.             return;
  11.         }
  12.         postfix += (char)st.pop();
  13.         stackOrder( s , i , postfix );      
  14.     }
  15.    
  16.     public static void stackToPostFix() {
  17.         while( !st.empty() ) {
  18.             postfix += (char)st.pop();
  19.         }
  20.        
  21.     }
  22.    
  23.     public static String convertInFixToPostFix( String s ) {
  24.         st = new Stack();
  25.         postfix = new String();
  26.        
  27.         for( int i = 0; i < s.length(); i++ ) {
  28.             if( !OperatorList.isOperator( s.charAt( i ) ) ) {
  29.                 postfix += s.charAt( i );
  30.             }
  31.             if( OperatorList.isOperator( s.charAt( i ) ) ) {
  32.                 if( st.empty() ) {
  33.                     st.push( s.charAt( i ) );
  34.                 }
  35.                 if( OperatorList.stronger( s.charAt( i ) , (char)st.firstElement() ) ) {
  36.                     st.push( s.charAt( i ) );
  37.                 }
  38.                 else {
  39.                     stackOrder( s , i , postfix );
  40.                 }
  41.             }
  42.         }
  43.         stackToPostFix();
  44.         return postfix;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment