Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 .

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

// 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:

image text in transcribedThe contents of the disk file (named infile.txt) containing data (7 rational numbers) to be sorted are shown below. Note that the two values in each row are the values for the numerator and denominator of a rational number.

image text in transcribed

C:\Windows system32\cmd.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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions