create a four function calculator for fractions using classes.here are the four
ID: 3623789 • Letter: C
Question
create a four function calculator for fractions using classes.here are the four arithmetic operations applied to fractions:addition: a/b + c/d
subtraction:a/b - c/d
multiplication: a/b * c/d
division:a/b / c/d
this program stores the numerator and denominator of two fractions before adding them and may also store the answer which is also a fraction modify the program so that all fractions are stored in variables of type class fraction.whose two members are the fractions and numerators(bothy type of int)all fraction related data should be stored in classes of this type.also use the function of get line.
Explanation / Answer
please rate - thanks
see what you can do with this--you weren't very specific
#include <iostream>
#include<math.h>
using namespace std;
class Fraction
{friend ostream &operator <<(ostream &, const Fraction &);
friend istream &operator >>(istream &,Fraction &);
public:
void Inputc();
void Outputc();
Fraction operator+(Fraction x);
Fraction operator-(Fraction x);
Fraction operator*(Fraction x);
Fraction operator/(Fraction x);
int Fraction:: gcd();
void Fraction::reduce();
private:
int num,den;
};
ostream &operator <<(ostream &strm, const Fraction &f)
{
if(abs(f.den)<abs(f.num))
cout<<f.num/f.den<<" "<<abs(f.num)%abs(f.den)<<"/"<<abs(f.den)<<endl;
else
cout<<f.num<<"/"<<f.den<<endl;
}
istream &operator >>(istream &strm,Fraction &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();
}
Fraction Fraction::operator + (Fraction x)
{Fraction temp;
temp.num=num*x.den+x.num*den;
temp.den=x.den*den;
reduce();
return(temp);}
Fraction Fraction::operator - (Fraction x)
{Fraction temp;
temp.num=num*x.den-x.num*den;
temp.den=x.den*den;
reduce();
return(temp);}
Fraction Fraction::operator *(Fraction x)
{Fraction temp;
temp.num=num*x.num;
temp.den=den*x.den;
reduce();
return(temp);}
Fraction Fraction::operator / (Fraction x)
{Fraction temp;
temp.num=num*x.den;
temp.den=den*x.num;
reduce();
return(temp);}
void Fraction::reduce()
{int fact;
fact=gcd();
num/=fact;
den/=fact;
if(((num>0)&&(den<0))||((num<0)&&(den<0)))
{num*=-1;
den*=-1;
}
return;
}
int Fraction::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;
Fraction fract_1, fract_2;
cout<<"Enter a fraction in the form "numerator/denominator" >>>";
cin>>fract_1;
cout<<"Enter a fraction in the form "numerator/denominator" >>>";
cin>>fract_2;
cout<<"The entered fraction, reduced is ";
cout<<fract_1;
cout<<"The entered fraction, reduced is ";
cout<<fract_2;
Fraction fract_3=fract_1+fract_2;
cout<<"fraction 1 + fraction 2 = ";
cout<<fract_3;
fract_3=fract_1-fract_2;
cout<<"fraction 1 - fraction 2 = ";
cout<<fract_3;
fract_3=fract_1*fract_2;
cout<<"fraction 1 * fraction 2 = ";
cout<<fract_3;
fract_3=fract_1/fract_2;
cout<<"fraction 1 / fraction 2 = ";
cout<<fract_3;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.