//Assignment #2 Solution #include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const int MIN_VALUE = 1;
const int MAX_VALUE = 20;
const int TEST_COUNT = 5;
////////////////////////////////////////////////////////////////////////////////
// This program will be able to simplify, add, subtract, divide, //
// multiply, display, and display as a real number a fraction or fractions //
////////////////////////////////////////////////////////////////////////////////
class CRational
{
private:
int den;
int num;
public:
CRational (int den=1, int num=1); // constructor
CRational (char ch); // constructor
void simplify(void); // member function to simplify
CRational Add(CRational rat); // member function to add
CRational Subtract(CRational rat); // member function to subtract
CRational Multiply(CRational rat); // member function to multiply
CRational Divide(CRational rat); // member function to divide
void display(void); // member function to display
void displayValue(void); // member function to display float
};
int gcd(int x, int y); // global function for gcd num
////////////////////////////////////////////////////////////////////////
// This constructor sets the values and also simplifies the fraction. //
////////////////////////////////////////////////////////////////////////
CRational::CRational (int den, int num)
{
this->den = den;
this->num = num;
simplify();
}
////////////////////////////////////////////////////////////////////////
// This function tests the default constructor the class //
////////////////////////////////////////////////////////////////////////
void TestConstructor(void)
{
cout << "TestConstructor" << endl;
cout << "---------------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
int d = rand()%MAX_VALUE + 1;
int n = rand()%MAX_VALUE + 1; if (rand()%2 == 1)
{
d = d;
n = -n;
}
CRational fraction(d, n);
fraction.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This constructor sets the values randomly //
// and also simplifies the fraction unless called with 'u' //
////////////////////////////////////////////////////////////////////////
CRational::CRational(char ch)
{
if (('r' == ch) || ('R' == ch))
{
num = rand()%MAX_VALUE + 1;
den = rand()%MAX_VALUE + 1;
if (rand()%2 == 1)
num = -num;
}
else if (('u' == ch) || ('U' == ch))
{
num = rand()%MAX_VALUE + 1;
den = rand()%MAX_VALUE + 1;
if (rand()%2 == 1)
num = -num;
}
else if (('i' == ch) || ('I' == ch))
{
cout << "Enter num ";
cin >> num;
cout << "Enter den ";
cin >> den;
}
else
{
num = 1;
den = 1;
} if (ch != 'u' && ch != 'U')
simplify();
}
////////////////////////////////////////////////////////////////////////
// This function test the random constructor //
////////////////////////////////////////////////////////////////////////
void TestConstructor2(void)
{
cout << "\nTestConstructor2" << endl;
cout << "----------------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
char ch;
cout << "Test #" << i+1 << endl;
switch (rand()%4)
{
case 0: ch = 'r'; break;
case 1: ch = 'R'; break;
case 2: ch = 'u'; break;
case 3: ch = 'U'; break;
case 6: ch = 'i'; break;
case 7: ch = 'I'; break;
default:
ch = 'k';
}
cout << ch << endl;
CRational rat(ch);
rat.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This constructor simplifies the fraction by finding the //
// greatest common fractor and dividing both the den and num by it //
////////////////////////////////////////////////////////////////////////
void CRational::simplify(void)
{
if (this->den < 0)
{
this->den = -this->den;
this->num = -this->num;
} if (this->num != 0 && this->den != 0)
{
int gcfactor = gcd(abs(this->num), this->den);
this->num = this->num / gcfactor;
this->den = this->den / gcfactor;
}
}
////////////////////////////////////////////////////////////////////////
// This functions tests the simplify function //
////////////////////////////////////////////////////////////////////////
void TestSimplify(void)
{
cout << "TestSimplify" << endl;
cout << "------------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "\nTest #" << i+1 << endl;
CRational r1('u');
r1.display();
r1.simplify();
r1.display();
}
}
////////////////////////////////////////////////////////////////////////
// This constructor added two fractions by the following method: //
// //
// a x a * y + x * b //
// - + - or ------------- //
// b y b * y //
////////////////////////////////////////////////////////////////////////
CRational CRational::Add(CRational rat)
{
CRational tempAmount;
tempAmount.num = this->num * rat.den + this->den * rat.num;
tempAmount.den = this->den * rat.den;
tempAmount.simplify();
return tempAmount;
}
////////////////////////////////////////////////////////////////////////
// This function tests the add function. //
////////////////////////////////////////////////////////////////////////
void TestAdd(void)
{
cout << "\nTestAdd" << endl;
cout << "-------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
CRational r1('r'), r2('r'), r3('r');
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
r1 = r2.Add(r3);
cout << "After r1 = r2 + r3;\n";
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This constructor added two fractions by the following method: //
// //
// a x a * y - x * b //
// - - - or ------------- //
// b y b * y //
////////////////////////////////////////////////////////////////////////
CRational CRational::Subtract(CRational rat)
{
CRational tempAmount;
tempAmount.num = this->num * rat.den - this->den * rat.num;
tempAmount.den = this->den * rat.den;
tempAmount.simplify();
return tempAmount;
}
////////////////////////////////////////////////////////////////////////
// This function tests the subtract function. //
////////////////////////////////////////////////////////////////////////
void TestSubtract(void)
{
cout << "\nTestSubtract" << endl;
cout << "------------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
CRational r1('r'), r2('r'), r3('r');
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
r1 = r2.Subtract(r3);
cout << "After r1 = r2 - r3;\n";
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This constructor added two fractions by the following method: //
// //
// a x a * x //
// - * - or ----- //
// b y b * y //
////////////////////////////////////////////////////////////////////////
CRational CRational::Multiply(CRational rat)
{
CRational tempAmount;
tempAmount.num = this->num * rat.num;
tempAmount.den = this->den * rat.den;
tempAmount.simplify();
return tempAmount;
}
////////////////////////////////////////////////////////////////////////
// This function tests the multiply function. //
////////////////////////////////////////////////////////////////////////
void TestMultiply(void)
{
cout << "\nTestMultiply" << endl;
cout << "------------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
CRational r1('r'), r2('r'), r3('r');
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
r1 = r2.Multiply(r3);
cout << "After r1 = r2 * r3;\n";
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This constructor added two fractions by the following method: //
// //
// a x a y //
// - / - or - * - //
// b y b x //
////////////////////////////////////////////////////////////////////////
CRational CRational::Divide(CRational rat)
{
CRational tempAmount;
tempAmount.num = this->num * rat.den;
tempAmount.den = this->den * rat.num;
tempAmount.simplify();
return tempAmount;
}
////////////////////////////////////////////////////////////////////////
// This function tests the divide function. //
////////////////////////////////////////////////////////////////////////
void TestDivide(void)
{
cout << "\nTestDivide" << endl;
cout << "----------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
CRational r1('r'), r2('r'), r3('r');
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
r1 = r2.Divide(r3);
cout << "After r1 = r2 / r3;\n";
cout << "r1 = "; r1.display();
cout << "r2 = "; r2.display();
cout << "r3 = "; r3.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This global function that find the greatest common divisor //
// by subtracting the greater number until both numbers are equal. //
////////////////////////////////////////////////////////////////////////
int gcd(int x, int y)
{
while (x != y)
if (y > x)
y = y - x;
else
x = x - y;
return x;
}
////////////////////////////////////////////////////////////////////////
// This function tests the gcd function. //
////////////////////////////////////////////////////////////////////////
void TestGcd(void)
{
cout << "\nTestGcd" << endl;
cout << "-------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
int x = rand()%MAX_VALUE + 1;
int y = rand()%MAX_VALUE + 1;
int greatestcd = gcd(x, y);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "gcd = " << greatestcd << endl;
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This function displays the fraction //
////////////////////////////////////////////////////////////////////////
void CRational::display(void)
{
cout << this->num << " / " << this->den << endl;
}
////////////////////////////////////////////////////////////////////////
// This function tests the display function. //
////////////////////////////////////////////////////////////////////////
void TestDisplay(void)
{
cout << "TestDisplay" << endl;
cout << "-----------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
CRational r1('r');
r1.display();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// This function display the fraction as a real number. //
////////////////////////////////////////////////////////////////////////
void CRational::displayValue(void)
{
double RealValue;
RealValue = double(this->num) / this->den;
cout << RealValue << endl;
}
////////////////////////////////////////////////////////////////////////
// This function tests the displayValue function. //
////////////////////////////////////////////////////////////////////////
void TestDisplayValue(void)
{
cout << "TestDisplayValue" << endl;
cout << "----------------" << endl;
for (int i=0; i<TEST_COUNT; i++)
{
cout << "Test #" << i+1 << endl;
CRational r1('r');
r1.display();
r1.displayValue();
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
// main function //
////////////////////////////////////////////////////////////////////////
void main(void)
{
srand(time(NULL));
TestConstructor();
TestConstructor2();
TestAdd();
TestSubtract();
TestMultiply();
TestDivide();
TestDisplay();
TestDisplayValue();
TestGcd();
//TestSimplify();
} /*
Sample Run: TestConstructor
---------------
Test #1
5 / 3 Test #2
17 / 2 Test #3
16 / 17 Test #4
1 / 1 Test #5
19 / 6
TestConstructor2
----------------
Test #1
u
7 / 7 Test #2
R
-3 / 4 Test #3
U
-18 / 8 Test #4
r
-19 / 3 Test #5
r
-6 / 7
TestAdd
-------
Test #1
r1 = -1 / 6
r2 = 11 / 16
r3 = 1 / 5
After r1 = r2 + r3;
r1 = 71 / 80
r2 = 11 / 16
r3 = 1 / 5 Test #2
r1 = -2 / 9
r2 = 1 / 2
r3 = -11 / 9
After r1 = r2 + r3;
r1 = -13 / 18
r2 = 1 / 2
r3 = -11 / 9 Test #3
r1 = -7 / 10
r2 = -6 / 17
r3 = 16 / 1
After r1 = r2 + r3;
r1 = 266 / 17
r2 = -6 / 17
r3 = 16 / 1 Test #4
r1 = -13 / 5
r2 = -6 / 5
r3 = 1 / 7
After r1 = r2 + r3;
r1 = -37 / 35
r2 = -6 / 5
r3 = 1 / 7 Test #5
r1 = 11 / 1
r2 = 18 / 1
r3 = -5 / 2
After r1 = r2 + r3;
r1 = 31 / 2
r2 = 18 / 1
r3 = -5 / 2
TestSubtract
------------
Test #1
r1 = -7 / 3
r2 = -1 / 1
r3 = -17 / 7
After r1 = r2 - r3;
r1 = 10 / 7
r2 = -1 / 1
r3 = -17 / 7 Test #2
r1 = 18 / 1
r2 = -10 / 3
r3 = -1 / 4
After r1 = r2 - r3;
r1 = -37 / 12
r2 = -10 / 3
r3 = -1 / 4 Test #3
r1 = 9 / 7
r2 = -3 / 1
r3 = 13 / 11
After r1 = r2 - r3;
r1 = -46 / 11
r2 = -3 / 1
r3 = 13 / 11 Test #4
r1 = -15 / 11
r2 = 3 / 13
r3 = 4 / 3
After r1 = r2 - r3;
r1 = -43 / 39
r2 = 3 / 13
r3 = 4 / 3 Test #5
r1 = 3 / 13
r2 = 16 / 9
r3 = 5 / 2
After r1 = r2 - r3;
r1 = -13 / 18
r2 = 16 / 9
r3 = 5 / 2
TestMultiply
------------
Test #1
r1 = -8 / 1
r2 = 3 / 2
r3 = 5 / 4
After r1 = r2 * r3;
r1 = 15 / 8
r2 = 3 / 2
r3 = 5 / 4 Test #2
r1 = 10 / 13
r2 = 4 / 3
r3 = 19 / 4
After r1 = r2 * r3;
r1 = 19 / 3
r2 = 4 / 3
r3 = 19 / 4 Test #3
r1 = -8 / 13
r2 = 20 / 7
r3 = 3 / 4
After r1 = r2 * r3;
r1 = 15 / 7
r2 = 20 / 7
r3 = 3 / 4 Test #4
r1 = 2 / 1
r2 = 13 / 4
r3 = 8 / 9
After r1 = r2 * r3;
r1 = 26 / 9
r2 = 13 / 4
r3 = 8 / 9 Test #5
r1 = -11 / 16
r2 = -4 / 11
r3 = 9 / 20
After r1 = r2 * r3;
r1 = -9 / 55
r2 = -4 / 11
r3 = 9 / 20
TestDivide
----------
Test #1
r1 = 8 / 3
r2 = -1 / 13
r3 = 19 / 17
After r1 = r2 / r3;
r1 = -17 / 247
r2 = -1 / 13
r3 = 19 / 17 Test #2
r1 = 10 / 13
r2 = -1 / 1
r3 = 19 / 10
After r1 = r2 / r3;
r1 = -10 / 19
r2 = -1 / 1
r3 = 19 / 10 Test #3
r1 = 17 / 16
r2 = -4 / 3
r3 = -3 / 4
After r1 = r2 / r3;
r1 = 16 / 9
r2 = -4 / 3
r3 = -3 / 4 Test #4
r1 = -9 / 4
r2 = 5 / 14
r3 = -18 / 7
After r1 = r2 / r3;
r1 = -5 / 36
r2 = 5 / 14
r3 = -18 / 7 Test #5
r1 = -2 / 7
r2 = -19 / 1
r3 = 11 / 14
After r1 = r2 / r3;
r1 = -266 / 11
r2 = -19 / 1
r3 = 11 / 14 TestDisplay
-----------
Test #1
-3 / 10 Test #2
-3 / 4 Test #3
6 / 7 Test #4
-5 / 3 Test #5
-19 / 18 TestDisplayValue
----------------
Test #1
7 / 11
0.636364 Test #2
-18 / 7
-2.57143 Test #3
-3 / 2
-1.5 Test #4
-4 / 3
-1.33333 Test #5
11 / 3
3.66667
TestGcd
-------
Test #1
x = 11
y = 15
gcd = 1 Test #2
x = 8
y = 11
gcd = 1 Test #3
x = 1
y = 16
gcd = 1 Test #4
x = 11
y = 2
gcd = 1 Test #5
x = 9
y = 12
gcd = 3 Press any key to continue
*/ |