// CS 202 HW7 // Problem 1 // the head file: rational.h #ifndef rational_h #define rational_h #include using std::istream; using std::ostream; class Rational //class for a rational fraction { public: // constructors Rational(); Rational(int num2); Rational(int num2, int den2); // overloaded operators using friend function friend Rational operator*(const Rational& lhs, const Rational& rhs); friend Rational operator/(const Rational& lhs, const Rational& rhs); friend Rational operator+(const Rational& lhs, const Rational& rhs); friend Rational operator-(const Rational& lhs, const Rational& rhs); friend bool operator< (const Rational& lhs, const Rational& rhs); friend bool operator== (const Rational& lhs, const Rational& rhs); friend Rational operator-(const Rational& lhs); friend ostream& operator<<(ostream& out, const Rational& lhs); //output function friend istream& operator>>(istream& in, Rational& lhs); //input function private: int num; int den; }; #endif // CS 201 HW7 // Problem 1 // Implementation file: raional.cpp #include "rational.h" using std::endl; //default constructor Rational::Rational():num(0), den(1) { } //constructor with 2 parameters Rational::Rational(int num2, int den2) { num=num2; den=den2; } //constructor with 1 parameter Rational::Rational(int num2):den(1) { num=num2; } // overloaded operators Rational operator*(const Rational& lhs, const Rational& rhs) { return Rational(lhs.num*rhs.num, lhs.den*rhs.den); } Rational operator/(const Rational& lhs, const Rational& rhs) { return Rational (lhs.num*rhs.den, lhs.den*rhs.num); } Rational operator+(const Rational& lhs, const Rational& rhs) { return Rational ((lhs.num*rhs.den)+(lhs.den*rhs.num), lhs.den*rhs.den); } Rational operator-(const Rational& lhs, const Rational& rhs) { return Rational ((lhs.num*rhs.den)-(lhs.den*rhs.num), lhs.den*rhs.den); } bool operator<(const Rational& lhs, const Rational& rhs) { return ((lhs.num*rhs.den)<(lhs.den*rhs.num)); } bool operator==(const Rational& lhs, const Rational& rhs) { return ((lhs.num*rhs.den)==(lhs.den*rhs.num)); } Rational operator-(const Rational& lhs) { return Rational (-lhs.num, lhs.den); } ostream& operator<<(ostream& out, const Rational& lhs) { out << lhs.num << "/" << lhs.den; return out; } istream& operator>>(istream &in, Rational& lhs) { in >> lhs.num; in >> lhs.den; return in; } // CS 201 HW7 // Problem 1 // the test file: driver.cpp #include #include "rational.h" using namespace std; int main() { const Rational onehalf(1,2); const Rational five(5); Rational x= five * onehalf; cout << "Five times one half is " << x << endl; cout << "Five divided by one half is " << five/onehalf << endl; cout << "One half minus five is " << onehalf-five << endl; cout << "(1/2 - (-1/3)) * 6 = " << (Rational(1,2) - -Rational(1,3))*Rational(6) << endl; cout << "Test the \"less\" operator where ab: " << endl; for(int ii=0;ii<3;++ii) { Rational a,b; cout << "Enter two rational numbers "; cin >> a; cin >> b; cout << a; if (a < b) cout << " is less than "; else if (a == b) cout << " is equal to "; else cout << " is not less than "; cout << b << endl; } return 0; } /* Five times one half is 5/2 Five divided by one half is 10/1 One half minus five is -9/2 (1/2 - (-1/3)) * 6 = 30/6 Test the "less" operator where ab: Enter two rational numbers 1 2 -2 4 1/2 is not less than -2/4 Enter two rational numbers 2 -4 -1 2 2/-4 is equal to -1/2 Enter two rational numbers 1 5 3 5 1/5 is less than 3/5 Press any key to continue */