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

Define a Fraction class similar to the one shown in earlier modules with num and

ID: 3870803 • Letter: D

Question

Define a Fraction class similar to the one shown in earlier modules with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member function called reduce() that gets called after arithmetic operations to reduce the fraction. +, -, *, / must return the result of the operation; e.g.: the sum or difference of the two fractions. +=, -=, *= and /= must assign the result of the operation to the object that's invoking the operation; i.e.: frac1 += frac2 must modify frac1 to make it equal to the sum of the two fractions, but frac1 + frac2 must simply return the sum.

If n1/d1 and n2/d2 are two fractions, their sum fraction's numerator is: n1 * d2 + n2 * d1 and its denominator is d1 * d2. To compare the two fractions, you can compare n1 * d2 with n2 * d1.

Define a class called FracList with private members fracPtr, a Fraction pointer and size to hold the size of the array of Fraction objects. Use the List container class developed in the modules as an example. Include constructor, destructor, copy constructor and overload the assignment, insertion (>>) and extraction (<<) operators. the [] operators for accessing array elements (both as lvalue and rvalue - see the example given in the last module) and relational operators <, >, ==, and !=. For < and >, compare the size of the two FracList objects and for == and != see if they're identical with identical array elements. Also, include two resize() member function to resize the list, one keeping the existing vales and one without keeping old values (see the List example). Also, include a sort and binary search function (see the example).

In main, create two FracList objects, ask the user how many elements to allocate for each list, read as many Fraction objects from the keyboard as specified by the user into each (using >> operator). Sort both lists using the sort member function and display them; then search for a value read from the user in both lists and print the index of the first occurrence or that it could not be found in either list.

Swap the two lists entered by the user so that the smaller size list (using < or > operator which only compares their size) is the first and larger size is the second. Use a swap function to swap the two lists. The example given in module did not use a function. Remember to swap two objects, you must either pass them by reference or pass their addresses into a couple of pointers. Print both lists again to verify the swap.

Resize the larger list to a size one larger than its original size, keeping all its values and assigning to its last element the sum of all other fractions (after reducing it), and print again.

An example run of the program:

Enter size of first fraction list: 5

Enter size of second fraction list: 3

Enter 5 fractions for first list: 1/4 1/8 2/3 1/3 1/2

Enter 3 fractions for second list: 3/4 1/5 1/4

Sorted lists:

1/8 1/4 1/3 1/2 2/3

1/5 1/4 3/4

Enter a fraction to search for: 1/4

1/4 was found at index 1 of list 1.

Swapped lists:

1/5 1/4 3/4

1/8 1/4 1/3 1/2 2/3

Resized list with sum of fractions at the end:

1/8 1/4 1/3 1/2 2/3 15/8

Print any key to continue.

Use separate files for class definitions (.h), function definitions and main program (3 files).

Explanation / Answer

Fraction.h:

#pragma once
#include <iostream>

class Fraction
{
private:
   int _x, _y;
   // Makes sure denominator is non negative.
   // Makes sure denom and nume are indivisible.
   void enforceConditions();
   void reduce();

public:
   Fraction();
   Fraction(int);
   Fraction(int, int);
   virtual ~Fraction();
   void setNumerator(int);
   void setDenominator(int);
   int getNumerator();
   int getDenominator();

   const Fraction& operator=(const Fraction &);
   Fraction operator +(Fraction);
   Fraction operator -(Fraction);
   Fraction operator *(Fraction);
   Fraction operator /(Fraction);

   int gcd(int num1, int num2);
};

Fraction.cpp:

#include "stdafx.h"
#include "Fraction.h"

// constructors
Fraction::Fraction() : _x{ 0 }, _y{ 1 }
{}

Fraction::Fraction(int x) : _x{ x }, _y{ 1 }
{}

Fraction::Fraction(int x, int y) : _x{ x }, _y{ y }
{
   enforceConditions();
}

// destructor
Fraction::~Fraction()
{}

/**
* Internal helper function to maintain integrity.
*/
void Fraction::enforceConditions()
{
   // if denominator is negative fix it.
   if (this->_y <= 0)
   {
       throw std::runtime_error("Denominator cannot be zero");
       this->_y *= -1;
   }

   // Make numer and denom indivisible.
   this->reduce();
}

// setters
void Fraction::setNumerator(int x) { this->_x = x; }
void Fraction::setDenominator(int y) { this->_y = y; }


int Fraction::getNumerator() { return this->_x; }
int Fraction::getDenominator() { return this->_y; }

const Fraction& Fraction::operator=(const Fraction & a)
{
   this->_x = a._x;
   this->_y = a._y;

   return a;
}

Fraction Fraction::operator +(Fraction b)
{
   Fraction res{};
   int numerator, denominator;

   numerator = (this->_x*b._y) + (this->_y*b._x);
   res.setNumerator(numerator);

   denominator = (b._y*this->_y);
   res.setDenominator(denominator);

   res.enforceConditions();

   return res;
}

Fraction Fraction::operator -(Fraction b)
{
   Fraction res{};
   int numerator, denominator;

   numerator = (this->_x*b._y) - (this->_y*b._x);
   res.setNumerator(numerator);

   denominator = (b._y*this->_y);
   res.setDenominator(denominator);

   res.enforceConditions();

   return res;
}


Fraction Fraction::operator *(Fraction b)
{
   Fraction res{};
   int numerator, denominator;

   numerator = this->_x*b._x;
   res.setNumerator(numerator);

   denominator = this->_y*b._y;
   res.setDenominator(denominator);

   res.enforceConditions();
   return res;
}

Fraction Fraction::operator /(Fraction b)
{
   Fraction res{};
   int numerator, denominator;

   numerator = this->_x*b._y;
   res.setNumerator(numerator);

   denominator = this->_y*b._x;
   res.setDenominator(denominator);

   res.enforceConditions();
   return res;
}


/**
* GCD helper function
*/
int Fraction::gcd(int num1, int num2)
{
   int temp{};  
   int res{};

   // swap if first number is smaller.
   if (num1 < num2) {
       temp = num1;
       num1 = num2;
       num2 = temp;
   }

   // iterate while we don't hit a zero
   do {      
       res = num1 % num2;
       num1 = num2;
       num2 = res;
   } while (res);

   return num1;
}

main.cpp:

// Fractions.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cassert>
#include "Fraction.h"
using namespace std;

int main() {
   // classical example.
   Fraction f1{ 2,3 };
   Fraction f2{ 3,4 };
   Fraction f3{ 3,8 };
   Fraction f4{};

   f4 = f1 + f2 + f3;

   assert(f4.getNumerator() == 43);
   assert(f4.getDenominator() == 24);
   cout << ".";

   cout << endl;  

   return 0;
}


void Fraction::reduce()
{
   int gcdValue{ gcd(this->_x, this->_y) };

   setNumerator(this->_x / gcdValue);
   setDenominator(this->_y / gcdValue);

   return;
}

Please like / rate thumbs up if the answer was helpful.

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