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

I am having trouble keeping up with C++ and was wondering if someone can either

ID: 3879007 • Letter: I

Question

I am having trouble keeping up with C++ and was wondering if someone can either give pointers or help solve the following problem:

Create a Rational Number (fractions) class. Provide the following capabilities:

1) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators.

2) Overload the addition, subtraction, multiplication, and division operators for this class.

3) Overload the relational and equality operators for this class.

4) Provide a function that returns the string representation of the rational number in the form a/b, where a is the numerator and b is the denominator.

5) Provide a function that returns the rational number as a double.

6) Also provide a main program to test your class.

Explanation / Answer

here is your program : ----------------->>>>>>>>>>>>>>>>>>

#include<iostream>

using namespace std;

class Rational
{
   private:
      int numerator;
      int denominator;
   public:
      Rational(){
      numerator = 0;
      denominator = 1;
   }
      Rational(int num){
      numerator = num;
      denominator = 1;
   }
      Rational(int num, int den){
      numerator = num;
      if(den > 0){
        denominator = den;
    }
   }
      const Rational operator+(const Rational &other) const{
      int num = (numerator*other.denominator + denominator*other.numerator );
      int den = (denominator * other.denominator);
     
      Rational temp(num,den);
     
      return temp;
   }
      const Rational operator-(const Rational &other) const{
      int num = (numerator*other.denominator - denominator*other.numerator );
      int den = (denominator * other.denominator);
     
      Rational temp(num,den);
     
      return temp;
    }
      const Rational operator*(const Rational &other) const{
      int num = (numerator*other.numerator );
      int den = (denominator * other.denominator);
     
      Rational temp(num,den);
     
      return temp;
   }
      const Rational operator/(const Rational &other) const{
      int num = (numerator*other.denominator);
      int den = (denominator * other.numerator);
     
      Rational temp(num,den);
     
      return temp;
   }
      void display() const{
      cout<<numerator<<"/"<<denominator;
   }
   string toString(){
   return numerator+"/"+denominator;
   }
   double toDouble(){
   return (double)(numerator/denominator);
   }
  
   void simplify(){
   int m = gcd(numerator,denominator);
   numerator = numerator/m;
   denominator = denominator/m;
   }
  
   bool operator==(Rational &other){
   if(toDouble() == other.toDouble()){
     return true;
    }
   
    return false;
   }
   bool operator>(Rational &other){
   if(toDouble() > other.toDouble()){
     return true;
    }
   
    return false;
   }
   bool operator<(Rational &other){
   if(toDouble() < other.toDouble()){
     return true;
    }
   
    return false;
   }
    private:
       int gcd(int a, int b){
         int m,n;

      m=a;
      n=b;
  
      while(m!=n)
      {
          if(m>n)
              m=m-n;
          else
              n=n-m;
      }
     
      return m;
    }
};

Rational getRational();
void displayResult(const string &op, const Rational &lhs, const Rational&rhs, const Rational &result);

int main() {
   Rational A, B, result;
   int choice;

   cout << "Enter Rational A:" << endl;
   A = getRational();
   cout << endl;

   cout << "Enter Rational B:" << endl;
   B = getRational();
   cout << endl;

   cout << "Enter Operation (1 - 4):" << endl
      << "1 - Addition (A + B)" << endl
      << "2 - Subtraction (A - B)" << endl
      << "3 - Multiplication (A * B)" << endl
      << "4 - Division (A / B)" << endl
      << "5 - Simplify A" << endl;
   cin >> choice;
   cout << endl;

   if (choice == 1) {
      result = A+B;
      displayResult("+", A, B, result);
   } else if (choice == 2) {
      result = A - B;
      displayResult("-", A, B, result);
   } else if (choice == 3) {
      result = A*B;
      displayResult("*", A, B, result);
   } else if (choice == 4) {
      result = A/B;
      displayResult("/", A, B, result);
   } else if (choice == 5) {
      A.simplify();
       A.display();
     }else {
      cout << "Unknown Operation";
   }
   cout << endl;

   return 0;
}

Rational getRational() {
   int choice;
   int numer, denom;

   cout << "Which Rational constructor? (Enter 1, 2, or 3)" << endl
      << "1 - 2 parameters (numerator & denominator)" << endl
      << "2 - 1 parameter (numerator)" << endl
      << "3 - 0 parameters (default)" << endl;
   cin >> choice;
   cout << endl;

   if (choice == 1) {
      cout << "numerator? ";
      cin >> numer;
      cout << endl;
      cout << "denominator? ";
      cin >> denom;
      cout << endl;
      return Rational(numer, denom);
   } else if (choice == 2) {
      cout << "numerator? ";
      cin >> numer;
      cout << endl;
      return Rational(numer);
   } else {
      return Rational();
   }
}

void displayResult(const string &op, const Rational &lhs, const Rational&rhs, const Rational &result) {
   cout << "(";
   lhs.display();
   cout << ") " << op << " (";
   rhs.display();
   cout << ") = (";
   result.display();
   cout << ")";
}