/*
Define a class for rational numbers. A rational number is a number that can be represented
as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational
numbers. (By 1/2 we mean the everyday meaning of the fraction, not the integer division
this expression would produce in a C++ program.) Represent rational numbers as two values
of type int, one for the numerator and one for the denominator. Call the class Rational.
Include a constructor with two arguments that can be used to set the member variables of
an object to any legitimate values. Also include a constructor that has only a single
parameter of type int; call this single parameter wholeNumber and define the constructor
so that the object will be initialized to the rational number wholeNumber/1. Also include
a default constructor that initializes an object to 0 (that is, 0/1).
Overload the relational operators ==, <, <=, >, >=, and != so that they correctly apply
to type Rational.
Also write a test program to test your class.
Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are
positive rational numbers then a/b is less than c/d provided a*d is less than c*b.
A function to standardize the values stored so that the denominator is always positive
should be included. (An additional exercise would be to include a function to reduce
the rational number so the numerator and denominator are as small as possible.)
*/
// *****************************************************************
//
// Rationals.cpp
//
// This program contains a class for rational numbers and a
// driver to test the class. The rational class represents a
// rational number using two integers - one for the numerator
// and one for the denominator. The class has 3 constructors -
// a default constructor that sets the number to 1; a constructor
// with one integer parameter that sets the rational number to
// that integer; a constructor with two integer parameters which
// are the numerator and denominator of the rational number.
// The class also contains a function to print a rational number
// in the format m/n and overloads the relational operators
// ==, >, <, >=, <=, !=
//
// *****************************************************************
#include
#include
using namespace std;
class Rational
{
public:
Rational();
// Initializes the rational number to 0
Rational (int wholeNumber);
// Initializes the rational number to wholeNumber/1
Rational (int m, int n);
// Initializes the rational number to m/n if n is not 0;
// the sign of the rational is stored in the numerator
// (the denominator is always positive);
// exits if n = 0 (invalid rational number)
void output();
// Precondition: The rational number is defined.
// Postcondition: The rational number has been displayed on
// the screen in the form m/n.
// I added to do some simple factoring
void factorRational();
// Precondition: The rational number is defined.
// Postcondition: The rational number is identical or may have been factored.
friend bool operator ==(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 equals r2; false otherwise.
friend bool operator <(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than r2; false otherwise.
friend bool operator >(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than r2; false otherwise.
friend bool operator <=(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than or equal to r2; false otherwise.
friend bool operator >=(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than or equal to r2; false otherwise.
friend bool operator !=(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is not equal to r2; false otherwise.
private:
int num; // the numerator of the number
int denom; // the denominator of the number
void standardize();
// Precondition: num and denom have values
// Postcondition: if denom is 0 the program is terminated;
// otherwise the rational number is standardized so that
// denom is positive.
};
// ========================
// main function
// ========================
int main ()
{
//
// Variable declarations
//
char doAgain; // for the loop control
int m, n; // numerator and denominator - m/n
cout << endl << "Testing relational operators..." << endl << endl;
do {
//
// Read and construct two rational numbers
//
cout << "Enter two rational numbers (numerator and denominator "
<< "separated by a blank)." << endl << endl;
cout << "Numerator and denominator of the first rational number: ";
cin >> m >> n;
cout << endl;
Rational r1(m, n);
// I added to factor and show it:
cout << "Your input of " << m << "/" << n << " is now factored to ";
r1.factorRational();
cout << endl;
cout << "Numerator and denominator of the second rational number: ";
cin >> m >> n;
cout << endl;
Rational r2(m, n);
// I added to factor and show it:
cout << "Your input of " << m << "/" << n << " is now factored to ";
r2.factorRational();
cout << endl;
//
// Test all comparisons
//
r1.output();
if (r1 == r2)
cout << " is equal to ";
else
cout << " is not equal to ";
r2.output();
cout << endl;
r1.output();
if (r1 < r2)
cout << " is less than ";
else
cout << " is not less than ";
r2.output();
cout << endl;
r1.output();
if (r1 > r2)
cout << " is greater than ";
else
cout << " is not greater than ";
r2.output();
cout << endl;
r1.output();
if (r1 <= r2)
cout << " is less than or equal to ";
else
cout << " is not less than or equal to ";
r2.output();
cout << endl;
r1.output();
if (r1 >= r2)
cout << " is greater than or equal to ";
else
cout << " is not greater than or equal to ";
r2.output();
cout << endl;
r1.output();
if (r1 != r2)
cout << " is not equal to ";
else
cout << " is equal to ";
r2.output();
cout << endl;
//
// See if the user wants to enter another number
//
cout << endl << "Enter another number? (y or n): ";
cin >> doAgain;
cout << endl;
}
while (doAgain == 'y' || doAgain == 'Y');
return 0;
}
// ===========================
// Function Definitions
// ===========================
Rational::Rational()
{
num = 0;
denom = 1;
}
Rational::Rational (int wholeNumber)
{
num = wholeNumber;
denom = 1;
}
Rational::Rational (int m, int n)
{
num = m;
denom = n;
standardize();
}
void Rational::standardize()
{
if (denom < 0)
{
denom = -denom;
num = -num;
}
else if (denom == 0)
{
cout << "Zero cannot be in the denominator - program aborting!!"
<< endl;
exit(1);
}
}
void Rational::output()
{
cout << num << "/" << denom;
}
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
bool operator ==(const Rational& r1, const Rational& r2) {
// Two rational numbers a/b and c/d are equal if a*d equals c*b.
return ( (r1.num * r2.denom) == (r1.denom * r2.num) ) ? true : false;
}
bool operator <(const Rational& r1, const Rational& r2) {
// If b and d are positive rational numbers then a/b is less than c/d provided a*d is less than c*b.
return ( (r1.num * r2.denom) < (r1.denom * r2.num) ) ? true : false;
}
bool operator >(const Rational& r1, const Rational& r2){
// If b and d are positive rational numbers then a/b is less than c/d provided a*d is less than c*b.
return ( (r1.num * r2.denom) > (r1.denom * r2.num) ) ? true : false;
}
bool operator <=(const Rational& r1, const Rational& r2){
// If b and d are positive rational numbers then a/b is less than c/d provided a*d is less than c*b.
return ( (r1.num * r2.denom) <= (r1.denom * r2.num) ) ? true : false;
}
bool operator >=(const Rational& r1, const Rational& r2) {
// If b and d are positive rational numbers then a/b is less than c/d provided a*d is less than c*b.
return ( (r1.num * r2.denom) >= (r1.denom * r2.num) ) ? true : false;
}
bool operator !=(const Rational& r1, const Rational& r2) {
// If b and d are positive rational numbers then a/b is less than c/d provided a*d is less than c*b.
return ( (r1.num * r2.denom) != (r1.denom * r2.num) ) ? true : false;
}
void Rational::factorRational() {
const int prime[4] = {2,3,5,7}; // setup four lowest primes to try factoring
bool notFactored = true;
// Curious to see count of loops.
int count = 0;
do
{
for (int i = 0; i < 4; i++) {
count++; // track number of times this loops
if ((this->num % prime[i] == 0) && (this->denom % prime[i] == 0)) {
// if both numerator and denominator are evenly divisible by 2,3,5, or 7
// then factor the fraction
this->num /= prime[i];
this->denom /= prime[i];
break; // exit for loop so do/while executes again
} else if (i == 3) {
// if numerator and denominator are not evenly divisible by 2,3,5, or 7
// then set condition to false so do/while stops
notFactored = false;
} else {
// Default is not factored.
notFactored = true;
} // end of if
} // end for loop
} while (notFactored);
cout << this->num << "/" << this->denom << endl;
cout << "factorRational count = " << count << endl;
}
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
/*
Testing relational operators...
Enter two rational numbers (numerator and denominator separated by a blank).
Numerator and denominator of the first rational number: 10 5
Your input of 10/5 is now factored to 2/1
factorRational count = 7
Numerator and denominator of the second rational number: 5 10
Your input of 5/10 is now factored to 1/2
factorRational count = 7
2/1 is not equal to 1/2
2/1 is not less than 1/2
2/1 is greater than 1/2
2/1 is not less than or equal to 1/2
2/1 is greater than or equal to 1/2
2/1 is not equal to 1/2
Enter another number? (y or n): y
Enter two rational numbers (numerator and denominator separated by a blank).
Numerator and denominator of the first rational number: 10 100
Your input of 10/100 is now factored to 1/10
factorRational count = 8
Numerator and denominator of the second rational number: 10 100
Your input of 10/100 is now factored to 1/10
factorRational count = 8
1/10 is equal to 1/10
1/10 is not less than 1/10
1/10 is not greater than 1/10
1/10 is less than or equal to 1/10
1/10 is greater than or equal to 1/10
1/10 is equal to 1/10
Enter another number? (y or n): n
Press any key to continue . . .
*/