Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For

In C++

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class Rational.

Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. Also include a default constructor that initializes an object to 0 (that is, to 0/1).

Overload the input and output operators >> and , >=, +, , *, and /. Also write a test program to test your class.

(Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b. You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/8 would be represented the same as 1/2. You should also write a test program to test your class.)

________

Page 1 and 2 of Helpimage text in transcribed

Page 3 of Help:

image text in transcribed

________

Essentially you are re-purposing the following code and all five tests need to be correct:

/* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include  #include  #include  #include  using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e 0 // member functions int get_numerator() const; // return the numerator of the fraction int get_denominator() const; // return the denominator of the fraction void reduce(); // reduce this fraction to simplest form. For instance, // 2/4 will be reduced to 1/2 Fraction reciprocal() const; // return the reciprocal of this Fraction // friend functions friend Fraction operator +(const Fraction& f1, const Fraction& f2); // return the sum of f1 and f2, // the result is reduced friend Fraction operator -(const Fraction& f1, const Fraction& f2); // return the difference of f1 and f2, // the result is reduced friend Fraction operator *(const Fraction& f1, const Fraction& f2); // return the product of f1 and f2, // the result is reduced friend Fraction operator /(const Fraction& f1, const Fraction& f2); // return the quotient of f1 and f2, // the result is reduced friend Fraction operator -(const Fraction& f); // return the negation of f friend bool operator  (const Fraction& f1, const Fraction& f2); // return true if f1 is greater than f2. // False otherwise friend bool operator = (const Fraction& f1, const Fraction& f2); // return true if f1 is greater or equal to f2. // False otherwise friend bool operator == (const Fraction& f1, const Fraction& f2); // return true if f1 is equal to f2. // False otherwise friend bool operator != (const Fraction& f1, const Fraction& f2); // return true if f1 is not equal to f2. // False otherwise friend istream& operator >> (istream& in, Fraction& f); // input f in the form of a/b, where b cannot be zero. Also, // if b is negative, the Fraction will change b to be positive. // So, again, 1/-3 will be changed to -1/3 friend ostream& operator , =, > and  0) cout  1.4) cout  0) cout  1.4) cout  0) cout  2.9) cout  if(f1 > f2 || !(f2 > f1) || f1 > f3) { cout  operator was wrong. "; result -= 0.5; } // Test == if(f1 == f2 || !(f1 == f3) || f2 == f3) { cout = if(f1 >= f2 || !(f1 >= f3) || !(f2 >= f3)) { cout  0) cout  1.9) cout > f; if(f.get_denominator() != 4 || f.get_numerator() != -3) { cout > choice; if(choice != 'y' && choice != 'Y') { cout  0) cout  0.9) cout   f1+f2 (f1.num * f1.num)/(f1.den * f2.den) f1/f2- f1/(f2.reciprocalO) Always try to use existing implementations. For example Fraction operator +const Fraction& fl, const Fraction& f2) (CSCI 361) Help on Project Three To finish project three, student should implement it step by step. In each step, comment away the testing code and test functions that are related to later tests. For example, when write code for the functions that are tested by testl, you may comment away all code that calls test2, test3, test4, and test5 function. You may also comment away the implementation of test2, test3, test4, and test5 function int den = f1.den * f2.den int num f1.num * f2.den + f1.den * f2.num; Fraction result -Fraction (num, den); result.reduce) return result Step One: Implement all functions that are tested in test1. These functions are three constructors and two get functions. The difficult part in this step is to ensure that the denominator is always positive. Fraction::Fraction(int a, int b) Fraction operator -(const Fraction& f1, const Fraction& f2) num = a; denb if(den , <. pay attention: after you haveand operator all rest can be get by recognize that f1f2 means f2 etc step two: implement unary reduce and reciprocal functions the difficult part for this one is function. also function just make sure denominator will positive. notice negation a friend void fraction:: bool fraction fi const f f1 return f.num i since its always int gcd den num ositive f.num0 if only numerator five: test input output operators. user your should istrean>> (istrean& ln, Fraction& f) return Fraction(fnum), f.den); Step Three: Implement , - , and/ operators. The students need to know the following facts f1+f2 (f1.num*f2.den + f1.den*f2.num ) / ( f1.den*f2.den) f1-f2 f1(-f2) int num, den; char slash; cin num cin >> slash; // read' cin  den; f= Fraction(num, den); return in; read 1 /1 ostreans operator 

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

Step: 3

blur-text-image

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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago

Question

2. Discuss the types of messages that are communicated nonverbally.

Answered: 1 week ago