Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. class InToPost // infix to postfix conversion
  2. {
  3.     private Stack theStack;
  4.     private String input;
  5.     private String output = "";
  6.     //--------------------------------------------------------------
  7.     public InToPost(String in) // constructor
  8.     {
  9.         input = in;
  10.         int stackSize = input.length();
  11.         theStack = new Stack(stackSize);
  12.     }
  13.     //--------------------------------------------------------------
  14.     public String doTrans() // do translation to postfix
  15.     {
  16.         for(int j=0; j<input.length(); j++)
  17.         {
  18.             char ch = input.charAt(j);
  19.             switch(ch)
  20.             {
  21.                 case '+': // it’s + or -
  22.                 case '-':
  23.                     gotOper(ch, 1); // go pop operators
  24.                     break; // (precedence 1)
  25.                 case '*': // it’s * or /
  26.                 case '/':
  27.                     gotOper(ch, 2); // go pop operators
  28.                     break; // (precedence 2)
  29.                 case '^':
  30.                     gotOper(ch, 3);
  31.                     break;
  32.                 case '(': // it’s a left paren
  33.                     theStack.push(ch); // push it
  34.                     break;
  35.                 case ')': // it’s a right paren
  36.                     gotParen(ch); // go pop operators
  37.                     break;
  38.                 default: // must be an operand
  39.                     output = output + ch; // write it to output
  40.             } // end switch
  41.         } // end for
  42.         while( !theStack.isEmpty() ) // pop remaining opers
  43.         {
  44.             output = output + theStack.pop(); // write to output
  45.         }
  46.         return output; // return postfix
  47.     } // end doTrans()
  48.     //--------------------------------------------------------------
  49.     public void gotOper(char opThis, int prec1)
  50.     { // got operator from input
  51.         while( !theStack.isEmpty() )
  52.         {
  53.             char opTop = theStack.pop();
  54.             if( opTop == '(' ) // if it’s a ‘(‘
  55.             {
  56.                 theStack.push(opTop); // restore ‘(‘
  57.                 break;
  58.             }
  59.             else // it’s an operator
  60.             {
  61.                 int prec2; // precedence of new op
  62.                 if(opTop=='+' || opTop=='-') // find new op prec
  63.                     prec2 = 1;
  64.                 else if(opTop == '*' || opTop == '/')
  65.                     prec2 = 2;
  66.                 else
  67.                     prec2 = 3;
  68.                 if(prec2 < prec1) // if prec of new op less
  69.                 { // than prec of old
  70.                     theStack.push(opTop); // save newly-popped op
  71.                     break;
  72.                 }
  73.                 else // prec of new not less
  74.                     output = output + opTop; // than prec of old
  75.             } // end else (it’s an operator)
  76.         } // end while
  77.         theStack.push(opThis); // push new operator
  78.     } // end gotOp()
  79.     //--------------------------------------------------------------
  80.     public void gotParen(char ch)
  81.     { // got right paren from input
  82.         while( !theStack.isEmpty() )
  83.         {
  84.             char chx = theStack.pop();
  85.             if( chx == '(' ) // if popped ‘(‘
  86.                 break; // we’re done
  87.             else // if popped operator
  88.                 output = output + chx; // output it
  89.         } // end while
  90.     } // end popOps()
  91.     //--------------------------------------------------------------
  92. } // end class InToPost