A rational number is a number that can be represented as the quotient of two int
ID: 3551338 • Letter: A
Question
A rational number is a number that can be represented as the quotient of two integers. For example, 1/3, 5/7, 7/2, and so forth are rational numbers (By 2/1, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) In general a rational number is represented as a/b where a and b are the numerator and the denominator, respectively, and the denominator does not equal to 0. Suppose a/b and c/d are rational numbers. Arithmetic operations on rational numbers are defined by the following rules: a / b + c / d = (ad + bc) / bd a / b - c / d = (ad - bc)/bd a / b x c / d = ac / bd (a / b) / (c / d) = ad / bc, where c / d ? 0 Rational numbers are compared as follows: a/b op c/d if ad op bc, where op is any of the relational operations. For example, a/b < c/d if ad < bc. In this exercise, define a class named as RationalNumber. Suppose x, y, and z are objects of type RationalNumber. The class RationalNumber ? Includes private member variables o numerator and denominator of type int. o str of type char*. This member variable is a C string with the format numerator/denominator and is managed by new and delete. ? Includes a member function reduction to normalize the values stored so that, after any operation (construction, initialization, assignment, any arithmetic operations, etc), the greatest common divisor (gcd) of numerator and denominator is 1. For example, after reduction, 21/14 becomes 3/2, i.e., the values of numerator and denominator are 3 and 2, respectively, and str is a C string A rational number is a number that can be represented as the quotient of two integers. For example, 1/3, 5/7, 7/2, and so forth are rational numbers (By 2/1, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) In general a rational number is represented as a/b where a and b are the numerator and the denominator, respectively, and the denominator does not equal to 0. Suppose a/b and c/d are rational numbers. Arithmetic operations on rational numbers are defined by the following rules: a / b + c / d = (ad + bc) / bd a / b - c / d = (ad - bc)/bd a / b x c / d = ac / bd (a / b) / (c / d) = ad / bc, where c / d ? 0 Rational numbers are compared as follows: a/b op c/d if ad op bc, where op is any of the relational operations. For example, a/b < c/d if ad < bc. In this exercise, define a class named as RationalNumber. Suppose x, y, and z are objects of type RationalNumber. The class RationalNumber ? Includes private member variables o numerator and denominator of type int. o str of type char*. This member variable is a C string with the format numerator/denominator and is managed by new and delete. ? Includes a member function reduction to normalize the values stored so that, after any operation (construction, initialization, assignment, any arithmetic operations, etc), the greatest common divisor (gcd) of numerator and denominator is 1. For example, after reduction, 21/14 becomes 3/2, i.e., the values of numerator and denominator are 3 and 2, respectively, and str is a C stringExplanation / Answer
Indented code can be found at:
RationalNumber class: https://dl.dropboxusercontent.com/u/42883368/chegg/Rational/RationalNumber.cpp
Driver program to test: https://dl.dropboxusercontent.com/u/42883368/chegg/Rational/testRationalNumber.cpp
/****************** Sample Output *************/
/************ testRationalNumber.cpp ******************/
#include <iostream>
#include "RationalNumber.cpp"
using namespace std;
int main(){
RationalNumber rn1(4,6);
RationalNumber rn2(21,14);
RationalNumber rn3;
cout << "Rational Number1 : " << rn1 << endl;
cout << "Rational Number2 : " << rn2 << endl;
cout << rn1 << " + " << rn2 << " = " << (rn1+rn2) << endl;
cout << rn1 << " - " << rn2 << " = " << (rn1-rn2) << endl;
cout << rn1 << " * " << rn2 << " = " << (rn1*rn2) << endl;
cout << rn1 << " / " << rn2 << " = " << (rn1/rn2) << endl;
cout << "Enter rational Number: ";
cin >> rn3;
cout << rn3 << endl;
}
/********************** RationalNumber.cpp *****************************/
// Header files section
#include<iostream>
#include <cstdlib>
using namespace std;
class RationalNumber
{
public:
//Declare two member variables numerator and denominator.
int numerator;
int denominator;
//Declare three constructors RationalNumber(), RationalNumber(int), and RationalNumber(int, int).
RationalNumber(int num, int den);
RationalNumber(int whole_number);
RationalNumber();
// function to overload the >> operator
friend istream& operator >>(istream&, RationalNumber&);
// function to overload the << operator
friend ostream& operator <<(ostream&, const RationalNumber&);
// function to overload the == operator
friend bool operator ==(const RationalNumber&, const RationalNumber&);
// function to overload the < operator
friend bool operator <(const RationalNumber&, const RationalNumber&);
// function to overload the <= operator
friend bool operator <=(const RationalNumber&, const RationalNumber&);
// function to overload the > operator
friend bool operator >(const RationalNumber&, const RationalNumber&);
// function to overload the >= operator
friend bool operator >=(const RationalNumber&, const RationalNumber&);
// function to overload the + operator
friend RationalNumber operator +(const RationalNumber&, const RationalNumber&);
// function to overload the - operator
friend RationalNumber operator -(const RationalNumber&, const RationalNumber&);
// function to overload the * operator
friend RationalNumber operator *(const RationalNumber&, const RationalNumber&);
// function to overload the / operator
friend RationalNumber operator /(const RationalNumber&, const RationalNumber&);
};
// function prototypes
void fraction_normalization(int &num, int &den);
int greatest_common_divisor(int number1, int number2);
// two parameters constructor RationalNumber(int, int)
RationalNumber::RationalNumber(int num, int den)
{
/* call the fraction_normalization function to normalize the fraction */
fraction_normalization(num, den);
numerator = num;
denominator = den;
// verify whether the denominator is zero
if(denominator == 0)
{
cout << "Denominator should not be zero." << endl;
system("pause");
exit(1);
} // end if
} // end of constructor
// one parameter constructor RationalNumber(int)
RationalNumber::RationalNumber(int whole_number)
{
numerator = whole_number;
denominator = 1;
} // end of constructor
// default constructor
RationalNumber::RationalNumber()
{
numerator = 0;
denominator = 1;
} // end of constructor
// operator >>() function definition
istream& operator >>(istream& input, RationalNumber& frctn)
{
// local variable
char ch;
// read the fraction entered by user
input >> frctn.numerator >> ch >> frctn.denominator;
// verify whether the second character is / or not
if(ch != '/')
{
cout << "Wrong input: " << ch << endl;
system("pause");
exit(1);
}
/* call the fraction_normalization function to normalize the fraction */
fraction_normalization(frctn.numerator, frctn.denominator);
return input;
} // end of function
// operator <<() function definition
ostream& operator <<(ostream& output,
const RationalNumber& frctn)
{
// display the simplified fraction to the user
output << frctn.numerator << '/' << frctn.denominator;
return output;
} // end of function
// operator ==() function definition
bool operator ==( const RationalNumber& frctn1, const RationalNumber& frctn2)
{
return frctn1.numerator * frctn2.denominator == frctn2.numerator * frctn1.denominator;
} // end of function
// operator <() function definition
bool operator <(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
return frctn1.numerator * frctn2.denominator < frctn2.numerator * frctn1.denominator;
} // end of function
// operator <=() function definition
bool operator <=(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
return frctn1.numerator * frctn2.denominator <= frctn2.numerator * frctn1.denominator;
} // end of function
// operator >() function definition
bool operator >(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
return frctn1.numerator * frctn2.denominator > frctn2.numerator * frctn1.denominator;
} // end of function
// operator >=() function definition
bool operator >=(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
return frctn1.numerator * frctn2.denominator >= frctn2.numerator * frctn1.denominator;
} // end of function
// operator +() function definition
RationalNumber operator +(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
// local variables
int n; // numerator
int d; // denominator
RationalNumber addition;
// calculate the addition of two fractions
n = frctn1.numerator * frctn2.denominator + frctn1.denominator * frctn2.numerator;
d = frctn1.denominator * frctn2.denominator;
/* call the fraction_normalization function to normalize the fraction */
fraction_normalization(n, d);
addition = RationalNumber(n, d);
return addition;
} // end of function
// operator -() function definition
RationalNumber operator -(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
// local variables
int n; // numerator
int d; // denominator
RationalNumber subtraction;
// calculate the addition of two fractions
n = frctn1.numerator * frctn2.denominator - frctn1.denominator * frctn2.numerator;
d = frctn1.denominator * frctn2.denominator;
/* call the fraction_normalization function to normalize the fraction */
fraction_normalization(n, d);
subtraction = RationalNumber(n, d);
return subtraction;
} // end of function
// operator *() function definition
RationalNumber operator *(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
// local variables
int n; // numerator
int d; // denominator
RationalNumber multiplication;
// calculate the multiplication of two fractions
n = frctn1.numerator * frctn2.numerator;
d= frctn1.denominator * frctn2.denominator;
/* call the fraction_normalization function to normalize the fraction */
fraction_normalization(n, d);
multiplication = RationalNumber(n, d);
return multiplication;
} // end of function
RationalNumber operator /(const RationalNumber& frctn1, const RationalNumber& frctn2)
{
// local variables
int n; // numerator
int d; // denominator
RationalNumber division;
// calculate the division of two fractions
n = frctn1.numerator * frctn2.denominator;
d = frctn1.denominator * frctn2.numerator;
/* call the fraction_normalization function to normalize the fraction */
fraction_normalization(n, d);
division = RationalNumber(n, d);
return division;
} // end of function
// fraction_normalization function definition
void fraction_normalization(int& num, int& den)
{
// local variable
int gcd;
// call greatest_common_divisor
gcd = greatest_common_divisor(num, den);
num = num / gcd;
den = den / gcd;
// change the sign of the fraction if requirs
if (num > 0 && den < 0 || num < 0 && den < 0)
{
num = -num;
den = -den;
} // end if
} // end of normalize function
// greatest_common_divisor function definition
int greatest_common_divisor(int number1, int number2)
{
// local variables
int temp;
int remainder;
// get the absolute values
number1 = abs(number1);
number2 = abs(number2);
// verify whether the number1 is greater than number2
if (number1 > number2)
{
temp = number1;
number1 = number2;
number2 = temp;
} // end if
// find the remainder
remainder = number1 % number2;
// repeat the loop until the remainder not equals to 0
while (remainder != 0)
{
remainder = number1 % number2;
number1 = number2;
number2 = remainder;
} // end while
// return the gcd
return number1;
} // end of greatest_common_divisor function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.