EECS 370

Machine Problem 4, Spring 2000

Stack Calculator

Due: Thursday, March 23, 2000 at 11:59 pm
Due: Monday, March 27, 2000 at 11:59 pm

Note: Turnin will not be disabled until Wednesday Morning (3/29/2000) about 9:00 am.

For this assignment, you are to program a stack calculator. The program will be a simplified version of the desk calculator UNIX command dc. A stack calculator uses reverse Polish notation (also called postfix notation) for expressioning equations. Hewlett/Packard calculators are examples of such calculators.

To use stack calculator, when a value is entered by the user, it is pushed on the stack. When an operator is entered by the user, the top two values are popped from the stack, the result of applying the operator to these two values is determined and the result is pushed onto the stack and also displayed to the user. Your program is to use the Standard Template Library class of stack vector to implement your stack. (See comments below on this change.)

The numbers on the calculator are written as a rational number, where the whole portion is represented as a integer and the decimal portion is represented as a fraction.  The denominator of the fraction is also aways reduced to the lowest possible power of 2 with the largest denominator being 128 (i.e. possible denominators are: 1, 2, 4, 8, 16, 32, 64, 128) and the numerator is always less than than demoninator.  This information is nearly identical to the StockPrice class you wrote for mp3. The form of the input of a numeric value will be

In order to determine which form an input was given in, your program may need to scan ahead in the input looking for the slash character separating the numerator and the denominator or the floating pointing separating the whole number from the decimal part. Note: each value will be given on a line of its own to be valid.

If the input is a floating point value, your program will need to convert it to its nearest proper fractional form. Also if the input has a fraction that does not have a proper demoninator value, your program will need to convert it to the nearest proper fractional form. Your program will have to check to see if the fractional form needs to be reduced. To convert to a proper fractional form use the following algorithm. (Note: as of the first writing, this algorithm has not been 100% tested, so it may change as testing dictates.) Check out the C++ program at ~i370/WWW/code/rattest.cpp.

// Modified 3/6/2000

double decPart = ????;   // assign the decimal part of the given value
int num = 0;
int den = 1;

if (decPart != 0.0)
   {
    double delta = decPart;		

    if (fabs(1.0 - decPart) < delta)   
       {
        delta = fabs(1.0 - decPart);
        num = 1;
        den = 1;
       }

    for (int exp = 1; i <= 7; i++)
       {
	int tempDen = pow (2, exp);
	for (int tempNum = 1; tempNum < tempDen; tempNum += 2)
	   {
	    if (fabs(decPart - (double(tempNum)/tempDen)) < delta)
	       {
		delta = fabs(decPart - (double(tempNum)/tempDen));
		num = tempNum;
		den = tempDen;
	       }
	   }
       }
   }

Your program will run interactively with the following commands.  Extra space or tab characters may be included in any command.  All commands consist of a single character.  Only one command will be issued on a line and each command will not span multiple lines. Unknown commands should result in an appropriate error message. All alphabetic commands can be given in either upper or lower case, with the exception of the s and l commands. Case causes a slightly different operation to occur for s and l.

Help - h
Provide help about the interactive commands for the program.

Quit the Program - q
The program is to quit.

Push a value onto the stack - <number>
The program is to push the given value onto the stack. The value may need to be converted into a proper fractional form.

Perform Arithmetic Operation - + | - | * | / | ^
The program is to add (+), subtract (-), multiply (*), divide (/) or exponentiate (^) the top two values from the stack. These two values are to be popped from the stack and the result of the operation is to be pushed on to the stack and displayed to the user.

Show the top value on the stack - p
The program is to display the top value on the stack. This value is NOT to be removed from the stack.

Show all values on the stack - f
The program is to display all values on the stack. The stack is not modified by this operation.

Remove the top value from the stack - r
The program is to remove the top element from the stack.

Clear all values from the stack - c
The program is to empty the stack.

Square root - v
The program is to replace the top element of the stack with its square root.

Duplicate the top value - d
The program is to push onto the stack the value at the top of the stack.

Store the top value from the stack - s<x> | S<x>
The program is to pop the value at the top of the stack and store it in a "register" named <x≷, where <x> may be ANY character. If s is capitalized (upper case), the register <x> is treated as a stack and the value is pushed onto it.

Load a new value on the stack - l<x> | L<x>
The program is to push onto the stack the last value stored in "register" <x>, where <x> may be ANY character value. All registers are to be initialize with a zero value. If l is in lower case, the register is not altered. If l is capitalized (upper case), the register is treated as a stack and its top value is popped onto the main stack. If the register is empty, place the value of zero onto the main stack.

Your program is to use the Standard Template Library class of map to implement the registers. Instead of storing the register information in a STL stack, your may wish to use the Standard Template Library class of vector. The vector class givens you more control which may be useful when dealing wiht the upper case and lower case versions of the s and l commands. (see below).

New Ideas on Which STL Items to Use

Therefore; your program must use the STL vector for your main stack, the STL map for your register set and the STL stack for each register.

The following are errors that your program must deal with. When an error is encountered print an appropriate message and stop performing the operation (do NOT exit yor program).

This program will require the use of multiple source code files, separate compilation and the use of a makefile. The division of the subroutines between the multiple source code files must be logical. One suggestion would be to have each class in its own source code file and the command interface commands in another source code file. Note: a "source code file" is not the same as a "header file". Source code files will have a file extension of .cpp (or .c, .C, .CC, etc.), while a header file will have a file extension of .h. Your program must separately compile the source code files and then link them together. Using a #include statement with a source code file will NOT satisfy this requirement.

This program will also require a 1-2 page write up of the data structures used in the program and the logical division of your program into multiple source code files (i.e. which routines are where). Remember that this write-up is to be written in ASCII format and is to be electronically turned in with your program. The name of this file should be "readme.txt". Also recall that your program will be given to another student to write a critique. Therefore, it is suggested that you do not include your Social Security Number in your program. Instead use your name and your EECS User ID to identify yourself.

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 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, header files, make file as well as your program description. Failure to turnin all required pieces will result in a lower grade for the assignment.