The interface for the rational class and the prototypes of the four non-member f
ID: 3711928 • Letter: T
Question
The interface for the rational class and the prototypes of the four non-member functions are provided below. Store them in a file named rational.h.
class rational
{
friend istream& operator>>(istream &in, rational &r);
// The user is prompted to enter two integer values for the numerator and denominator of a rational number
// Postcondition: the calling rational object is assigned with values entered by the user
friend ostream& operator<<(ostream &out, rational &r);
// Postcondition: The rational object referenced by r is display as “a / b”, e.g., 1 / 2, -5 /9 (not 5 / -9), 1 / 4 (not 2 / 8, etc.).
public:
rational();
// Postcondition: declared rational number is initialized to 0 / 1.
rational(int aa, int bb);
// Postcondition: declared rational number is initialized to aa / bb.
void set(int aa, int bb);
// Postcondition: the calling rational object is set to aa / bb.
rational operator+(const rational &r2) const;
// Postcondition: returns the sum of the calling rational object and r2
rational operator-(const rational &r2) const;
// Postcondition: returns the difference of subtracting r2 from the calling rational object.
rational operator*(const rational &r2) const;
// Postcondition: returns the product of the calling rational object and r2.
rational operator/(const rational &r2) const;
// Postcondition: returns the quotient of dividing the calling rational object by r2.
int operator>(const rational&r2) const;
// Postcondition: returns 1 if the calling object is greater than r2; 0 if it is equal to r2; -1 is it is less than r2
private:
int GCD() const;
// A helper member function. You must use the Euclidean algorithm. https://en.wikipedia.org/wiki/Euclidean_algorithm
// Postcondition: returns the "greatest common divisor" between the numerator and denominator of the calling rational object
int a; // numerator part of a rational number a / b
int b; // denominator part of a rational number a / b
};
int fillArrayFromDiskFile(rational arr[]);
// Precondition: array r[] is assumed to have enough capacity to store all rational numbers in the disk file
// Postcondition: returns the actual # of rational numbers read from the disk file
void displayArray(rational arr[], int n);
void sort(rational arr[], int n);
void swap(rational &x, rational &y);
You are asked to:
Implement all member and non-member functions defined above and stored the code in the second file and name it rational.cpp.
Write a main() function containing code to test the four non-member and all member function of the rational class. The main() function must be stored in the third file named rationalMain.cpp.
Use the separate-file approach to compile and link three file into a load module (i.e., the executable code) and run the program.
The output of your program should be similar to the following sample display. Note that your main() function must first read at least seven different rational numbers from the disk file into an rational type array. The main() function then call the non-member function sort and other functions to sort and display the contents of the array in either ascending or descending order or both as shown in the following sample display.
Array of rational numbers sorted in ascending order:
C:Windows system32cmd.exe Enter the name of the input disk file (uo to 15 characters): infile.txt Atotal of 7 rational numbers have been read into the from a disk file Before sort, the array contains: After sort, array contains: Press any key to continue .. .Explanation / Answer
Note: I almost completed the code..But got some doubt.After reading the name of the input file.In the function declarations we have to pass the filename to the fillarray() function...or if we read the file contents from the main() function then what is the need of the fillarray() function...Could u plz clarify ...so that i can develop exactly...
________________
rational.h
#ifndef RATIONAL_H
#define RATIONAL_H
class rational
{
friend istream& operator>>(istream &in, rational &r);
// The user is prompted to enter two integer values for the numerator and denominator of a rational number
// Postcondition: the calling rational object is assigned with values entered by the user
friend ostream& operator<<(ostream &out, rational &r);
// Postcondition: The rational object referenced by r is display as “a / b”, e.g., 1 / 2, -5 /9 (not 5 / -9), 1 / 4 (not 2 / 8, etc.).
public:
rational();
// Postcondition: declared rational number is initialized to 0 / 1.
rational(int aa, int bb);
// Postcondition: declared rational number is initialized to aa / bb.
void set(int aa, int bb);
// Postcondition: the calling rational object is set to aa / bb.
rational operator+(const rational &r2) const;
// Postcondition: returns the sum of the calling rational object and r2
rational operator-(const rational &r2) const;
// Postcondition: returns the difference of subtracting r2 from the calling rational object.
rational operator*(const rational &r2) const;
// Postcondition: returns the product of the calling rational object and r2.
rational operator/(const rational &r2) const;
// Postcondition: returns the quotient of dividing the calling rational object by r2.
int operator>(const rational&r2) const;
// Postcondition: returns 1 if the calling object is greater than r2; 0 if it is equal to r2; -1 is it is less than r2
private:
int a;
int b;
int GCD() const;
// A helper member function. You must use the Euclidean algorithm. https://en.wikipedia.org/wiki/Euclidean_algorithm
// Postcondition: returns the "greatest common divisor" between the numerator and denominator of the calling rational object
};
#endif
int fillArray(rational arr[]);
void displayArray(rational arr[], int n);
void sort(rational arr[], int n);
void swap(rational &x, rational &y);
________________
rational.cpp
#include <iostream>
#include <fstream>
using namespace std;
#include "Rational.h"
ostream& operator<<(ostream &out, rational &r) {
int GCD = r.GCD();
out << "(" << r.a/GCD<< "/" << r.b / GCD << ")";
return out;
}
istream& operator>>(istream &in, rational &r) {
cout << "Numerator:";
in >> r.a;
cout << "Denominator:";
in >> r.b;
return in;
}
rational::rational()
{
a=0;
b=1;
}
// Postcondition: declared rational number is initialized to 0 / 1.
rational::rational(int aa, int bb)
{
a=aa;
b=bb;
}
// Postcondition: declared rational number is initialized to aa / bb.
void rational::set(int aa, int bb)
{
a=aa;
b=bb;
}
// Postcondition: the calling rational object is set to aa / bb.
rational rational::operator+(const rational &r2) const
{
int aa=this -> b * r2.a + this -> a * r2.b;
int bb=(this -> b * r2.b);
rational result(aa,bb);
int GCD = result.GCD();
result.set(aa / GCD,bb / GCD);
return result;
}
// Postcondition: returns the sum of the calling rational object and r2
rational rational::operator-(const rational &r2) const
{
int aa=this -> a * r2.b - r2.a * this -> b;
int bb=(this -> b * r2.b);
rational result(aa,bb);
int GCD = result.GCD();
result.set(aa / GCD,bb / GCD);
return result;
}
// Postcondition: returns the difference of subtracting r2 from the calling rational object.
rational rational::operator*(const rational &r2) const
{
int aa=this -> a * r2.a;
int bb=(this -> b * r2.b);
rational result(aa,bb);
int GCD = result.GCD();
result.set(aa / GCD,bb / GCD);
return result;
}
// Postcondition: returns the product of the calling rational object and r2.
rational rational::operator/(const rational &r2) const
{
int a = this->a;
int b = this->b;
int c = r2.a;
int d = r2.b;
int aa = (a * d);
int bb = (c * b);
rational result(aa,bb);
int GCD = result.GCD();
result.set(aa / GCD,bb / GCD);
return result;
}
// Postcondition: returns the quotient of dividing the calling rational object by r2.
int rational::operator>(const rational&r2) const
{
if(((double)this->a)/this->b>((double)r2.a/r2.b))
return 1;
else
return 0;
}
int rational::GCD() const
{
int divi = (a > b ? a : b);
int div = (a < b ? a : b);
int rem = divi % div;
while (rem != 0)
{
divi = div;
div = rem;
rem = divi % div;
}
return div;
}
int fillArray(rational arr[])
{
//defines an input stream for the data file
ifstream dataIn;
int a,b,i=0;
while(dataIn>>a>>b)
{
cin>>a;
cin>>b;
rational r(a,b);
arr[i]=r;
i++;
}
return i;
}
void displayArray(rational arr[], int n)
{
for(int i=0;i<7;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void sort(rational arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[j]>arr[i])
swap(arr[i],arr[j]);
}
}
}
void swap(rational &x, rational &y)
{
rational temp;
temp = x;
x = y;
y = temp;
}
__________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.