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

I need some help with C++ Programming assignment. (Rational Class) Create a clas

ID: 3542302 • Letter: I

Question

I need some help with C++ Programming assignment.


(Rational Class) Create a class called Rational for programming arithmetic with fractions. Write a program to test your class.


Use integer variables to represent the private data of the class -- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction


2

4

would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks:


A) adding two rational numbers. The result should be stored in reduced form.

B) Subtracting two rational numbers. The result should be stored in reduced form.

C) Multiplying two rational numbers. The result should be stored in reduced form.

D) Dividing two rational numbers. The result should be stored in reduced form.

E) Printing rational numbers in the form a/b, where a is the numerator and b is the demoninator.

F) Printing Rational numbers in floating point format.


Important: I need this to run in dev c++. I have already looked at the existing answers to this question on Chegg and there aren't any good programs that actually run.


Important: I NEED good notes on this. Please consider this.

Explanation / Answer

please rate - thanks


it should be self explanatory-any questions ask, I will answer, even though it's already rated




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

private:
        int numerator;
        int denominator;
};
      


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 << " What is the numerator of the first fraction? ";
    if(!(cin >> num)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    do{
    error=false;
    cout << "What is the denominator of the first fraction? ";
     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 << " What is the numerator of the second fraction? ";
    if(!(cin >> num)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    do{
    error=false;
    cout << "What is the denominator of the second fraction? ";
     if(!(cin >> den)) {
            cout << "Please 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 the sum of the two fractions "
    << "2. Displays the difference "
    << "3. Displays the product "
    << "4. Displays the quotient "
    << "5. Exit "
    << " Selection is ";
    if(!(cin >> choice)) {
            cout << "Please 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 << "Not an input between 1 and 5. ";
                break;    
           }
           }while (!exit);
          
system("pause");

}
void Rational::simplify()     //Euclids algorithm
{ int a,b,t;
a=numerator;
b=denominator;
if(a<0)a=-a;                     //deal with negative numbers
if(b<0)b=-b;
if(b>a)
   {t=b;
    b=a;
    a=t;
    }
    while(b>0)
       {t=a%b;
        a=b;
        b=t;
    }

numerator/=a;
denominator/=a;
}       
Rational::Rational()
{

numerator = 0;
denominator = 0;

}

void Rational::setFrac(int newNum, int newDen)
{
     numerator = newNum;
     denominator = newDen;
    
}

Rational Rational::Add(Rational otherFraction) const
{
         Rational sum;
         sum.numerator = numerator*otherFraction.denominator+otherFraction.numerator*denominator;
         sum.denominator = denominator*otherFraction.denominator;
         return sum;
}

Rational Rational::Sub(Rational otherFraction) const
{
    Rational difference;
    difference.numerator = numerator*otherFraction.denominator-otherFraction.numerator*denominator;
    difference.denominator = denominator*otherFraction.denominator;
       return difference;
}

Rational Rational::Mul(Rational otherFraction) const
{
    Rational product;
    product.numerator = numerator*otherFraction.numerator;
    product.denominator = denominator*otherFraction.denominator;   
    return product;
}

Rational Rational::Div(Rational otherFraction) const
{
    Rational quotient;
    quotient.numerator = numerator*otherFraction.denominator;
    quotient.denominator = denominator*otherFraction.numerator;
    return quotient;
}


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