document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. class InToPost
  2. {
  3.     private Stack theStack;
  4.     private String input;
  5.     private Queue queue;
  6.     public InToPost(String in)
  7.     {
  8.         input = in;
  9.         int stackSize = input.length();
  10.         int queueSize = input.length();
  11.         queue = new Queue(queueSize);
  12.         theStack = new Stack(stackSize);
  13.     }
  14.    
  15.     public String doTrans()
  16.     {
  17.         for(int j=0; j<input.length(); j++)
  18.         {
  19.             char ch = input.charAt(j);
  20.             switch(ch)
  21.             {
  22.                 case \'+\':
  23.                 case \'-\':
  24.                 gotOper(ch, 1);
  25.                 break;
  26.                 case \'*\':
  27.                 case \'/\':
  28.                 gotOper(ch, 2);
  29.                 case \'(\':
  30.                 theStack.push(ch);
  31.                 break;
  32.                 case \')\':
  33.                 gotParen(ch);    
  34.                 break;
  35.                 default:    
  36.                 queue.enqueue(ch);    
  37.                 break;
  38.             }  
  39.         }  
  40.         while( !theStack.isEmpty() )    
  41.         {
  42.             queue.enqueue(theStack.pop());
  43.         }
  44.         return queue.getString();  
  45.     }  
  46.     public void gotOper(char opThis, int prec1)
  47.     {    
  48.         while( !theStack.isEmpty() )
  49.         {
  50.             char opTop = theStack.pop();
  51.             if( opTop == \'(\' )
  52.             {
  53.                 theStack.push(opTop);
  54.                 break;
  55.             }
  56.             else  
  57.             {
  58.                 int prec2;
  59.                 if(opTop==\'+\' || opTop==\'-\')
  60.                     prec2 = 1;
  61.                 else
  62.                     prec2 = 2;
  63.                 if(prec2 < prec1)
  64.                 {
  65.                     theStack.push(opTop);
  66.                     break;
  67.                 }
  68.                 else
  69.                     queue.enqueue(opTop);
  70.             }
  71.         }
  72.         theStack.push(opThis);
  73.     }
  74.  
  75.     public void gotParen(char ch)
  76.     {
  77.         while( !theStack.isEmpty() )
  78.         {
  79.             char chx = theStack.pop();
  80.             if( chx == \'(\' )
  81.                 break;
  82.             else
  83.                 queue.enqueue(chx);
  84.         }
  85.     }
  86. }
');