A fraction practice program.
Part 1:
You are going to create an equation class, for equations with Rationals.
It will look like
enum Operation {plus, minus, mult, divide};
class Equation {
public:Equation( Rational f, Operation op, Rational s); //f op s = answerEquation(); //create some valid default Equationbool set(Rational f, Operation oper, Rational s); //returns false if there is a problem with the dataistream & read(istream & in); //Reads an equation in the form Rational sym Rational// where sym is + - * or /ostream & print(ostream & out); //Print the equationstatic void setFormat(bool ); // true = with answer, false = no answerRational getFirstTerm(); // returns the first RationalRational getSecondTerm(); // returns the second RationalRational getAnswer(); // returns the answerchar getOperation();// returns the symbol + - * or /private:Rational first;Rational second;Operation op;Rational answer;static bool withAnswer; //true, output should show answer.static char theOps[4]; //set to +, -, *, /
};
The set function, the alternate constructor, and ... should set
the value of answer.
You should use the Rational class, not repeat the code that
you used there.
(I will put up, next week, a version of the Rational class that
you may use, or you may use your own. You may modify the Rational
class, but only to improve it, not to have it do work that should be done
by the Equation class. In particular, you will need to modify the
input and output functions for Rational to use files.)
The two static data members should have their initial values set in
the cpp file, as if they were global variables.
The output operator prints an equation with or without the answer.
For example, if we already have the rationals
a = 2/3 b=1/5
then we want
Equation e;
e.set(a,plus,b);
e.setFormat(true);
e.print(cout);to produce the output
If we then have
e.setFormat(false);
e.print(cout);we get
Make sure your Equation class in running well by writing a test stub. Be sure you try all different kinds of equations. Also, remember that all functions should be short, not longer than a page, so complex tasks should be broken down further. If you need other functions to help, you may declare them in the class either as public if they will be useful to others, or a s private if you (and the member functions) are only one who will be using them.
2. Now that you have the working class, you should use it as part
of an interactive program for giving students practice with fractions.
There will be a file, fractiondata.txt, consisting of many problems of
the form
Rational operation Rational
for example
3/5 + 9/2
0/5 + 8/3
The first item in the file will be the number of problems in the file.
Sample file.
Your program should