// // Program name: test_rationals_v2 // Author: B. Massingill // // Purpose: test RationalNum class (version 2) // #include #include "rationals_v2.h" // class definition // -------- function prototypes -------- // precondition: // postcondition: functions of RationalNum class have been tested, // using p and q void testRationalNum(RationalNum p, RationalNum q); // -------- main program -------- int main() { char prompt1[] = "Enter first number, in the form m/n, " "or ctrl-D to end: "; char prompt2[] = "Enter second number, in the form m/n: "; RationalNum p; RationalNum q; cout << prompt1; while (cin >> p) { cout << prompt2; cin >> q; testRationalNum(p, q); cout << prompt1; } cout << endl; return 0 ; } // -------- function definitions -------- void testRationalNum(RationalNum p, RationalNum q) { cout << "p = " << p << endl; cout << "q = " << q << endl; p.reduce(); q.reduce(); // test comparisons if (p == q) cout << "p == q" << endl; if (p < q) cout << "p < q" << endl; if (p > q) cout << "p > q" << endl; if (p <= q) cout << "p <= q" << endl; if (p >= q) cout << "p >= q" << endl; if (p != q) cout << "p != q" << endl; // test reduce cout << "reduced p = " << p << endl; cout << "reduced q = " << q << endl; // test arithmetic functions cout << "p + q = " << p + q << endl; cout << "p - q = " << p - q << endl; cout << "p * q = " << p * q << endl; cout << "p / q = " << p / q << endl; cout << "-p = " << -p << endl; // test in-place arithmetic RationalNum p_copy; p_copy = p; p_copy += q; cout << "after p+=q, p = " << p_copy << endl; p_copy = p; p_copy -= q; cout << "after p-=q, p = " << p_copy << endl; p_copy = p; p_copy *= q; cout << "after p*=q, p = " << p_copy << endl; p_copy = p; p_copy /= q; cout << "after p/=q, p = " << p_copy << endl; return; }