Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem: A rational number is a quotient of two integers such as 4/7 and 5/9. We consider a number to be in a reduced form

Problem: A rational number is a quotient of two integers such as 4/7 and 5/9. We consider a number to be in a reduced form if the rational number denominator and numerator have no gcd greater than 1. For example the reduced form for 4/6 is 2/3. The following code provides a skeleton code implementation to this problem. The Rational class has a constructor Rational(int, int) that takes two integers and stores two values in reduced form in corresponding private numbers. The class has an overloaded insertion operator << that is used for output of objects of the class.

Your need to: - Read and understand the skeleton code. - Modify the class Rational to add overloaded operators +, -, *, / to be used for addition, subtraction, multiplication and division. - For simplicity, assume the numbers and the arithmetic operators are separated by whitespaces such as 2 / 5 1 / 7

In C++

//Rational Arithmetic I #include #include using namespace std; class Rational { // Declaration of overloaded stream insertion operator friend ostream & operator << (ostream &, Rational r); private: int numerator, denominator; public: // Constructor builds a rational number n/d Rational(int n, int d):numerator(n), denominator(d) { reduce(); } private: // This member function transforms a rational number into // reduced form where the numerator and denominator have 1 // as greatest common factor void reduce(); }; //************************************************************ // This member function transforms a rational number into * // reduced form where the numerator and denominator have 1 * // as greatest common factor. * //************************************************************ void Rational::reduce() { bool negative = (numerator < 0) != (denominator < 0); numerator = abs(numerator); denominator = abs(denominator); int factor = 2; while (factor <= min(numerator, denominator)) { if (numerator % factor == 0 && denominator % factor == 0) { numerator = numerator / factor; denominator = denominator / factor; continue; } factor ++; } if (negative) numerator = - numerator; } //************************************************ // Overloaded stream insertion operator * //************************************************ ostream & operator << (ostream &out, Rational r) { out << r.numerator << "/" << r.denominator; return out; } int main() { cout << Rational(6, -12); return 0; }

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

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

What are some of the classic signs of an unfocused operation?

Answered: 1 week ago

Question

3. What are potential solutions?

Answered: 1 week ago