I am having a little difficulty in creating a function called display2() that will display the result of an addition, subtraction, multiply and division fraction as a mixed fraction (for example the result should output say 1 3/4). I have 3 files called fract.h, fract.cc and TestClassFract, as shown below:
// file fract.h
// a Fraction Abstract Data Type
#ifndef FRACTION_H
#define FRACTION_H
// The DOMAIN OF VALUES
class Fraction
{
private:
int num;
int denom;
public:
// The SET OF OPERATIONS
Fraction(); // default constructor
// initialises fraction with numerator up.
// denominator down
// returns 0 if no error
int create(int up, int down);
// displays in form up/down
void display();
// displays in form up/down
void display2();
// reduce to lowest terms
void simplify();
// adds another to produce third in simplified form
void add(Fraction, Fraction&);
// and now the others
void subtract(Fraction, Fraction&);
void multiply(Fraction, Fraction&);
void divide(Fraction, Fraction&);
};
#endif
How do you perform this calculation in your head? Write the code the same way.
5/3 == 1 using integer division. To get the remainder, 5 - 1*3 = 2.
On the other hand, if the numerator is strictly less than the denominator then the whole part is 0, which is typically not written, so display2() can simply revert to calling display1() in that case.