Assignment 4

due Thursday, March 6

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 = answer
Equation();  //create some valid default Equation
bool set(Rational f, Operation oper, Rational s);  //returns false if there is a problem with the data
istream & read(istream & in);  //Reads an equation in the form Rational sym Rational
                                             // where sym is + - * or /
ostream & print(ostream & out); //Print the equation
static void setFormat(bool ); // true = with answer, false = no answer
Rational getFirstTerm();  // returns the first Rational
Rational getSecondTerm(); // returns the second Rational
Rational getAnswer(); // returns the answer
char 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
   2/3 + 1/5 = 13/15;

If we then have

   e.setFormat(false);
   e.print(cout);
we get
   2/3 + 1/5 =
 

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