In: Computer Science
C++
Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class:
a. A default constructor that should use default arguments in
case no initializers are included in the main. The fraction needs
to be stored in reduced form. Make sure the denominator is not set
to 0 or a negative value.
b. Add two fractions and store the sum in reduced form.
c. Subtract two fractions and store the difference in reduced
form.
d. Multiply two fractions and store the product in reduced
form.
e. Divide two fractions and store the quotient in reduced
form.
f. Print a fraction.
g. Change a fraction to its reciprocal. For example, the reciprocal of the fraction 5/8 is 8/5
Your main should instantiate two fractions and call each of the class methods. The two fractions should be printed a long with the sum, difference, product, quotient, and after being changed to its reciprocal. (Please do not overload the operators for this program.)
#include <iostream>
using namespace std;
class Fraction
{
public:
Fraction (int num=1, int den=1);
int gcd(int a, int b);
int get_num( ) const;
int get_den( ) const;
int numerator;
int denominator;
void reduce();
Fraction operator+(Fraction fraction);
Fraction operator/(Fraction fraction);
Fraction operator-(Fraction fraction);
Fraction operator*(Fraction fraction);
};
int Fraction::get_num(void)const {
return this->numerator;
}
int Fraction::get_den(void)const {
return this->denominator;
}
Fraction::Fraction(int n , int d) {
this->numerator = n;
this->denominator = d;
}
Fraction Fraction::operator*(Fraction fraction) {
Fraction res;
res.numerator = (this->numerator * fraction.get_num());
res.denominator = (this->denominator * fraction.get_den());
return res;
}
Fraction Fraction::operator-(Fraction fraction) {
Fraction res;
if (this->denominator == fraction.get_den()) {
res.numerator = (this->numerator - fraction.get_num());
res.denominator = (this->denominator);
} else {
res.numerator = ((this->numerator * fraction.get_den()) - (fraction.get_num() * this->denominator));
res.denominator = (this->denominator * fraction.get_den());
}
return res;
}
Fraction Fraction::operator+(Fraction fraction) {
Fraction res;
if (this->denominator == fraction.get_den()) {
res.numerator = (this->numerator + fraction.get_num());
res.denominator = (this->denominator);
} else {
res.numerator = ((this->numerator * fraction.get_den()) + (fraction.get_num() * this->denominator));
res.denominator = (this->denominator * fraction.get_den());
}
return res;
}
Fraction Fraction::operator/(Fraction fraction) {
Fraction res;
res.denominator = (this->denominator * fraction.get_num());
res.numerator = (this->numerator * fraction.get_den());
return res;
}
int Fraction::gcd(int X, int Y)
{
if (X == 0)
return Y;
return gcd(Y%X, X);
}
void Fraction::reduce(void) {
int g = gcd(this->numerator, this->denominator);
this->numerator /= g;
this->denominator /= g;
}
int main()
{
int n;
int num , den;
do{
cout<<"1. Addition"<<endl;
cout<<"2. Substraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Division"<<endl;
cout<<"5. Reduce a fraction"<<endl;
cout<<"6. Show decimal equivalent"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Enter your choice: ";
cin>>n;
switch(n){
case 1:
{
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fa(num, den);
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fb(num, den);
Fraction f = fa + fb;
f.reduce();
cout<<"Result: "<<f.numerator<<"/"<<f.denominator<<endl<<endl;
}
break;
case 2:
{
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fc(num, den);
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fd(num, den);
Fraction f1 = fc - fd;
f1.reduce();
cout<<"Result: "<<f1.numerator<<"/"<<f1.denominator<<endl<<endl;
}
break;
case 3:
{
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fe(num, den);
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction ff(num, den);
Fraction f3 = fe * ff;
f3.reduce();
cout<<"Result: "<<f3.numerator<<"/"<<f3.denominator<<endl<<endl;
}
break;
case 4:
{
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fg(num, den);
cout<<"Enter 1st numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fh(num, den);
Fraction f4 = fg / fh;
f4.reduce();
cout<<"Result: "<<f4.numerator<<"/"<<f4.denominator<<endl<<endl;
}
break;
case 5:
{
cout<<"Enter numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fh(num, den);
fh.reduce();
cout<<"Result: "<<fh.numerator<<"/"<<fh.denominator<<endl<<endl;
}
break;
case 6:
{
cout<<"Enter numerator and denominator (sepatrated by space)";
cin>>num>>den;
Fraction fi(num, den);
cout<<"Result: "<<fi.numerator*1.0/fi.denominator<<endl<<endl;
}
break;
case 7:
exit(0);
}
}while(num != 7);
return 0;
}
===========================================
SEE OUTPUT
PLEASE COMMENT if there is any concern.
=========================================