/************************************************************************
* *
* Rational Test *
* *
************************************************************************/
#include <iostream>
#include <string>
#include "rational.h"
using namespace std;
int main() {
// Test the Rational constructors
cout << "Test the constructors" << endl << endl;
Rational r;
cout << "The value of r is " << r << endl;
Rational s(2);
cout << "The value of s is " << s << endl;
Rational t(2, 3);
cout << "The value of t is " << t << endl;
Rational u = 3;
cout << "The value of u is " << u << endl;
Rational v = Rational(4, 5);
cout << "The value of v is " << v << endl;
Rational w = s + t;
cout << "The value of w is " << w << endl;
// Test the extraction and insertion operators
cout << endl << "Test the extraction and insertion operators" << endl << endl;
cout << "Enter a rational value: " << flush;
cin >> r;
cout << "r = " << r << endl;
// Construct some Rational constants
cout << endl << "Test the arithmetic operators" << endl << endl;
r = Rational(1, 3);
s = Rational(1, 4);
cout << r << " + " << s << " = " << r + s << endl;
cout << r << "  " << s << " = " << r  s << endl;
cout << r << " * " << s << " = " << r * s << endl;
cout << r << " / " << s << " = " << r / s << endl;
// Rational type cast
cout << endl << "Test mixing of ints, floats, and Rationals and typecasting" << endl << endl;
cout << "Rational + int: " << r << " + " << 2 << " = " << r + 2 << endl;
cout << "int + Rational: " << 2 << " + " << r << " = " << 2 + r << endl;
cout << "Rational + float: " << r << " + " << 2.8 << " = " << r + 2.8 << endl;
cout << "float + Rational: " << 2.8 << " + " << r << " = " << 2.8 + r << endl;
cout << "Assign int 2 to Rational" << endl;
r = 2;
cout << "r = " << r << endl;
cout << "Assign float 2.8 to Rational" << endl;
r = 2.8;
cout << "r = " << r << endl;
cout << "Typecast int 2 as Rational" << endl;
cout << "(Rational) " << 2 << " is " << (Rational) 2 << endl;
cout << "Typecast float 2.8 as Rational" << endl;
cout << "(Rational) " << 2.8 << " is " << (Rational) 2.8 << endl;
// Test compound assignment
cout << endl << "Test the compound assignments" << endl << endl;
r = Rational(1, 3);
s = Rational(1, 4);
cout << "r = " << r << endl;
cout << "Add " << s << " to r" << endl;
r += s;
cout << "r = " << r << endl;
cout << "Subtract " << s << " from r" << endl;
r = s;
cout << "r = " << r << endl;
cout << "Multiply r by " << s << endl;
r *= s;
cout << "r = " << r << endl;
cout << "Divide r by " << s << endl;
r /= s;
cout << "r = " << r << endl;
// Test increment and decrement
cout << endl << "Test increment and decrement" << endl << endl;
cout << "r = " << r << endl;
cout << "(Preincrement) r = " << ++r << endl;
cout << "(Postincrement) r = " << r++ << endl;
cout << "r = " << r << endl;
cout << "(Predecrement) r = " << r << endl;
cout << "(Postdecrement) r = " << r << endl;
cout << "r = " << r << endl;
// Test the relational operators
// Test operators when r == s
cout << endl << "Test the relational operators when r == s" << endl << endl;
r = s;
if (r == s)
cout << r << " == " << s << endl;
else
cout << r << " != " << s << endl;
if (r != s)
cout << r << " != " << s << endl;
else
cout << r << " == " << s << endl;
if (r < s)
cout << r << " < " << s << endl;
else
cout << r << " >= " << s << endl;
if (r > s)
cout << r << " > " << s << endl;
else
cout << r << " <= " << s << endl;
if (r <= s)
cout << r << " <= " << s << endl;
else
cout << r << " > " << s << endl;
if (r >= s)
cout << r << " >= " << s << endl;
else
cout << r << " < " << s << endl;
// Test relational operators when r < s
cout << endl << "Test the relational operators when r < s" << endl << endl;
r = s  1;
if (r == s)
cout << r << " == " << s << endl;
else
cout << r << " != " << s << endl;
if (r != s)
cout << r << " != " << s << endl;
else
cout << r << " == " << s << endl;
if (r < s)
cout << r << " < " << s << endl;
else
cout << r << " >= " << s << endl;
if (r > s)
cout << r << " > " << s << endl;
else
cout << r << " <= " << s << endl;
if (r <= s)
cout << r << " <= " << s << endl;
else
cout << r << " > " << s << endl;
if (r >= s)
cout << r << " >= " << s << endl;
else
cout << r << " < " << s << endl;
// Test relational operators when r > s
cout << endl << "Test the relational operators when r > s" << endl << endl;
r = s + 1;
if (r == s)
cout << r << " == " << s << endl;
else
cout << r << " != " << s << endl;
if (r != s)
cout << r << " != " << s << endl;
else
cout << r << " == " << s << endl;
if (r < s)
cout << r << " < " << s << endl;
else
cout << r << " >= " << s << endl;
if (r > s)
cout << r << " > " << s << endl;
else
cout << r << " <= " << s << endl;
if (r <= s)
cout << r << " <= " << s << endl;
else
cout << r << " > " << s << endl;
if (r >= s)
cout << r << " >= " << s << endl;
else
cout << r << " < " << s << endl;
// Test the other member functions
cout << endl << "Test the other member functions" << endl << endl;
r = Rational(8, 3);
cout << "r = " << r << endl;
cout << "The integer part of " << r << " is " << r.IntPart() << endl;
cout << "The fractional part of " << r << " is " << r.FracPart() << endl;
cout << "The nearest integer to " << r << " is " << r.Round() << endl;
cout << r << " is approximately equal to " << r.Approx() << endl;
cout << r << " rounded to the nearest 8th is ";
cout << r.RatApprox(8) << endl;
cout << r << " rounded to the nearest 10th is ";
cout << r.RatApprox(10) << endl;
cout << endl << "Goodbye" << endl;
return 0;
}
