/****************************************************************************** * Compilation: javac InfixToPostfix.java * Execution: java InfixToPostFix * Dependencies: Stack.java StdIn.java StdOut.java * * Reads in a fully parenthesized infix expression from standard input * and prints an equivalent postfix expression to standard output. * * Windows users: replace [Ctrl-d] with [Ctrl-z] to signify end of file. * * % java InfixToPostfix * ( 2 + ( ( 3 + 4 ) * ( 5 * 6 ) ) ) * [Ctrl-d] * 2 3 4 + 5 6 * * + * * % java InfixToPostfix * ( ( ( 5 + ( 7 * ( 1 + 1 ) ) ) * 3 ) + ( 2 * ( 1 + 1 ) ) ) * 5 7 1 1 + * + 3 * 2 1 1 + * + * * % java InfixToPostfix | java EvaluatePostfix * ( 2 + ( ( 3 + 4 ) * ( 5 * 6 ) ) ) * [Ctrl-d] * 212 * ******************************************************************************/ public class InfixToPostfix { public static void main(String[] args) { Stack stack = new Stack(); while (!StdIn.isEmpty()) { String s = StdIn.readString(); if (s.equals("+")) stack.push(s); else if (s.equals("*")) stack.push(s); else if (s.equals(")")) StdOut.print(stack.pop() + " "); else if (s.equals("(")) StdOut.print(""); else StdOut.print(s + " "); } StdOut.println(); } }