Practice with fractions
Problem statement
Write a program to test a student's understanding of arithmetic with rational numbers (fractions). For example:
3/2 + 1/3 is 11/6
5/4 * 2/9 is 5/18
etc.
Procedure
Follow these steps to end up with a sucessful program:
- The problem statement is given above.
- Write the problem description.
- Do the analysis for object design, choosing suitable representations for the class members. Write the object design down.
- Do the analysis for process design, adding suitable function members to your classes with appropriate parameters. The application class with have (at least) a function member
run
. The class for fractional numbers must have (at least) overloaded operators for +
, -
,*
, /
, assignment (=
) and equality (==
).
- Write C++ program code from your analysis in 3 and 4.
- Create a source file using the editor.
- Compile the source code using g++.
- The program should present a random problem to the students, read the student's answer and check it against the actual value.
- Deliverables:
- your design document with its four sections: problem statement, problem description, object design and process design.
- your source code, suitably commented and with good layout
- the output produced by the program with the tests as given in 8.
- your source code e-mailed to the grader, login name iflores.
Notes and hints
- Keep your classes to the minimum necessary (but use an application class)
- Keep the member representations as simple as possible. You do not need anything more fancy than base types
- Keep all data private: use accessor functions to selectively open up access to other classes. Don't use global variables!!
- display the result to the student in its simplest form -- use this version greatest common divisor -- make it a private function:
long gcd(long x, long y) {
return (x == 0) ? y : gcd(y % x, x);
}
- use this format for inputting fractions: <numerator>/<denominator>, e.g. 1/2, 25/7, etc. You will need to check for correct format on input of the answer. Prompt the user for this input
- Use this for generating random integers:
#include <ACG.h>
#include <RndInt.h>
...
RandomInteger n(1, 10, new ACG);
...
n() // each "call" produces a different value
Don't worry about seeding the random number generator.
- end of input on UNIX is signalled by typing ctrl-D on a line by itself (ctrl-Z in DOS)
- display screen output with reasonable layout
- use
cin
and cout
, not printf
and scanf
- when you are ready to produce the final output of the program, redirect the standard input to cin from a file, and output produced by
cout
into a file by typing:
a.out > outputfile < inputfile
outputfile can then be printed on the lineprinter.
- mail your source code with:
mail iflores < filename.cc