//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 6.18 on page 121
//  A modification of Example 6.5

import java.util.Stack;
import java.io.*;

class CharStack
{ private char[] s = new char[1000];
  private int top=-1;
  public boolean empty() { return top<0; }
  public char peek() { return s[top]; }
  public char pop() { return s[top--]; }
  public void push(char ch) { s[++top] = ch; }
}

public class Pr0618
{ public static void main(String[] args)
  { try
    { CharStack stack = new CharStack();
      InputStreamReader reader = new InputStreamReader(System.in);
      StreamTokenizer tokens = new StreamTokenizer(reader);
      tokens.ordinaryChar('/');
      tokens.eolIsSignificant(true);
      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(ch);
        else if (ch==')')
          System.out.print(stack.pop()+" ");
      }
      while (!stack.empty())
        System.out.print(stack.pop()+" ");
    }
    catch (Exception e)
    { System.out.println(e);
    }
  }
}
