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

Please help C++ work Assignment 11.1 [45 points] For this assignment you will be

ID: 3917293 • Letter: P

Question

Please help C++ work

Assignment 11.1 [45 points]

For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions.

Please keep all of your code in one file for this week. We will separate things into three files for the next assignment.

Here are the client program and correct output.

Your class should support the following operations on Fraction objects:

Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors.

You should check to make sure that the denominator is not set to 0. The easiest way to do this is to use an assert statement: assert(inDenominator != 0); You can put this statement at the top of your constructor. Note that the variable in the assert() is the incoming parameter, not the data member. In order to use assert(), you must #include <cassert>

For this assignment, you may assume that all Fractions are positive. We'll fix that next week.

Printing a Fraction to a stream with an overloaded << operator. Next week we will get fancy with this, but for now just print the numerator, a forward-slash, and the denominator. No need to change improper Fractions to mixed numbers, and no need to reduce.

All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare Fractions to other Fractions as well as Fractions to integers. Either Fractions or integers can appear on either side of the binary comparison operator. You should only use one function for each operator.

The four basic arithmetic operations (+, -, *, /) should be supported. Again, they should allow Fractions to be combined with other Fractions, as well as with integers. Either Fractions or integers can appear on either side of the binary operator. Only use one function for each operator.

Note that no special handling is needed to handle the case of dividing by a Fraction that is equal to 0. If the client attempts to do this, they will get a runtime error, which is the same behavior they would expect if they tried to divide by an int or double that was equal to 0.

The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. Fractions can appear on the left-hand side, and Fractions or integers on the right-hand side.

The increment and decrement (++, --) operators should be supported in both prefix and postfix form for Fractions. To increment or decrement a Fraction means to add or subtract (respectively) one (1).

Additional Requirements and Hints:

You will not be graded on documentation on this assignment. You'll be working on the documentation next week.

The name of your class must be "Fraction". No variations will work.

Use exactly two data members.

You should not compare two Fractions by dividing the numerator by the denominator. This is not guaranteed to give you the correct result every time, because of the way that double values are stored internally by the computer. I would cross multiply and compare the products.

Don't go to a lot of trouble to find the common denominator (when adding or subtracting). Simply multiply the denominators together.

The last two bullets bring up an interesting issue: if your denominators are really big, multiplying them together (or cross multiplying) may give you a number that is too big to store in an int variable. This is called overflow. The rule for this assignment is: don't worry about overflow in these two situations.

My solution has 20 member functions (including friend functions). All of them are less than 4 lines long. I'm not saying yours has to be like this, but it shouldn't be way off.

Do not use as a resource a supplementary text or website if it includes a Fraction class (or rational or ratio or whatever).

Getting Started

Here are some suggestions for those of you who have trouble just figuring out where to start with assignment 1. Remember to use iterative development. That means start with the smallest, simplest subset of the final product that you can, make sure it works, and then start adding things to it one at a time (preferably the simple things first, if possible).

Start with everything in one file if you're not 100% comfortable with using three files: class declaration at the top, then member function definitions, then main. Start with just a default constructor and a stream insertion operator. For now, don't even worry about mixed numbers, just write the stream insertion operator so that it works with proper fractions. Test this out with a client program something like this:

(You should get output of "0/1" because you should have initialized the fraction to 0/1 in your constructor.)

client code

Explanation / Answer

ScreenShot

-------------------------------------------------------------------------------------

Program

//Header file for I/O
#include<iostream>
#include<string>
using namespace std;
//Fraction class
class Fraction {
   int numerator;
   int denominator;
public:
   //Constructors
   Fraction() {
       numerator = 0;
       denominator = 1;
   }
   Fraction(int num) {
       numerator = num;
       denominator = 1;
   }
   Fraction(int num, int den) {
       if (den == 0) {
           numerator = num;
           denominator = 1;
       }
       else {
           numerator = num;
           denominator = den;
       }
   }
   //Cout overloading
   friend ostream & operator << (ostream &out, const Fraction &c)
   {
       out << c.numerator << "/" << c.denominator << endl;;
       return out;
   }
   //Less than
   friend bool operator < (Fraction f1, const Fraction &f) {
       if (f1.numerator*f.denominator < f1.denominator*f.numerator)
           return true;
       else
           return false;
   }
   //Greater than
   friend bool operator > (Fraction f1, const Fraction &f) {
       if (f1.numerator*f.denominator > f1.denominator*f.numerator)
           return true;
       else
           return false;
   }
   //Greater than equal
   friend bool operator >= (Fraction f1, const Fraction &f) {
       if (f1.numerator*f.denominator >= f1.denominator*f.numerator)
           return true;
       else
           return false;
   }
   //Less than equal
   friend bool operator <= (Fraction f1, const Fraction &f) {
       if (f1.numerator*f.denominator <= f1.denominator*f.numerator)
           return true;
       else
           return false;
   }
   //Equal to overload
   friend bool operator == (Fraction f1, const Fraction &f) {
       if (f1.numerator*f.denominator == f1.denominator*f.numerator)
           return true;
       else
           return false;
   }
   //Not equal overload
   friend bool operator != (Fraction f1, const Fraction &f) {
       if (f1.numerator*f.denominator != f1.denominator*f.numerator)
           return true;
       else
           return false;
   }
   //Overload addition
   friend Fraction operator +(Fraction f1, const Fraction &f) {
       Fraction tmp((f1.numerator*f.denominator + f1.denominator*f1.numerator),(f1.denominator*f.denominator));
       return tmp;
   }
   //Overload subtraction
   friend Fraction operator -(Fraction f1, const Fraction &f) {
       Fraction tmp((f1.numerator*f.denominator - f1.denominator*f1.numerator), (f1.denominator*f.denominator));
           return tmp;
   }
   //Overload Multiplication
   friend Fraction operator *(Fraction f1, const Fraction &f) {
       Fraction tmp((f1.numerator*f.numerator), (f1.denominator*f.denominator));
       return tmp;
   }
   //Overload division
   friend Fraction operator /(Fraction f1, const Fraction &f) {
       Fraction tmp(f1.numerator*f.denominator, f1.denominator*f.numerator);
           return tmp;;
   }
   //Overload addition with number
   friend Fraction operator +(const Fraction &f,int num) {
       Fraction tmp((f.denominator*num)+f.numerator, f.denominator);
       return tmp;
   }
   //Overload subtraction with number
   friend Fraction operator -(const Fraction &f, int num) {
       Fraction tmp((f.denominator*num) - f.numerator, f.denominator);
       return tmp;
   }
   //Overload multiplication with number
   friend Fraction operator *(const Fraction &f, int num) {
       Fraction tmp((f.numerator*num), f.denominator);
       return tmp;
   }
   //Overload division with number
   friend Fraction operator /(const Fraction &f, int num) {
       Fraction tmp(f.numerator, f.denominator*num);
       return tmp;;
   }
   //Overloading basic operations inversely
   friend Fraction operator +(int num, const Fraction &f) {
       Fraction tmp((f.denominator*num) + f.numerator, f.denominator);
       return tmp;
   }
   friend Fraction operator -(int num, const Fraction &f) {
       Fraction tmp((f.denominator*num) - f.numerator, f.denominator);
       return tmp;
   }
   friend Fraction operator *(int num,const Fraction &f) {
       Fraction tmp((f.numerator*num), f.denominator);
       return tmp;
   }
   friend Fraction operator /(int num,const Fraction &f) {
       Fraction tmp(f.denominator*num,f.numerator);
       return tmp;
   }
   //Overload Addition equal
   friend Fraction operator +=(Fraction &f1, const Fraction &f) {
       Fraction tmp((f.numerator*f1.denominator + f1.numerator*f.denominator), (f1.denominator*f.denominator));
       f1 = tmp;
       return f1;
   }
   //Overload sub equal
   friend Fraction operator -=(Fraction &f1, const Fraction &f) {
       Fraction tmp((f.numerator*f1.denominator - f1.numerator*f.denominator), (f1.denominator*f.denominator));
       f1 = tmp;
       return f1;
   }
   //Overload mul equal
   friend Fraction operator *=(Fraction &f1, const Fraction &f) {
       Fraction tmp((f1.numerator*f.numerator), (f1.denominator*f.denominator));
       f1 = tmp;
       return f1;
   }
   //Overload div equal
   friend Fraction operator /=(Fraction &f1, const Fraction &f) {
       Fraction tmp(f1.numerator*f.denominator, f1.denominator*f.numerator);
       f1 = tmp;
       return f1;
   }
   //Overload all above with number
   friend Fraction operator +=(Fraction &f, int num) {
       Fraction tmp((f.denominator*num) + f.numerator, f.denominator);
       f = tmp;
       return f;
   }
   friend Fraction operator -=(Fraction &f, int num) {
       Fraction tmp((f.denominator*num) - f.numerator, f.denominator);
       f = tmp;
       return f;
   }
   friend Fraction operator *=(Fraction &f, int num) {
       Fraction tmp((f.numerator*num), f.denominator);
       f = tmp;
       return f;
   }
   friend Fraction operator /=(Fraction &f, int num) {
       Fraction tmp(f.numerator, f.denominator*num);
       f = tmp;
       return f;
   }
  
};
//Function prototype
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
string boolString(bool convertMe);
//Test method
int main()
{
   //Simple test
   Fraction f1;
   cout << f1;
   //Call all methods
   BasicTest();
   RelationTest();
   BinaryMathTest();
   MathAssignTest();
    return 0;
}
//Function to test general fraction generation
void BasicTest()
{
   cout << " ----- Testing basic Fraction creation & printing ";

   const Fraction fr[] = { Fraction(4, 8), Fraction(-15,21),
       Fraction(10), Fraction(12, -3),
       Fraction(), Fraction(28, 6), Fraction(0, 12) };

   for (int i = 0; i < 7; i++) {
       cout << "Fraction [" << i << "] = " << fr[i] << endl;
   }
}
string boolString(bool convertMe) {
   if (convertMe) {
       return "true";
   }
   else {
       return "false";
   }
}

//Function for logical test
void RelationTest()
{
   cout << " ----- Testing relational operators between Fractions ";

   const Fraction fr[] = { Fraction(3, 6), Fraction(1,2), Fraction(-15,30),
       Fraction(1,10), Fraction(0,1), Fraction(0,2) };

   for (int i = 0; i < 5; i++) {
       cout << "Comparing " << fr[i] << " to " << fr[i + 1] << endl;
       cout << " Is left < right? " << boolString(fr[i] < fr[i + 1]) << endl;
       cout << " Is left <= right? " << boolString(fr[i] <= fr[i + 1]) << endl;
       cout << " Is left > right? " << boolString(fr[i] > fr[i + 1]) << endl;
       cout << " Is left >= right? " << boolString(fr[i] >= fr[i + 1]) << endl;
       cout << " Does left == right? " << boolString(fr[i] == fr[i + 1]) << endl;
       cout << " Does left != right ? " << boolString(fr[i] != fr[i + 1]) << endl;
   }

   cout << " ----- Testing relations between Fractions and integers ";
   Fraction f(-3, 6);
   int num = 2;
   cout << "Comparing " << f << " to " << num << endl;
   cout << " Is left < right? " << boolString(f < num) << endl;
   cout << " Is left <= right? " << boolString(f <= num) << endl;
   cout << " Is left > right? " << boolString(f > num) << endl;
   cout << " Is left >= right? " << boolString(f >= num) << endl;
   cout << " Does left == right? " << boolString(f == num) << endl;
   cout << " Does left != right ? " << boolString(f != num) << endl;

   Fraction g(1, 4);
   num = -3;
   cout << "Comparing " << num << " to " << g << endl;
   cout << " Is left < right? " << boolString(num < g) << endl;
   cout << " Is left <= right? " << boolString(num <= g) << endl;
   cout << " Is left > right? " << boolString(num > g) << endl;
   cout << " Is left >= right? " << boolString(num >= g) << endl;
   cout << " Does left == right? " << boolString(num == g) << endl;
   cout << " Does left != right ? " << boolString(num != g) << endl;
}
//Function for basic operations

void BinaryMathTest()
{
   cout << " ----- Testing binary arithmetic between Fractions ";

   const Fraction fr[] = { Fraction(1, 6), Fraction(1,3),
       Fraction(-2,3), Fraction(5), Fraction(-4,3) };

   for (int i = 0; i < 4; i++) {
       cout << fr[i] << " + " << fr[i + 1] << " = " << fr[i] + fr[i + 1] << endl;
       cout << fr[i] << " - " << fr[i + 1] << " = " << fr[i] - fr[i + 1] << endl;
       cout << fr[i] << " * " << fr[i + 1] << " = " << fr[i] * fr[i + 1] << endl;
       cout << fr[i] << " / " << fr[i + 1] << " = " << fr[i] / fr[i + 1] << endl;
   }

   cout << " ----- Testing arithmetic between Fractions and integers ";
   Fraction f(-1, 2);
   int num = 4;
   cout << f << " + " << num << " = " << f + num << endl;
   cout << f << " - " << num << " = " << f - num << endl;
   cout << f << " * " << num << " = " << f * num << endl;
   cout << f << " / " << num << " = " << f / num << endl;

   Fraction g(-1, 2);
   num = 3;
   cout << num << " + " << g << " = " << num + g << endl;
   cout << num << " - " << g << " = " << num - g << endl;
   cout << num << " * " << g << " = " << num * g << endl;
   cout << num << " / " << g << " = " << num / g << endl;
}
//Function for advance operations
void MathAssignTest()
{
   cout << " ----- Testing shorthand arithmetic assignment on Fractions ";

   Fraction fr[] = { Fraction(1, 6), Fraction(4),
       Fraction(-1,2), Fraction(5) };

   for (int i = 0; i < 3; i++) {
       cout << fr[i] << " += " << fr[i + 1] << " = ";
       cout << (fr[i] += fr[i + 1]) << endl;
       cout << fr[i] << " -= " << fr[i + 1] << " = ";
       cout << (fr[i] -= fr[i + 1]) << endl;
       cout << fr[i] << " *= " << fr[i + 1] << " = ";
       cout << (fr[i] *= fr[i + 1]) << endl;
       cout << fr[i] << " /= " << fr[i + 1] << " = ";
       cout << (fr[i] /= fr[i + 1]) << endl;
   }

   cout << " ----- Testing shorthand arithmetic assignment using integers ";
   Fraction f(-1, 3);
   int num = 3;
   cout << f << " += " << num << " = ";
   cout << (f += num) << endl;
   cout << f << " -= " << num << " = ";
   cout << (f -= num) << endl;
   cout << f << " *= " << num << " = ";
   cout << (f *= num) << endl;
   cout << f << " /= " << num << " = ";
   cout << (f /= num) << endl;
}