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.
Your program is to use the Standard Template Library class of map
to implement the registers.
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.
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.