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

Write a class Rational for performing arithmetic with fractions. Use integers to

ID: 3664059 • Letter: W

Question

Write a class Rational for performing arithmetic with fractions. Use integers to represent the (private) variables numerator and denominator. Provide a constructor function that enables an object of this class to be initialized, and which should contain default values in case no input is provided. The constructor should also guarantee that the fraction is stored in re duced form, so that for example) the inputs 2 and 4, for the fraction 4 will be stored with numerator 1 and denominator 2 Also provide public functions for the following operations Addition of two rational numbers, where the result should be stored in reduced form Subtraction of two rational numbers, where the result should be stored in reduced form Multiplication of two rational numbers, where the result should be stored in reduced form Division of two rational numbers, where the result should be stored in reduced form. Printing rational numbers Cusing cout in the form of a b where a is the numerator and b is the denominator Printing rational numbers in floating point format

Explanation / Answer

#include <iostream>
class Rational
{
public:
       Rational(); // default constructor
       void setFrac (int newNum,int newDen);
       Rational Add (Rational x) const;
       Rational Sub (Rational x) const;
       Rational Mul (Rational x) const;
       Rational Div (Rational x) const;
       void simplify();
       void showFrac() const;
       void showDec() const;

private:
        int numer;
        int denom;
};
      


using namespace std;


int main()
{
    bool exit=false;
   
    do
    {
    
    int num;
    int den;
    Rational num1;
    Rational num2;
    Rational result;
    int choice;
    bool error;
    do{
    error=false;
    cout << " umerator of the first fraction is";
    if(!(cin >> num)) {
            cout << "enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    do{
    error=false;
    cout << " denominator of the first fraction is ";
     if(!(cin >> den)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    num1.setFrac(num, den);
    do{
    error=false;
    cout << " numerator of the second fraction is";
    if(!(cin >> num)) {
            cout << "enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    do{
    error=false;
    cout << " denominator of the second fraction? ";
     if(!(cin >> den)) {
            cout << " enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    num2.setFrac(num, den);
    cout << endl;
    do{
    error=false;         
    cout << "Enter a number from 1 to 5 "
    << "1. displays sum of the two fractions "
    << "2. displays the difference of two fractions "
    << "3. displays the product "
    << "4. displays the quotient "
    << "5. Exit "
   
    if(!(cin >> choice)) {
            cout << "enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
        error=true;
       }
    }while(error);
  
    cout << endl;
    exit=false;

    switch (choice)
           {
           case 1:
                cout << endl;
                num1.showFrac();
                cout << " + ";
                num2.showFrac();
                cout << " = ";
                result = num1.Add(num2);
                result.simplify();
                result.showFrac();
                 result.showDec();
                   cout << endl;
                break;
           case 2:
                num1.showFrac();
                cout << " - ";
                num2.showFrac();
                cout << " = ";
                result = num1.Sub(num2);
                result.simplify();
                result.showFrac();
                 result.showDec();
                cout << endl;
                break;
           case 3:
                num1.showFrac();
                cout << " * ";
                num2.showFrac();
                cout << " = ";
                result = num1.Mul(num2);
                result.simplify();
                result.showFrac();
                 result.showDec();
                cout << endl;
                break;
           case 4:
                num1.showFrac();
                cout << " / ";
                num2.showFrac();
                cout << " = ";
                result = num1.Div(num2);
                result.simplify();
                result.showFrac();
                result.showDec();
                cout << endl;
                break;
           case 5:
                exit=true;
                break;     
        default: cout << "please check your number. ";
                break;    
           }
           }while (!exit);
          
system("pause");

}
void Rational::simplify()   
{ int a,b;
a=numer;
b=denom;
while(a!=b)
   {if(a>b)
      a -= b;
    else
      b -= a;
   }
numer/=a;
denom/=a;
}       
Rational::Rational()
{

numer = 0;
denom = 0;

}

void Rational::setFrac(int newNum, int newDen)
{
     numer = newNum;
     denom = newDen;
    
}

Rational Rational::Add(Rational x) const
{
         Rational sum;
         sum.numer = numer*x.denom+x.numer*denom;
         sum.denom = denom*x.denom;
         return sum;
}

Rational Rational::Sub(Rational x) const
{
    Rational difference;
    difference.numer = numer*x.denom-x.numer*denom;
    difference.denom = denom*x.denom;
    return difference;
}

Rational Rational::Mul(Rational x) const
{
    Rational product;
    product.numer = numer*x.numer;
    product.denom = denom*x.denom;   
    return product;
}

Rational Rational::Div(Rational x) const
{
    Rational quotient;
    quotient.numer = numer*x.denom;
    quotient.denom = denom*x.numer;
    return quotient;
}


void Rational::showFrac() const
{     if(denom>numer)
          cout << numer << '/' << denom;
       else
          if(numer%denom!=0)
             cout<<numer/denom<<" "<<numer%denom<<"/"<<denom;
         else
             cout<<numer/denom;
   
}
void Rational::showDec() const
      {cout<<" = "<<numer/(double)denom;
      }

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