Sie sind auf Seite 1von 2

Programming Assignment 3 : Prefx to Postfx

Expression Conversion
Your assignment is to write a program that converts a prefix expression to a postfix expression.
The linked implementations of ADT Stack and ADT Queue are to be used for this project.
Input
The input file "Prefix.in" contains a series of mostly error-free, simple arithmetic expressions in
prefix notation. A few will be invalid input sequences and your program will detect this and
identify them as such. There is one expression per line. The expressions are made up of two types
of symbols: (1) single-letter operands and (2) the operators +, -, *, and /. There may be zero, one,
or more blanks between symbols. All input sequences will involve only single-letter operands and
the operators +, -, *, and /.
Output
All output should be written to the output file, "Postfix.out". Each prefix expression from the input
should be echoprinted, with an appropriate label. After your program converts the input
expression to its postfix equivalent, the postfix expression should be printed to the output file, with
an appropriate label.
Sample:
Prefix: + * A B / C D
Postfix: A B * C D / +
Prefix: * A + B / C D
Postfix: A B C D / + *
Prefix: - * A + B / C D E
Postfix: A B C D / + * E -
Prefix: * + A B - C D
Postfix: A B + C D - *
Although your program does not process or print infix, here are the four infix expressions that
correspond to the above forms:
A * B + C / D
A * (B + C / D)
A * (B + C / D) - E
(A + B) * (C - D)
Discussion
The key idea here is that after a portion of an expression has been converted, it is to be treated as a
single operand. Assume that every operator is associated with a flag. The flag is initially "off" to
signal that neither of the two operands corresponding to the given operator has been processed
yet. When the first operand is processed, the flag is switched "on." When the second operand is
processed, the given operator may immediately be appended to the output string.
Below is a description of a simple prefix-to-postfix conversion algorithm that works on one
input expression:
1. Initialize the input queue, the output queue, and the operators stack.
2. Repeat steps 3-5 until there are no more input symbols.
3. Get the next input symbol.
4. If it is an operator, put it on the stack with its associated flag off.
5. If it is an operand, append it to the output queue. Furthermore, the operand must correspond
to the top operator on the stack. (Why?) Of course, the stack cannot be empty at this point.
(Why?) Finally, here is the important question: is the operand just processed the first or the
second operand associated with the top operator? If it is the second, it is time to append the
top operator to the output queue. Now, remembering the key idea at the beginning of this
section, determine what happens to the remaining operators on the stack.
Examples
Here is a hand simulation of the first sample input expression. The rightmost symbol under Stack
is the top operator. Parentheses around an operator denote that the associated flag is on. The
"Stack" and "Out So far" columns show the relevant information following the processing of the
symbol in the "Next Prefix Symbol" column.
NEXT PREFIX QUEUE SYMBOL STACK OUTPUT QUEUE
+ +
* + *
A + (*) A
B (+) A B *
/ (+) / A B *
C (+ /) A B * C
D A B * C D / +
Deliverables
1. All code for classes and interfaces used for implementing ADTs Stack and Queue (linked
implementation).
2. Specification and implementation of your program (hard copy and on disk).
3. Listing of your implemented test plan, and of the output produced by your program. Include
at least 12 different input strings in this test, some of which are invalid input.
Optional for Extra Credit or Just Fun (5 points): In our text, a similar programming assignment is
described for translating an infix expression to a postfix expression. If you wish, you can do both
the translation from prefix to postfix and from infix to postfix in the same project
Project is due on Tuesday, October 25th 2005, at beginning of class.

Das könnte Ihnen auch gefallen