EECS 360 - Machine Problem 2

Expression Trees

Due: 11:59 pm on Thursday 9/21/2000

For this assignment you must work alone.

For this assignment, you are to write a C/C++ program that will transform an infox expression into an expression tree, print the tree in a "tree-like" format and compute the value of the expression tree. The infix expression will contain unsigned integers as operands, binary operands and parentheses. The operators will have the following level of precedence

  1. (, ) : parentheses (highest precedence)
  2. ^ : exponentiation
  3. *, / : multiplication and division
  4. +, - : addition and subtraction

The input to the program will be a number of infix expressions. For each expression, you are to first print out the expression just as it was read in, then print out the expression tree for the expression and finally print out the value the expression tree evaluates to. When your program encounters the expression of -1, your program is to quit. As with the previous assignment, the input must be read from standard input; therefore, the input can be entered at runtime or redirected from an input file.

You can assume that the input expression is correct (i.e. no error checking is required). There may be spaces between the operands and the operators. Each infix expression will be given on its own line of input and will be less than 80 characters in length. You may assume that the integer operands will be of value from 0 to 999.

Sample Input

     5+3+4-(6/2)
     35 -    70/11 +90 *3* 6 -(9+7*(45-6) )
     (14 - 19 *8 -4 * (14/2-3))
     8 ^ 2 + 7
     -1

Program Output Summary

For each infix expression read in:
  1. Print out the infix expression.
  2. Print out the expression tree in a tree-like form. Assume we have the expression 5+3*4-(6/2). Its expression tree could look as follows:
    			2
    		/
    			6
    	-
    				4
    			*
    				3
    		+
    			5
    
    If you rotate the tree 90 degrees, it will look like what we normally draw. This can be done using a modification of one of the tree traversal algorithms.
  3. Print out the value the expression tree calculates. This is also done by using a modification of one of the tree traversal algorithms.

Notes

General Comments

You must determine the value of the expression by evaluating the expression tree. Any other method (i.e. directly evaluating an infix or postfix expression) will result in a grade of zero for the assignment. You are not allowed to used the C++ Standard Template Libraries (or any other library set) to implement any needed stack or queue or tree for this assignment.

Your program must be written in good programming style. This includes (but is not limited to) meaningful identifier names, a file header at the beginning of each source code file, a function header at the beginning of the function, proper use of blank lines and indentation to aide in the reading of your code, explanatory "value-added" in-line comments, etc.

The work you turn in must be 100% done by your own own. You are not allowed to share code with any other students (inside this class or not). You may discuss the project with other students; however, you may not show any code you write to another student nor may you look at any other student's written code.

You are to submit this project using the EECS Department's UNIX machine's turnin command. The project name for this assignment is mp2. Be sure to submit all source code and header files and makefile. Failure to turnin all required pieces will result in a lower grade for the assignment. The turnin command for this assignment is:

     turnin -c eecs360a -p mp2 <file list>
where <file list> is the names of all files that you will submit for this program. You can use the turnin command multiple times; however, ever time you must submit all files needed for the program. To see which files you have submitted to turnin use the -v option with the turnin command as follows:
     turnin -c eecs360a -p mp2 -v