EECS 370 - Standard Template Library

Machine Problem 4

Due date: Thursday November 11, 1999 at 11:59 pm

For this assignment, you are to use the Standard Template Library and your Unum class to create an infix expression evaluator. The input that you must evaluate will be in the form of either:

When an <expression> is given by itself in the input, the value of the expression is to be output to standard output. When an <expression> is preceded by "= <varname>", the value of the expression is stored under the key of <varname>. When the # symbol is given as the first character on the line, quit the program. An <expression> is any infix expression that uses the following operators:

    Operator   Order of Operation   Precedence
( )Left to RightHighest
^Right to Left
*, /Left to Right
+, -Left to RightLowest

The operands can be either a <varname> or a constant. The <varname> can be any single letter in either upper or lower case for a total of 52 different <varname>'s. The constants can be integers, floating points or complex numbers. The complex numbers will be in the form of NR+NIi, where N can be any positive or negative integer or floating point value.

For each <expression>, you will first need to convert it from infix to a postfix expression and then evaluate the postfix expression. The algorithms for both of these are contained in the data structure book written by Mark Allen Weiss, Data Structures & Algorithm Analysis in C, starting on page 72 (on page 103 in the C++ edition). This book is used for EECS 360. Both of these algorithms use a stack. You must use the STL class stack for these algorithms. In the infix to post conversion algorithm, you will most likely need to have the stack be a stack of characters. In the evaluation algorithm, you will most likely need to have a stack of Unum (as defined from MP 3).

The input will expect only one <expression> per line. If the <expression> is to be written on multiple lines, a backslash "\" will be used at the end of each line to indicate the <expression> will be continued onto the next line. While we can assume each line will have a maximum line of 100 characters, we can make no assumptions on the length of an <expression>. To deal with this, store each expression in the STL class vector. This will allow us not to worry about the unlimited maximum size.

Infix to Postfix Conversion

To convert from infix to postfix, we translate one vector (in infix form) to another vector (in postfix form). When an operand is encountered, it is moved directly from the infix vector to the postfix vector. When an operator is encountered in the infix vector, it is not moved directly to the postfix vector, instead it will be pushed on the stack. Depending on what operand is encountered, we may pop operands from the stack before pushing the current operand.

Postix Expression Evaluation

When an operand is encountered, its value is pushed onto the stack. When an operator is encountered, two values are popped from the stack (since every operator is a binary operator), the operation indicated by the operator is performed and the result is pushed onto the stack (pay attention to which value is the left-hand-side operand and which is the right-hand-side operand). If the stack does not contain two values when an operator is encountered, an error has occurred. When we reach the end of the postfix expression, there should be only one value left on the stack which is the value for the <expression>. If there is not a single value on the stack, then an error has occurred.

Use of <varname>

The <varname>'s are to store the values in the STL class map. This will allow a value evaluated by one <expression> to be used in another <expression>. When the operand in an <expression> is a <varname>, your program is to retrieve the value from the map. You must check that <varname> already has a stored value. If not, then an error has occurred. For the input that begins with an equal sign, you are to store the value of the <expression> (if no error occurred in the <expression>) into the map.

When an error occurs, you are to print out some appropriate message to the standard error and continue evaluation at the next <expression> input. You may assume that all operators and operands are separated by white space. For 10 points extra credit, your program must work even it the operands and operators are not separated by white space. An example input that is not white space separated is:

     b+(a+-32+5i)*-2
The hard part will be determining between the addition operator and the plus sign used to separate the real and imaginary parts of a complex number and between the subtraction operator and the minus sign to indicate a negative constant.

Last 4 paragraphs from mp1

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% your own. You are not allowed to share code with any other person (inside this class or not). You may discuss the project with other persons; however, you may not show any code you write to another person nor may you look at any other person's written code.

You are also to write a one to two page program description. This write up must be submitted with your program and be in ASCII text format. This description is to explain your internal data structures, code structures and the algorithms used in your program. Remember, this program description will be read by another student when the critiques are done for this assignment. Often the title of "readme" is used for these types of documents.

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