2. Write a driver program to test your class.
----
A fraction number is the ratio of two integers and is typically represented in the manner a/b. The basic arithmetic operations have the following definitions:
Refer to this link on how to operate on fractions.
Reducing a fraction involves finding the Greatest Common Divisor, and then dividing all the terms by that amount.
Euclid’s Algorithm:
if x < y, then swap x and y
While y is not zero
remainder = x mod y
x = y
y = remainder
When you’re done, x is the GCD.
----
----
A fraction number is the ratio of two integers and is typically represented in the manner a/b. The basic arithmetic operations have the following definitions:
Refer to this link on how to operate on fractions.
Reducing a fraction involves finding the Greatest Common Divisor, and then dividing all the terms by that amount.
Euclid’s Algorithm:
if x < y, then swap x and y
While y is not zero
remainder = x mod y
x = y
y = remainder
When you’re done, x is the GCD.
----
//In fraction.h
class Fraction {
public: // member functions
// default constructor
Fraction();
// a second constructor
Fraction(int number, int denom=1);
// destructor
~Fraction();
// copy constructor
Fraction(const Fraction &r);
// member assignment
Fraction& operator=(const Fraction &r);
// can it be: void operator=(const Fraction &r); ?????
// some arithmetic and stream facilitators
Fraction Add(const Fraction &r) const;
Fraction Sub(const Fraction &r) const;
Fraction Multiply(const Fraction &r) const;
Fraction Divide(const Fraction &r) const;
void Insert(ostream &sout) const;
void Extract(istream &sin);
protected:
// inspectors
int GetNumerator() const;
int GetDenominator() const;
// mutators
void SetNumerator(int numer);
void SetDenominator(int denom);
private:
// data members
int NumeratorValue;
int DenominatorValue;
};
// Fraction ADT: auxiliary operator description
Fraction operator+(const Fraction &r, const Fraction &s);
Fraction operator-(const Fraction &r, const Fraction &s);
Fraction operator*(const Fraction &r, const Fraction &s);
Fraction operator/(const Fraction &r, const Fraction &s);
ostream& operator<<(ostream &sout, const Fraction &s);
istream& operator>>(istream &sin, Fraction &r);// can they be: void operator<<(ostream &sout, const Fraction &); ?????
//In fraction.cpp
<insert your answer here...>
//In driver_program.cpp
...
int main() {
Fraction r;
Fraction s;
cout << "Enter 1st rational number (a/b): ";
cin >> r;
cout << "Enter 2nd rational number (a/b): ";
cin >> s;
Fraction tr(r);
Fraction ts = s;
Fraction tts;
tts = s;
ts = tts = r;
Fraction Sum = r + s;
Fraction Difference = r - s;
Fraction Product = r * s;
Fraction Ratio = r / s;
cout << r << " + " << s << " = " << Sum << endl;
cout << r << " - " << s << " = " << Difference << endl;
cout << r << " * " << s << " = " << Product << endl;
cout << r << " / " << s << " = " << Ratio << endl;
return 0;
}
What you mean by "overload I/O operators to input and output fractions" sir?
ReplyDeleteostream& operator<<(ostream &sout, const Fraction &s);
ReplyDeleteistream& operator>>(istream &sin, Fraction &r);
okay thank you sir :)
ReplyDelete