/*
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).
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.) Provide a function to output a
rational number in the form m/n, where m and n are the numerator and denominator, respectively.
Overload the + and * operators so that they correctly apply to type Rational.
Also write a test program to test your class.
*/
// *****************************************************************
//
// 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 output a rational number
// in the format m/n and overloaded arithmetic operators + and *.
//
// *****************************************************************
#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)
// 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.
void output();
// Precondition: The rational number is defined.
// Postcondition: The rational number has been displayed on
// the screen in the form m/n.
friend Rational operator +(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns the sum of r1 and r2
friend Rational operator *(const Rational& r1, const Rational& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns the product of r1 and r2
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
Rational sum, product;
cout << endl << "Testing sum and product..." << 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);
cout << "Numerator and denominator of the second rational number: ";
cin >> m >> n;
cout << endl;
Rational r2(m, n);
//
// Compute the sum and product
//
sum = r1 + r2;
product = r1 * r2;
//
// Print out the results
//
cout << "The sum of ";
r1.output();
cout << " and ";
r2.output();
cout << " is ";
sum.output();
cout << endl;
cout << "The product is ";
product.output();
cout << endl << endl;
// Paul Taylor added to show factoring.
cout << "Factored sum is ";
sum.factorRational();
cout << "Factored product is ";
product.factorRational();
//
// See if the user wants to enter another number
//
cout << "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 -----
// --------------------------------
Rational operator +(const Rational& r1, const Rational& r2) {
Rational temp;
temp.num = (r1.num * r2.num) + r2.num;
temp.denom = (r1.denom * r2.denom) + r2.denom;
return temp;
}
Rational operator *(const Rational& r1, const Rational& r2) {
Rational temp;
temp.num = r1.num * r2.num;
temp.denom = r1.denom * r2.denom;
return temp;
}
// I added to do some simple factoring
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 --------
// --------------------------------
/* My Output
Testing sum and product...
Enter two rational numbers (numerator and denominator separated by a blank).
Numerator and denominator of the first rational number: 10 5
Numerator and denominator of the second rational number: 10 5
The sum of 10/5 and 10/5 is 20/10
The product is 100/25
Factored sum is 2/1
factorRational count = 8
Factored product is 4/1
factorRational count = 10
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 -5
Numerator and denominator of the second rational number: 10 -5
The sum of -10/5 and -10/5 is -20/10
The product is 100/25
Factored sum is -2/1
factorRational count = 8
Factored product is 4/1
factorRational count = 10
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: 100 44
Numerator and denominator of the second rational number: 88 -10
The sum of 100/44 and -88/10 is 12/54
The product is -8800/440
Factored sum is 2/9
factorRational count = 7
Factored product is -220/11
factorRational count = 10
Enter another number? (y or n): n
Press any key to continue . . .
*/