The objective of this assignment is to make us of C++ and templates and STL to re-implement the rational number data type and calculator from assignment 2.
Rational numbers are numbers that can be represented as a ratio of an integer and a non-zero positive integer. These are sometimes useful when precision is very important. For example, 1/3 cannot be precisely represented using any of the standard numeric types in C++ (or most other widely used programming languages).
Your rational number class should be called rational and must useable with the pre-existing number types in C++. Specifically it should be possible to write expressions of the form x op y, where op is any of the four standard arithmetic operators (+, -, *, /) and either of x or y are rational. If the other operand is an integral type (int or long) then the result should be rational. If the other operand is float or double, then the result should be float or double respectively. This can be done by either providing appropriate overloaded operator functions, or by using constructor and conversion operators to provide conversions.
For this assignment you must create a polymorphic number calculator using C++ templates. You must then create two instantiations of your template calculator to create a calculator that operates on the C++ type double and one that operates on your new type rational. The specification for the calculator is essentially the same as the one for assignment 2 with the minimum modifications necessary to remove the base type from the specification.
Input: The input will be from standard input (which will be redirected to read from a file for obvious reasons.. The input (file) will contain a sequence of “commands” one per line. A command will not contain any white space characters. Any characters on a line following a white space character (space or tab) should be ignored. The possible commands are:
Output:
Implementation:
You are strongly encouraged to use the STL stack class as the stack for your calculator (type “man stack” at a Unix prompt to see learn more).
For this assignment you will need to submit a makefile and any required source files. Your makefile should create two executables named rcalc and dcalc that run the rational calculator and double calculator respectively. There should be at least 6 source files:
You must always print rational numbers in simplest form (no common factors except 1 in the numerator and denominator).
Print any rational with a zero numerator simply as “0”.
Print any rational with one in the denominator as an integer (just the numerator - no slash).
Here is a sample input file and the corresponding output when this input file is read using file redirection.
Sample Input:
Sample Input:
-1/3
1/3
+
1/2
+
3/4
*
3/8
/
2
3
+
*
300001/3
dup
+
top
pop
0
+
Sample Output:
-1/3
1/3
0
1/2
1/2
3/4
3/8
3/8
1
2
3
5
5
300001/3
300001/3
600002/3
600002/3
600002/3
0
5
Print double values using the default C++ output stream format except with the precision set to 15 (use cout << setprecision(15)).
The input format for type double used for the first input bullet above must accept any legal C++ double format.
Sample Input:
1
3
/
dup
dup
+
1.0
3.0
/
+
1.234e56
+
pop
pop
Sample Output:
1
3
0.333333333333333
0.333333333333333
0.333333333333333
0.666666666666667
1
3
0.333333333333333
1
1.234e+56
1.234e+56
1.234e+56
0.333333333333333
TBS