Programming Problem #46
"Simplify It"

Your job is to write a program to simplify arithmetic expressions.

The input consists of one or more lines, each of which contains a valid expression. Lines will be at most 255 characters long. Expressions will consist of integer constants and the operators +, -, *, and /. The / operator means integer division (discard any remainder). * and / have the highest priority, and + and - have the lowest priority. Operators of equal priority are processed from left to right. Expressions will not contain parentheses. No expression will require division by zero. The final result of an expression will fit into a 32-bit signed integer.

For each expression, output a sequence of expressions (starting with the given expression) showing the steps necessary to simplify it. Separate the output for different expressions by a blank line. Within an expression, minus signs must immediately precede the number, and constants and operators must be separated by exactly one space.

Input must be read from the file prob46.in, and output must be written to the file prob46.out. All output to the screen will be ignored.

Example Input

4 + 5 / 6
5
3 + 5 * -6 - 7

Example Output

4 + 5 / 6
4 + 0
4

5

3 + 5 * -6 - 7
3 + -30 - 7
-27 - 7
-34

A version of this problem originally appeared in the 1996 ACM Mid-Central regional programming contest.

Dr. Eric Shade