Microsoft Word - EXTRA CREDITS ASSIGNMENT3.docx Extracredits-3 -BCS230,M.Alrajab 1 EXTRACREDITSASSIGNMENT Problem:...

1 answer below »
All knowledge should relate to the text C++ early objects ninth edition written by Gaddis(Chapter 1- 16).


Microsoft Word - EXTRA CREDITS ASSIGNMENT3.docx Extracredits-3-BCS230,M.Alrajab 1 EXTRACREDITSASSIGNMENT Problem: Arationalnumberisaquotientoftwointegerssuchas4/7and5/9.Weconsidera numbertobeinareducedformiftherationalnumberdenominatorandnumeratorhave nogcdgreaterthan1.Forexamplethereducedformfor4/6is2/3. Thefollowingcodeprovidesaskeletoncodeimplementationtothisproblem.The RationalclasshasaconstructorRational(int,int)thattakestwointegersandstorestwo valuesinreducedformincorrespondingprivatenumbers.Theclasshasanoverloaded insertionoperator< 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="" submission="" •="" test="" your="" code="" thoroughly="" before="" submission.="" •="" submit="" the="" source="" code="" only="" (not="" project="" submission).="" •="" write="" comments.="" •="" should="" be="" completed="" before="" dec="" 10th="" 2017="" at="" 11:59="" pm.="" •="" no="" late="" submission="" is="" accepted.="" •="" check="" rubric="" extra="" credits="" -3="" -="" bcs230,="" m.="" alrajab="" 2="" 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; } rational(6,="" -12);="" return="" 0;="">
Answered Same DayMay 07, 2020BCS230

Answer To: Microsoft Word - EXTRA CREDITS ASSIGNMENT3.docx Extracredits-3 -BCS230,M.Alrajab 1...

Tanya answered on May 08 2020
153 Votes
Assignment
Operator Overloading with Rational Numbers
//Rational Arithmetic I
#include
#include
usi
ng 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();
}
void reduce();
void operator+(Rational);
void operator-(Rational);
void operator*(Rational);
void operator/(Rational);
};
//************************************************************
// 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 ++;
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here