//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 6.05 on page 114
//  Converting infix to postfix

import java.util.Stack;
import java.io.*;

public class Ex0605
{ public static void main(String[] args)
  { try
    { Stack stack = new Stack();
      InputStreamReader reader = new InputStreamReader(System.in);
      StreamTokenizer tokens = new StreamTokenizer(reader);
      tokens.ordinaryChar('/');  // otherwise, this would be a comment
      tokens.eolIsSignificant(true);  // default is false
      int tokenType;
      System.out.print("Enter an infix expression: ");
      while ((tokenType=tokens.nextToken()) != StreamTokenizer.TT_EOL)
      { char ch = (char)tokenType;
        if (tokenType==StreamTokenizer.TT_NUMBER)
          System.out.print(tokens.nval + " ");
        else if (ch=='+' || ch=='-' || ch=='*' || ch=='/')
          stack.push(new Character(ch));
        else if (ch==')')
          System.out.print((Character)stack.pop()+" ");
      }
      while (!stack.empty())
        System.out.print((Character)stack.pop()+" ");
    }
    catch (Exception e)
    { System.out.println(e);
    }
  }
}
