Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment Do the following: a. Add the following operators to the class: operat

ID: 3626020 • Letter: A

Question

Assignment
Do the following: a. Add the following operators to the class:

operator-()
operator*()                                                                                                                                     operator/()
b. Make sure Rats are reduced to lowest terms. So if a Rat is 2/4 it should be reduced to 12.
c. If a Rat represents an “improper fraction” (i.e. numerator >denominator) print the Rat as a “mixed number.” So 6/4 will be printed as 1 12.


// Rat class i.e. a class for rational numbers
#include <iostream> using namespace std;
class Rat{

private:
int n;
int d;

public:
// constructors
// default constructor

Rat(){
n=0;

d=1;

}

// 2 parameter constructor Rat(int i, int j){
n=i;

d=j;

}

// conversion constructor
Rat(int i){

n=i;
d=1;

}

//accessor functions (usually called get() and set(...) )

int getN(){ return n;}

int getD(){ return d;}
void setN(int i){ n=i;}

void setD(int i){ d=i;}

//arithmetic operators
Rat operator+(Rat r){

Rat t;

t.n=n*r.d+d*r.n;

t.d=d*r.d;

return t;

}

// 2 overloaded i/o operators

friend ostream& operator<<(ostream& os, Rat r);
friend istream& operator>>(istream& is, Rat& r);

}; //end Rat

// operator<<() is NOT a member function but since it was declared a friend of Rat // it has access to its private parts.

ostream& operator<<(ostream& os, Rat r){

os<<r.n<<" / "<<r.d<<endl;

return os;}

// operator>>() is NOT a member function but since it was declared a friend of Rat // it has access to its provate parts.
istream& operator>>(istream& is, Rat& r){

is>>r.n>>r.d;
return is;

}

int main(){ Rat x(1,2), y(2,3), z; z=x+y; cout<<z;
}
x.setN(3); y.setD(2); z=x+y; cout<<z;
cin>>x; cout<<x;
z= x+5; cout<<z; system("pause"); return 0;

}

Explanation / Answer

// ENJOY CHEERS :)

#include <iostream>
using namespace std;

int gcd(int a,int b)
{
    int c;
    while(1)
    {
    c = a%b;
    if(c==0)
    return b;
    a = b;
    b = c;
    }
}


class Rat{

private:
int n;
int d;

public:
// constructors
// default constructor

Rat(){
n=0;

d=1;

}

Rat(int i, int j){
n=i;

d=j;

}

// conversion constructor
Rat(int i){

n=i;
d=1;

}

//accessor functions (usually called get() and set(...) )

int getN(){ return n;}

int getD(){ return d;}
void setN(int i){ n=i;}

void setD(int i){ d=i;}

//arithmetic operators
Rat operator+(Rat r){

Rat t;

t.n=n*r.d+d*r.n;

t.d=d*r.d;

int temp = gcd(t.n , t.d);
t.n = t.n/temp;
t.d = t.d /temp;
return t;

}

Rat operator-(Rat r){

Rat t;

t.n=n*r.d-d*r.n;

t.d=d*r.d;
int temp = gcd(t.n , t.d);
t.n = t.n/temp;
t.d = t.d /temp;

return t;

}
Rat operator*(Rat r){

Rat t;

t.n=n*r.n;

t.d=d*r.d;
int temp = gcd(t.n , t.d);
t.n = t.n/temp;
t.d = t.d /temp;

return t;

}
Rat operator/(Rat r){

Rat t;

t.n=n*r.d;

t.d=d*r.n;
int temp = gcd(t.n , t.d);
t.n = t.n/temp;
t.d = t.d /temp;

return t;

}
// 2 overloaded i/o operators

friend ostream& operator<<(ostream& os, Rat r);
friend istream& operator>>(istream& is, Rat& r);

}; //end Rat

// operator<<() is NOT a member function but since it was declared a friend of Rat // it has access to its private parts.

ostream& operator<<(ostream& os, Rat r)
{
int temp = gcd(r.n , r.d);
r.n = r.n/temp;
r.d = r.d /temp;
if(r.n>r.d)
{
           int k = r.n/r.d;
           r.n = r.n-k*r.d;
           os<<k <<" "<< r.n<<" / "<<r.d<<endl;   
}
else
{
os<<r.n<<" / "<<r.d<<endl;
}

return os;
}

// operator>>() is NOT a member function but since it was declared a friend of Rat // it has access to its provate parts.
istream& operator>>(istream& is, Rat& r){

is>>r.n>>r.d;
return is;

}



int main()
{
Rat x(1,2), y(2,3), z; z=x+y; cout<<z;

x.setN(3); y.setD(2); z=x+y; cout<<z;
cin>>x; cout<<x;
z= x+5; cout<<z;
system("pause");
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote