// A class that represents an infix expression.
// Barry Cornelius, 19 June 2000
public class Infix
{
   private String iInfixString;
   private StringBuffer iPostfixStringBuffer;
   private Stack iOpStack;
   public Infix(final String pInfixString)
   {
      iInfixString = pInfixString;
      iOpStack = new LinkedStack();
   }
   public String toReversePolish()
   {
      iPostfixStringBuffer = new StringBuffer();
      for (int tCharNumber = 0; tCharNumber<iInfixString.length();
                                tCharNumber++)
      {
         iProcessChar(iInfixString.charAt(tCharNumber));
      }
      iProcessEndOfExpression();
      return iPostfixStringBuffer.toString();
   }
   private void iProcessChar(final char pChar)
   {
      switch (pChar)
      {
         case '+':
         case '-':
         case '*':
         case '/':
         {
            iProcessOp(pChar);
         }
         break;
         case ' ':
         {
            // do nothing
         }
         break;
         default:
         {
            iPostfixStringBuffer.append(pChar);
         }
         break;
      }
   } //BJCHEREFIRST
   private void iProcessOp(final char pOp)
   {
      while ( iOpStack.size()!=0 && iPriority(iTopChar())>=iPriority(pOp) )
      {
         iPostfixStringBuffer.append(iTopChar());
         iOpStack.remove();
      }
      // iOpStack.size()==0 !! iPriority(iTopChar())<iPriority(pOp)
      iOpStack.add(new Character(pOp));
   }
   private void iProcessEndOfExpression()
   {
      while ( iOpStack.size()!=0 )
      {
         iPostfixStringBuffer.append(iTopChar());
         iOpStack.remove();
      }
      // iOpStack.size()==0
   }
   private char iTopChar()
   {
      final Character tTopCharacter = (Character)iOpStack.getFirst();
      return tTopCharacter.charValue();
   }
   private int iPriority(final char pOp)
   {
      int tPriority = 0;
      switch (pOp)
      {
         case '*': 
         case '/':
         {
            tPriority = 20;
         }
         break;
         case '+': 
         case '-':
         {
            tPriority = 10;
         }
         break;
      }
      return tPriority;
   }
}
