Write the function in C ++ to reduce the numerator and denominator in the Rat cl
ID: 3548982 • Letter: W
Question
Write the function in C ++ to reduce the numerator and denominator in the Rat class to lowest terms.
// 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
// 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
void reduce()
{
// first find which one is bigger out of n and d.
int max = (n>d?n:d);
int gcd = 1;
// intialize gcd with 1.
for(int i=max; i>=1; i--)
{
// start checking for big factor that commonly divides n and d;
// once it found break it.
if(n%i==0 && d%i==0) { gcd = i; break; }
}
// dividing both numerator and denominator with gcd.
n = n/gcd;
d = d/gcd;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.