Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Write a class for rational numbers. Each object in the class should have

ID: 3784735 • Letter: I

Question

In C++ Write a class for rational numbers. Each object in the class should have two integer values that define the rational number: the numerator and the denominator. For example, the fraction 5/6 would have a denominator of 5 and a numerator of 6. Include a constructor with two arguments that can be used to set the numerator and denominator (forbidding zero in the denominator). Provide default values of zero for the numerator and one for the denominator. Overload the input and output operators. Numbers are to be read and written in the form 1/2, 32/15, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2, 32/-15, and -300/-401 are possible. Include a function to normalize the values stored so that, after normalization, the denominator is positive and as small as possible. For example, after normalization, 4/-8 would be represented the same as -1/2. Overload the usual arithmetic operators to provide addition, subtraction, multiplication, and division of two rational numbers. Overload the usual comparison operations to allow comparison of two rational numbers.

Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. For positive rational numbers, a/b is less than c/d, provided a*d is less than c*b.

Explanation / Answer

C++ code:

#include <bits/stdc++.h>
using namespace std;

int gcd ( int a, int b )
{
int c;
while ( a != 0 ) {
c = a; a = b%a; b = c;
}
return b;
}


class RN
{
public:
    int numrator = 0;
    int denominator = 1;
RN(int n, int d)
{
numrator = n;
denominator = d;
}
void input(int n, int d)
{
numrator = n;
denominator = d;
}
void output()
{
cout << numrator << "/" << denominator;   
}
void normlise()
{
int tmp_n = abs(numrator);
int tmp_d = abs(denominator);
if((numrator < 0 and denominator < 0) or (numrator > 0 and denominator > 0))
{

while(gcd(tmp_n,tmp_d) > 1)
{
int tmp_gcd = gcd(tmp_n,tmp_d) ;
tmp_n = tmp_n/tmp_gcd;
tmp_d = tmp_d/tmp_gcd;
}
numrator = tmp_n;
denominator = tmp_d;
}
else
{
while(gcd(tmp_n,tmp_d) > 1)
{
int tmp_gcd = gcd(tmp_n,tmp_d) ;
tmp_n = tmp_n/tmp_gcd;
tmp_d = tmp_d/tmp_gcd;
}
numrator = -tmp_n;
denominator = tmp_d;
}
cout << numrator << "/" << denominator << endl;   
}
float value()
{
return float(numrator)/float(denominator);
}
void cmp(RN n1, RN n2)
{
if(n1.value() == n2.value())
{
cout << "equal numbers" << endl;
}
else if(n1.value() > n2.value())
{
n1.output();
cout << " is > " ;
n2.output();
cout << endl;   
}
else
{
n2.output();
cout << " is > " ;
n1.output();
cout << endl;   
}
}
void add(RN n1, RN n2)
{
n1.output();
cout << " + ";
n2.output();
int n = (n1.numrator)*(n2.denominator) + (n2.numrator)*(n1.denominator);
int d = (n1.denominator) + (n2.denominator);
cout << " = " << n << "/" << d << endl;
}
void sub(RN n1, RN n2)
{
n1.output();
cout << " - ";
n2.output();
int n = (n1.numrator)*(n2.denominator) - (n2.numrator)*(n1.denominator);
int d = (n1.denominator) + (n2.denominator);
cout << " = " << n << "/" << d << endl;   
}
void mult(RN n1, RN n2)
{
n1.output();
cout << " * ";
n2.output();
int n = (n1.numrator)*(n2.numrator);
int d = (n1.denominator) * (n2.denominator);
cout << " = " << n << "/" << d << endl;
}
void div(RN n1, RN n2)
{
n1.output();
cout << " / ";
n2.output();
int n = (n1.numrator)*(n2.denominator);
int d = (n1.denominator) * (n2.numrator);
cout << " = " << n << "/" << d << endl;   
}

};

int main()
{
RN n1(4,8);
RN n2(1,2);
cout << "n1 is ";
n1.output();
cout << endl;
cout << "n1 is ";
n2.output();
cout << endl;

cout << "Normalised forms " << endl;
n1.normlise();
n2.normlise();

cout << "Compare two numbers " << endl;
n1.cmp(n1,n2);

n1.add(n1,n2);
n1.sub(n1,n2);
n1.mult(n1,n2);
n1.div(n1,n2);   
return 0;
}

Sample Output:

n1 is 4/8
n1 is 1/2
Normalised forms
1/2
1/2
Compare two numbers
equal numbers
1/2 + 1/2 = 4/4
1/2 - 1/2 = 0/4
1/2 * 1/2 = 1/4
1/2 / 1/2 = 2/2