Bill Teter
Office:          149 Redcay
Telephone:    2782
Office Hours:  Tuesday, Thursday 9:00-12:00

email:  william.teter@plattsburgh.edu

       

 

Lab 10        Stacks and Postfix Notation

Write a program that uses a stack to translate an infix algebraic expression into the equivalent postfix expression.  The tokens in the infix expression will be positive integers and the operators +, *, - and /.  There will be no parentheses.  The tokens will be separated by exactly one blank.  Input will be one command line argument.  Note that you will need to surround the argument with quotes.  Call your program Postfix.  If your program is run with the command:

        java Postfix "2 + 3 * 4 - 5"

The output should be:

         2 3 4 * + 5 -

Use the generic stack from the Java API.  Be sure to check the specifications of the methods.  They differ from our text's conventions.

 

Extra Credit

1.  Allow parentheses in your infix expression

2.  Make the program robust so that it never crashes but prints a warning "invalid expression" for any input that is not an expression.  We define an expression recursively as follows:

        An expression is either an expression followed by an operator followed by an expression or an integer or a "(" followed by an expression followed by ")".  An operator is either +, *, - or /.

3.  Write your own generic stack implementation using a linked structure.