%3Cp%3ERational%20fractions%26nbsp%3Bare%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3Bform%
ID: 3550995 • Letter: #
Question
%3Cp%3ERational%20fractions%26nbsp%3Bare%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3Bform%26nbsp%3Ba%26nbsp%3B%2F%26nbsp%3Bb%26nbsp%3B%2C%26nbsp%3Bwhere%26nbsp%3Ba%26nbsp%3Band%26nbsp%3Bb%26nbsp%3Bare%26nbsp%3Bintegers%26nbsp%3Band%26nbsp%3Bb%26nbsp%3B%3F%26nbsp%3B0.%26nbsp%3BSuppose%26nbsp%3Ba%26nbsp%3B%2F%26nbsp%3Bb%26nbsp%3Band%26nbsp%3Bc%26nbsp%3B%2F%26nbsp%3Bd%26nbsp%3Bare%26nbsp%3Bfractions.%26nbsp%3BArithmetic%26nbsp%3Boperations%26nbsp%3Bon%26nbsp%3Bfractions%26nbsp%3Bare%26nbsp%3Bdefined%26nbsp%3Bby%26nbsp%3Bthe%26nbsp%3Bfollowing%26nbsp%3Brules%3A%26nbsp%3Ba%26nbsp%3B%2F%26nbsp%3Bb%26nbsp%3B%2B%26nbsp%3Bc%26nbsp%3B%2F%26nbsp%3Bd%26nbsp%3B%3D%26nbsp%3B(ad%26nbsp%3B%2B%26nbsp%3Bbc)%26nbsp%3B%2F%26nbsp%3Bbd%26nbsp%3Ba%26nbsp%3B%2F%26nbsp%3Bb%26nbsp%3B-%26nbsp%3Bc%26nbsp%3B%2F%26nbsp%3Bd%26nbsp%3B%3D%26nbsp%3B(ad%26nbsp%3B-%26nbsp%3Bbc)%26nbsp%3B%2F%26nbsp%3Bbd%26nbsp%3Ba%26nbsp%3B%2F%26nbsp%3Bb%26nbsp%3B*%26nbsp%3Bc%26nbsp%3B%2F%26nbsp%3Bd%26nbsp%3B%3D%26nbsp%3Bac%26nbsp%3B%2F%26nbsp%3Bbd%26nbsp%3B(a%26nbsp%3B%2F%26nbsp%3Bb)%26nbsp%3B%2F%26nbsp%3B(c%26nbsp%3B%2F%26nbsp%3Bd)%26nbsp%3B%3D%26nbsp%3Bad%26nbsp%3B%2F%26nbsp%3Bbc%2C%26nbsp%3Bwhere%26nbsp%3Bc%2Fd%26nbsp%3B%3F%26nbsp%3B0.%26nbsp%3BFractions%26nbsp%3Bare%26nbsp%3Bcompared%26nbsp%3Bas%26nbsp%3Bfollows%3A%26nbsp%3Ba%26nbsp%3B%2F%26nbsp%3Bb%26nbsp%3Bop%26nbsp%3Bc%2Fd%26nbsp%3Bif%26nbsp%3Bad%26nbsp%3Bop%26nbsp%3Bbc%2C%26nbsp%3Bwhere%26nbsp%3Bop%26nbsp%3Bis%26nbsp%3Bany%26nbsp%3Brelational%26nbsp%3Boperator.%26nbsp%3BFor%26nbsp%3Bexample%2C%26nbsp%3Ba%2Fb%26nbsp%3B%26lt%3B%26nbsp%3Bc%2Fd%26nbsp%3Bif%26nbsp%3Bad%26nbsp%3B%26lt%3B%26nbsp%3Bbc.%26nbsp%3B1.%26nbsp%3BCreate%26nbsp%3Ba%26nbsp%3Bclass%2C%26nbsp%3Bcalled%26nbsp%3BRational%2C%26nbsp%3Bfor%26nbsp%3Bperforming%26nbsp%3Barithmetic%26nbsp%3Band%26nbsp%3Brelational%26nbsp%3Boperations%26nbsp%3Bon%26nbsp%3Bfractions.%26nbsp%3BUse%26nbsp%3Binteger%26nbsp%3Bvariables%26nbsp%3Bto%26nbsp%3Brepresent%26nbsp%3Bthe%26nbsp%3Bprivate%26nbsp%3Binstance%26nbsp%3Bvariables%26nbsp%3Bof%26nbsp%3Bthe%26nbsp%3Bclass%26nbsp%3B%E2%80%93%26nbsp%3Bthe%26nbsp%3Bnumerator%26nbsp%3Band%26nbsp%3Bthe%26nbsp%3Bdenominator.%26nbsp%3BProvide%26nbsp%3Ba%26nbsp%3Bconstructor%26nbsp%3Bmethod%26nbsp%3Bthat%26nbsp%3Ballows%26nbsp%3Ban%26nbsp%3Bobject%26nbsp%3Bof%26nbsp%3Bthis%26nbsp%3Bclass%26nbsp%3Bto%26nbsp%3Bbe%26nbsp%3Binitialized%26nbsp%3Bwhen%26nbsp%3Bit%26nbsp%3Bis%26nbsp%3Bdeclared.%26nbsp%3BThe%26nbsp%3Bconstructor%26nbsp%3Bshould%26nbsp%3Bstore%26nbsp%3Bthe%26nbsp%3Bfraction%26nbsp%3Bin%26nbsp%3Breduced%26nbsp%3Bform%26nbsp%3B(i.e.%26nbsp%3Bthe%26nbsp%3Bfraction%26nbsp%3B2%2F4%26nbsp%3Bwould%26nbsp%3Bbe%26nbsp%3Bstored%26nbsp%3Bas%26nbsp%3B1%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bnumerator%26nbsp%3Band%26nbsp%3B2%26nbsp%3Bin%26nbsp%3Bthe%26nbsp%3Bdenominator).%26nbsp%3BProvide%26nbsp%3Ba%26nbsp%3Bno-argument%26nbsp%3Bconstructor%26nbsp%3Bin%26nbsp%3Bcase%26nbsp%3Bno%26nbsp%3Binitializers%26nbsp%3Bare%26nbsp%3Bprovided.%26nbsp%3Ba)%26nbsp%3BOverload%26nbsp%3Bthe%26nbsp%3Barithmetic%26nbsp%3Boperators%26nbsp%3B(%26nbsp%3B%2B%2C%26nbsp%3B-%2C%26nbsp%3B*%2C%26nbsp%3B%2F%26nbsp%3B)%26nbsp%3B%2C%26nbsp%3Bso%26nbsp%3Bthat%26nbsp%3Bthe%26nbsp%3Bappropriate%26nbsp%3Bsymbol%26nbsp%3Bcan%26nbsp%3Bbe%26nbsp%3Bused%26nbsp%3Bto%26nbsp%3Bperform%26nbsp%3Bthese%26nbsp%3Boperations.%26nbsp%3BThe%26nbsp%3Bresult%26nbsp%3Bof%26nbsp%3Beach%26nbsp%3Boperation%26nbsp%3Bshould%26nbsp%3Bbe%26nbsp%3Bin%26nbsp%3Breduced%26nbsp%3Bform.%26nbsp%3Bb)%26nbsp%3BOverload%26nbsp%3Bthe%26nbsp%3Brelational%26nbsp%3Boperators%26nbsp%3B(%26nbsp%3B%26lt%3B%2C%26gt%3B%2C%26nbsp%3B%26gt%3B%3D%2C%26nbsp%3B%3D%3D%2C%26nbsp%3B!%3D%26nbsp%3B)%26nbsp%3B%2C%26nbsp%3Bso%26nbsp%3Bthat%26nbsp%3Bthe%26nbsp%3Bappropriate%26nbsp%3Bsymbol%26nbsp%3Bcan%26nbsp%3Bbe%26nbsp%3Bused%26nbsp%3Bto%26nbsp%3Bperform%26nbsp%3Bthese%26nbsp%3Boperations.%26nbsp%3Bc)%26nbsp%3BOverload%26nbsp%3Bthe%26nbsp%3Bstream%26nbsp%3Binsertion%26nbsp%3Band%26nbsp%3Bstream%26nbsp%3Bextraction%26nbsp%3Boperators%26nbsp%3Bfor%26nbsp%3Binput%26nbsp%3Band%26nbsp%3Boutput.%26nbsp%3B2.%26nbsp%3BWrite%26nbsp%3Ba%26nbsp%3BC%2B%2B%26nbsp%3Bprogram%26nbsp%3Bthat%2C%26nbsp%3Busing%26nbsp%3Bthe%26nbsp%3BRational%26nbsp%3Bclass%2C%26nbsp%3Bperforms%26nbsp%3Boperations%26nbsp%3Bon%26nbsp%3Bfractions.%26nbsp%3BTest%26nbsp%3Byour%26nbsp%3Bclass%26nbsp%3Bthoroughly.%26nbsp%3B%3C!--%2C--%3E%3C%2Fp%3EExplanation / Answer
#include <iostream>
#include<math.h>
using namespace std;
class fractionType
{friend ostream &operator <<(ostream &, const fractionType &);
friend istream &operator >>(istream &,fractionType &);
public:
fractionType operator+(fractionType x);
fractionType operator-(fractionType x);
fractionType operator*(fractionType x);
fractionType operator/(fractionType x);
int fractionType:: gcd();
void fractionType::reduce();
private:
int num,den;
};
ostream &operator <<(ostream &strm, const fractionType &f)
{
if(abs(f.den)<abs(f.num))
cout<<f.num/f.den<<" "<<abs(f.num)%abs(f.den)<<"/"<<abs(f.den);
else
cout<<f.num<<"/"<<f.den;
}
istream &operator >>(istream &strm,fractionType &f)
{char i,slash,trash;
do{
strm>>f.num>>slash>>f.den;
if(f.den==0)
cout<<"Illegal denominator - retry ";
}while(f.den==0);
trash=getchar();
f.reduce();
}
fractionType fractionType::operator + (fractionType x)
{fractionType temp;
temp.num=num*x.den+x.num*den;
temp.den=x.den*den;
reduce();
return(temp);}
fractionType fractionType::operator - (fractionType x)
{fractionType temp;
temp.num=num*x.den-x.num*den;
temp.den=x.den*den;
reduce();
return(temp);}
fractionType fractionType::operator *(fractionType x)
{fractionType temp;
temp.num=num*x.num;
temp.den=den*x.den;
reduce();
return(temp);}
fractionType fractionType::operator / (fractionType x)
{fractionType temp;
temp.num=num*x.den;
temp.den=den*x.num;
reduce();
return(temp);}
void fractionType::reduce()
{int fact;
fact=gcd();
num/=fact;
den/=fact;
if(((num>0)&&(den<0))||((num<0)&&(den<0)))
{num*=-1;
den*=-1;
}
return;
}
int fractionType::gcd()
{int a=num;
int b=den;
if(a<0)
a=-a;
if(b<0)
b=-b;
while (a != b)
{
if (a > b)
a -= b;
else
b -= a;
}
return a;
}
int main()
{char choice,trash;
fractionType fract_1, fract_2;
cout<<"Enter a fraction in the form "numerator/denominator": ";
cin>>fract_1;
cout<<"The entered fraction, reduced is ";
cout<<fract_1<<endl;
cout<<"Enter a fraction in the form "numerator/denominator": ";
cin>>fract_2;
cout<<"The entered fraction, reduced is ";
cout<<fract_2<<endl;
fractionType fract_3=fract_1+fract_2;
cout<<fract_1<<" + "<<fract_2<<" = "<<fract_3<<endl;
fract_3=fract_1-fract_2;
cout<<fract_1<<" - "<<fract_2<<" = "<<fract_3<<endl;
fract_3=fract_1*fract_2;
cout<<fract_1<<" * "<<fract_2<<" = "<<fract_3<<endl;
fract_3=fract_1/fract_2;
cout<<fract_1<<" / "<<fract_2<<" = "<<fract_3<<endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.