(Solved Homework): cout…

REDUCE FRACTIONS:

Add a private simplify) function to your class and call it from the appropriate member functions. (If you write your code the way that 90% of students write it, there will be 6 places where you need to call it. But if you call it a different number of times and your class works, thats also fine.) The best way to do this is to make the function a void function with no parameters that reduces the calling object Recall that simplifying or reducing a fraction is a separate task from converting it from an improper fraction to a mixed number. Make sure you keep those two tasks separate in your mind For now (until you get down to the part of the assignment where you improve your insertion operator) your fractions will still be printed as improper fractions, not mixed numbers. In other words, 19/3 will still be 19/3, not 6+1/3. Make sure that your class will reduce ANY fraction, not just the fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In ather words, you should ensure that all fraction objects are reduced before the end of any member function. To put it yet another way: each member function must be able to assume that all fraction objects are in simple form when it begins execution. You must create your own algorithm for reducing fractions. Dont look up an already existing algorithm for reducing fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, dont use Euclids algorithm. Dont worry about being efficient. Its fine to have your function check every possible facto, even if it would be more efficient to just check prime numbers, Just create something of your own that works correctly on ANY fraction. Your simplify() function should also ensure that the denominator is never negative. If the denominator is negative, fix this by multiplying numerator and denominator by -1 Better Insertion Operator [10 points] Now modify your overloaded < operator so that improper fractions are printed as mixed numbers. Whole numbers should print without a denominator (e.g. not 3/1 but just 3). Improper fractions should be printed as a mixed number with a +sign between the two parts (2+1/2). Negative fractions should be printed with a leading minus sign. Note that your class should have only two data members. Fractions will be stored as improper fractions. The<< operator is responsible for printing the improper fraction as a mixed number. Also, the+ in mixed numbers does not mean add. It is simply a separator (to separate the integer part from the fraction part of the number). So the fraction negative two and one-sixth would be written as -2+1/6, even though -2 plus 1/6 is not what we mean. Extraction Operator [10 points] You should be able to read any of the formats described above (mixed number, negative number, whole numbers etc.). You may assume that there are no spaces or formatting errors in the fractions that you read. You may need to exceed 15 lines for this function. My solution is about 20 lines long Since your extraction operator should not consume anything after the end of the fraction being read, you will probably want to use the .peek) function to look ahead in the input stream and see what the next character is after the first number is read. If its not either a or a +, then you are done reading and should read no further. I have something like this int temp in >> temp; } else if (in-peek ( )-ソ·H t else ( doSomething dosomethingElse. doThirdoption Hint: You dont need to detect or read the minus operator as a separate character. When you use the extraction operator to read an int, it will interpret a leading minus sign correctly. So, for example, you shouldnt have if (in.peek() Three Files and Namespaces [10 points] Split the project up into three files: client file, implementation file, and header (specification) file. Also, place the class declaration and implementation in a namespace. Normally one would call a namespace something more likely to be unique, but for purposes of convenience we will all call our namespace cs fraction. Namespaces are covered in lesson 16.15 Add Documentation [10 points] See Style Convention 1, especially Style Convention 1D Every public member function and friend function, however simple, must have a precondition (if there is one) and a postcondition listed in the header file. Here is a two part explanation of pre- and post- conditions. Youll have to

Don't use plagiarized sources. Get Your Custom Essay on
(Solved Homework): cout…
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

Client Program:

#include 
#include "Fraction.h"
#include 
#include 
#include 
using namespace std;
using namespace cs_Fraction;

void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(ifstream& in);
string boolString(bool convertMe);


int main()
{
    BasicTest();
    RelationTest();
    BinaryMathTest();
    MathAssignTest();
}





void BasicTest()
{
    cout << "n----- Testing basic Fraction creation & printingn";
    cout << "(Fractions should be in reduced form, and as mixed numbers.)n";
    
    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;
    }
        
        
    cout << "n----- Now reading Fractions from filen";
    ifstream in("Fraction.txt");
    assert(in);
    while (!eof(in)) {
        Fraction f;     
        if (in.peek() == '#') {   
            in.ignore(128, 'n');                       //skip this line, it's a comment
        } else {
            in >> f;
            cout << "Read Fraction = " << f << endl;
        }
    }
}


bool eof(ifstream& in)
{
        char ch;
        in >> ch;
        in.putback(ch);
        return !in;
}
        




string boolString(bool convertMe) {
        if (convertMe) {
                return "true";
        } else {
                return "false";
        }
}


void RelationTest()
{
    cout << "n----- Testing relational operators between Fractionsn";

    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 << "tIs left < right? " << boolString(fr[i] < fr[i+1]) << endl;
          cout << "tIs left <= right? " << boolString(fr[i] <= fr[i+1]) << endl;
          cout << "tIs left > right? " << boolString(fr[i] > fr[i+1]) << endl;
          cout << "tIs left >= right? " << boolString(fr[i] >= fr[i+1]) << endl;
          cout << "tDoes left == right? " << boolString(fr[i] == fr[i+1]) << endl;
          cout << "tDoes left != right ? " << boolString(fr[i] != fr[i+1]) << endl;
    }
 
    cout << "n----- Testing relations between Fractions and integersn";
    Fraction f(-3,6);
    int num = 2;
    cout << "Comparing " << f << " to " << num << endl;
    cout << "tIs left < right? " << boolString(f < num) << endl;
    cout << "tIs left <= right? " << boolString(f <= num) << endl;
    cout << "tIs left > right? " << boolString(f > num) << endl;
    cout << "tIs left >= right? " << boolString(f >= num) << endl;
    cout << "tDoes left == right? " << boolString(f == num) << endl;
    cout << "tDoes left != right ? " << boolString(f != num) << endl;
    
    Fraction g(1,4);
    num = -3;
    cout << "Comparing " << num << " to " << g << endl;
    cout << "tIs left < right? " << boolString(num < g) << endl;
    cout << "tIs left <= right? " << boolString(num <= g) << endl;
    cout << "tIs left > right? " << boolString(num > g) << endl;
    cout << "tIs left >= right? " << boolString(num >= g) << endl;
    cout << "tDoes left == right? " << boolString(num == g) << endl;
    cout << "tDoes left != right ? " << boolString(num != g) << endl;  
}





void BinaryMathTest()
{    
    cout << "n----- Testing binary arithmetic between Fractionsn";
    
    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 << "n----- Testing arithmetic between Fractions and integersn";
    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;
}






void MathAssignTest()
{    
    cout << "n----- Testing shorthand arithmetic assignment on Fractionsn";
    
    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 << "n----- Testing shorthand arithmetic assignment using integersn";
    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;

    cout << "n----- Testing increment/decrement prefix and postfixn";
    Fraction g(-1, 3);
    cout << "Now g = " << g << endl;
    cout << "g++ = " << g++ << endl;
    cout << "Now g = " << g << endl;
    cout << "++g = " << ++g << endl;
    cout << "Now g = " << g << endl;
    cout << "g-- = " << g-- << endl;
    cout << "Now g = " << g << endl;
    cout << "--g = " << --g << endl;
    cout << "Now g = " << g << endl;
}

Frac data:

# This file shows the patterns your Fraction class needs to be able to
# read.  A Fraction may be just a single integer, two integers separated by
# a slash, or a mixed number which consists of an integer, followed by a +
# and then two integers with a slash.  A minus sign may appear in the
# very first character to indicate the whole Fraction is negative.
# No white space is allowed in between the component parts of a Fraction.
#
1/3
3/6
3072/4096
-4/5
12/2
5
-8
21/15
-50/3
1+1/4
1+5/5
-4+3/12
-10+10/12

Output:

----- Testing basic Fraction creation & printing
(Fractions should be in reduced form, and as mixed numbers.)
Fraction [0] = 1/2
Fraction [1] = -5/7
Fraction [2] = 10
Fraction [3] = -4
Fraction [4] = 0
Fraction [5] = 4+2/3
Fraction [6] = 0

----- Now reading Fractions from file
Read Fraction = 1/3
Read Fraction = 1/2
Read Fraction = 3/4
Read Fraction = -4/5
Read Fraction = 6
Read Fraction = 5
Read Fraction = -8
Read Fraction = 1+2/5
Read Fraction = -16+2/3
Read Fraction = 1+1/4
Read Fraction = 2
Read Fraction = -4+1/4
Read Fraction = -10+5/6

----- Testing relational operators between Fractions
Comparing 1/2 to 1/2
        Is left < right? false
        Is left <= right? true
        Is left > right? false
        Is left >= right? true
        Does left == right? true
        Does left != right ? false
Comparing 1/2 to -1/2
        Is left < right? false
        Is left <= right? false
        Is left > right? true
        Is left >= right? true
        Does left == right? false
        Does left != right ? true
Comparing -1/2 to 1/10
        Is left < right? true
        Is left <= right? true
        Is left > right? false
        Is left >= right? false
        Does left == right? false
        Does left != right ? true
Comparing 1/10 to 0
        Is left < right? false
        Is left <= right? false
        Is left > right? true
        Is left >= right? true
        Does left == right? false
        Does left != right ? true
Comparing 0 to 0
        Is left < right? false
        Is left <= right? true
        Is left > right? false
        Is left >= right? true
        Does left == right? true
        Does left != right ? false

----- Testing relations between Fractions and integers
Comparing -1/2 to 2
        Is left < right? true
        Is left <= right? true
        Is left > right? false
        Is left >= right? false
        Does left == right? false
        Does left != right ? true
Comparing -3 to 1/4
        Is left < right? true
        Is left <= right? true
        Is left > right? false
        Is left >= right? false
        Does left == right? false
        Does left != right ? true

----- Testing binary arithmetic between Fractions
1/6 + 1/3 = 1/2
1/6 - 1/3 = -1/6
1/6 * 1/3 = 1/18
1/6 / 1/3 = 1/2
1/3 + -2/3 = -1/3
1/3 - -2/3 = 1
1/3 * -2/3 = -2/9
1/3 / -2/3 = -1/2
-2/3 + 5 = 4+1/3
-2/3 - 5 = -5+2/3
-2/3 * 5 = -3+1/3
-2/3 / 5 = -2/15
5 + -1+1/3 = 3+2/3
5 - -1+1/3 = 6+1/3
5 * -1+1/3 = -6+2/3
5 / -1+1/3 = -3+3/4

----- Testing arithmetic between Fractions and integers
-1/2 + 4 = 3+1/2
-1/2 - 4 = -4+1/2
-1/2 * 4 = -2
-1/2 / 4 = -1/8
3 + -1/2 = 2+1/2
3 - -1/2 = 3+1/2
3 * -1/2 = -1+1/2
3 / -1/2 = -6

----- Testing shorthand arithmetic assignment on Fractions
1/6 += 4 = 4+1/6
4+1/6 -= 4 = 1/6
1/6 *= 4 = 2/3
2/3 /= 4 = 1/6
4 += -1/2 = 3+1/2
3+1/2 -= -1/2 = 4
4 *= -1/2 = -2
-2 /= -1/2 = 4
-1/2 += 5 = 4+1/2
4+1/2 -= 5 = -1/2
-1/2 *= 5 = -2+1/2
-2+1/2 /= 5 = -1/2

----- Testing shorthand arithmetic assignment using integers
-1/3 += 3 = 2+2/3
2+2/3 -= 3 = -1/3
-1/3 *= 3 = -1
-1 /= 3 = -1/3

----- Testing increment/decrement prefix and postfix
Now g = -1/3
g++ = -1/3
Now g = 2/3
++g = 1+2/3
Now g = 1+2/3
g-- = 1+2/3
Now g = 2/3
--g = -1/3
Now g = -1/3

THANKS IN ADVANCE!

Add a private “simplify)” function to your class and call it from the appropriate member functions. (If you write your code the way that 90% of students write it, there will be 6 places where you need to call it. But if you call it a different number of times and your class works, that’s also fine.) The best way to do this is to make the function a void function with no parameters that reduces the calling object Recall that “simplifying” or “reducing” a fraction is a separate task from converting it from an improper fraction to a mixed number. Make sure you keep those two tasks separate in your mind For now (until you get down to the part of the assignment where you improve your insertion operator) your fractions will still be printed as improper fractions, not mixed numbers. In other words, 19/3 will still be 19/3, not 6+1/3. Make sure that your class will reduce ANY fraction, not just the fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In ather words, you should ensure that all fraction objects are reduced before the end of any member function. To put it yet another way: each member function must be able to assume that all fraction objects are in simple form when it begins execution. You must create your own algorithm for reducing fractions. Don’t look up an already existing algorithm for reducing fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don’t use Euclid’s algorithm. Don’t worry about being efficient. It’s fine to have your function check every possible facto, even if it would be more efficient to just check prime numbers, Just create something of your own that works correctly on ANY fraction. Your simplify() function should also ensure that the denominator is never negative. If the denominator is negative, fix this by multiplying numerator and denominator by -1 Better Insertion Operator [10 points] Now modify your overloaded

Expert Answer

 

main.cpp
————————————————-
#include <iostream>
#include <string>
using namespace std;

class fraction {

//variables
private: int numerator;
int denominator;

//functions
public:
// default constructor
fraction();
//constructor with one agrument
fraction(int);
//constructor with two agruments
fraction(int, int);

//overloaded << operator
friend std::ostream& operator<<(std::ostream &out, const fraction &inFraction);

//Compare operators
friend bool operator== (const fraction& inLeft, const fraction& inRight);
friend bool operator!= (const fraction& inLeft, const fraction& inRight);
friend bool operator> (const fraction& inLeft, const fraction& inRight);
friend bool operator>= (const fraction& inLeft, const fraction& inRight);
friend bool operator< (const fraction& inLeft, const fraction& inRight);
friend bool operator<= (const fraction& inLeft, const fraction& inRight);

//Operations operators
friend fraction operator+ (const fraction& inLeft, const fraction& inRight);
friend fraction operator- (const fraction& inLeft, const fraction& inRight);
friend fraction operator* (const fraction& inLeft, const fraction& inRight);
friend fraction operator/ (const fraction& inLeft, const fraction& inRight);

//
fraction operator+= (const fraction& inFraction);
fraction operator-= (const fraction& inFraction);
fraction operator*= (const fraction& inFraction);
fraction operator/= (const fraction& inFraction);

//Prefix and Postfix
fraction operator++ ();
fraction operator++ (int);
fraction operator– ();
fraction operator– (int);

};

//**************************************CONSTRUCTORS***************************************************
/*
* default constructor
*/
fraction:: fraction() {
numerator = 0;
denominator = 1;
}

/*
* constructor with one agrument
*/
fraction:: fraction(int number) {
numerator = number;
denominator = 1;
}

/*
* constructor with two agruments
*/
fraction:: fraction(int inNumerator,int inDenominator)
{
numerator = inNumerator;

if(inDenominator == 0)
{
denominator = 1;
}
else
{
denominator = inDenominator;
}
}

//**********************************OUT OPERATOR***************************************
ostream& operator<< (ostream& out, const fraction& inFraction) {
return out << ‘(‘ << inFraction.numerator << ‘/’ << inFraction.denominator << ‘)’;
}

//*************************************COMPARE OPERATORS*********************************************
/*
*
*/
bool operator< (const fraction& inLeft, const fraction& inRight)
{
return (inLeft.numerator * inRight.denominator < inRight.numerator * inLeft.denominator);
}

/*
*
*/
bool operator<= (const fraction& inLeft, const fraction& inRight)
{
return (inLeft.numerator * inRight.denominator <= inRight.numerator * inLeft.denominator);
}

/*
*
*/
bool operator> (const fraction& inLeft, const fraction& inRight)
{
return (inLeft.numerator * inRight.denominator > inRight.numerator * inLeft.denominator);
}

/*
*
*/
bool operator>= (const fraction& inLeft, const fraction& inRight)
{
return (inLeft.numerator * inRight.denominator >= inRight.numerator * inLeft.denominator);
}

/*
*
*/
bool operator== (const fraction& inLeft, const fraction& inRight)
{
return (inLeft.numerator * inRight.denominator == inRight.numerator * inLeft.denominator);
}

/*
*
*/
bool operator!= (const fraction& inLeft, const fraction& inRight)
{
return (inLeft.numerator * inRight.denominator != inRight.numerator * inLeft.denominator);
}

//***************************************Operators*****************************************
/*
*
*/
fraction operator+ (const fraction& inLeft, const fraction& inRight) {
fraction result(inLeft.numerator * inRight.denominator + inRight.numerator * inLeft.denominator,
inLeft.denominator * inRight.denominator );
return result;
}

/*
*
*/
fraction operator- (const fraction& inLeft, const fraction& inRight) {
fraction result(inLeft.numerator * inRight.denominator –
inRight.numerator * inLeft.denominator,
inLeft.denominator * inRight.denominator);
return result;
}

/*
*
*/
fraction operator* (const fraction& inLeft, const fraction& inRight) {
fraction result(inLeft.numerator * inRight.numerator,
inLeft.denominator * inRight.denominator);
return result;
}

/*
*
*/
fraction operator/ (const fraction& inLeft, const fraction& inRight) {
fraction result(inLeft.numerator * inRight.denominator,
inLeft.denominator * inRight.numerator);
return result;
}

/*
*
*/
fraction fraction::operator+=(const fraction &inFraction) {
*this = *this + inFraction;
return *this;
}

/*
*
*/
fraction fraction::operator-=(const fraction &inFraction) {
*this = *this – inFraction;
return *this;
}

/*
*
*/
fraction fraction::operator*=(const fraction &inFraction) {
*this = *this * inFraction;
return *this;
}

/*
*
*/
fraction fraction::operator/=(const fraction &inFraction) {
*this = *this / inFraction;
return *this;
}

//*****************************PREFIX AND POSTFIX**************************************
/*
*
*/
fraction fraction::operator++ () {
numerator += denominator;
return *this;
}

/*
*
*/
fraction fraction::operator++ (int) {
fraction temp(numerator, denominator);
numerator += denominator;
return temp;
}

/*
*
*/
fraction fraction::operator– () {
numerator -= denominator;
return *this;
}

/*
*
*/
fraction fraction::operator– (int) {
fraction temp(numerator, denominator);
numerator -= denominator;
return temp;
}

//*****************************************TEST***********************************

void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
string boolString(bool convertMe);

int main()
{
BasicTest();
RelationTest();
BinaryMathTest();
MathAssignTest();
}

void BasicTest()
{
cout << “n—– Testing basic fraction creation & printingn”;

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”;
}
}

void RelationTest()
{
cout << “n—– Testing relational operators between fractionsn”;

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 << “tIs left < right? ” << boolString(fr[i] < fr[i+1]) << endl;
cout << “tIs left <= right? ” << boolString(fr[i] <= fr[i+1]) << endl;
cout << “tIs left > right? ” << boolString(fr[i] > fr[i+1]) << endl;
cout << “tIs left >= right? ” << boolString(fr[i] >= fr[i+1]) << endl;
cout << “tDoes left == right? ” << boolString(fr[i] == fr[i+1]) << endl;
cout << “tDoes left != right ? ” << boolString(fr[i] != fr[i+1]) << endl;
}

cout << “n—– Testing relations between fractions and integersn”;
fraction f(-3,6);
int num = 2;
cout << “Comparing ” << f << ” to ” << num << endl;
cout << “tIs left < right? ” << boolString(f < num) << endl;
cout << “tIs left <= right? ” << boolString(f <= num) << endl;
cout << “tIs left > right? ” << boolString(f > num) << endl;
cout << “tIs left >= right? ” << boolString(f >= num) << endl;
cout << “tDoes left == right? ” << boolString(f == num) << endl;
cout << “tDoes left != right ? ” << boolString(f != num) << endl;

fraction g(1,4);
num = -3;
cout << “Comparing ” << num << ” to ” << g << endl;
cout << “tIs left < right? ” << boolString(num < g) << endl;
cout << “tIs left <= right? ” << boolString(num <= g) << endl;
cout << “tIs left > right? ” << boolString(num > g) << endl;
cout << “tIs left >= right? ” << boolString(num >= g) << endl;
cout << “tDoes left == right? ” << boolString(num == g) << endl;
cout << “tDoes left != right ? ” << boolString(num != g) << endl;
}

void BinaryMathTest()
{
cout << “n—– Testing binary arithmetic between fractionsn”;

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 << “n—– Testing arithmetic between fractions and integersn”;
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;
}

void MathAssignTest()
{
cout << “n—– Testing shorthand arithmetic assignment on fractionsn”;

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 << “n—– Testing shorthand arithmetic assignment using integersn”;
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;

cout << “n—– Testing increment/decrement prefix and postfixn”;
fraction g(-1, 3);
cout << “Now g = ” << g << endl;
cout << “g++ = ” << g++ << endl;
cout << “Now g = ” << g << endl;
cout << “++g = ” << ++g << endl;
cout << “Now g = ” << g << endl;
cout << “g– = ” << g– << endl;
cout << “Now g = ” << g << endl;
cout << “–g = ” << –g << endl;
cout << “Now g = ” << g << endl;
}

———————————————————————–
fraction.cpp
—————————————-
#include “fraction.h”

namespace cs_fraction {

using namespace std;

fraction::fraction(int inNumerator, int inDenominator) {
assert(inDenominator != 0);
numerator = inNumerator;
denominator = inDenominator;
this->simplify();
}

ostream& operator<< (ostream& out, const fraction& inFraction) {

if (abs(inFraction.numerator) >= abs(inFraction.denominator)) {
out << inFraction.numerator / inFraction.denominator;

if (inFraction.numerator % inFraction.denominator != 0) {
out << “+” << abs(inFraction.numerator % inFraction.denominator) << “/” <<
inFraction.denominator;
}
}

else if (inFraction.numerator == 0) {
out << 0;
}

else {
out << inFraction.numerator << “/” << inFraction.denominator;
}

return out;
}

//Takes user input of mixed numbers, negative numbers, whole numbers or fractions
//and stores them in fraction f.
istream& operator>> (istream& in, fraction& inFraction)
{
int temp;
in >> temp;
if (in.peek() == ‘+’) {

in.ignore();
in >> inFraction.numerator;
in.ignore();
in >> inFraction.denominator;

if(temp < 0) {

temp *= -1;
inFraction.numerator += temp * inFraction.denominator;
inFraction.numerator *= -1;
}

else {

inFraction.numerator += temp * inFraction.denominator;
}
}
else if (in.peek() == ‘/’) {

in.ignore();
in >> inFraction.denominator;
inFraction.numerator = temp;
}
else {

inFraction.numerator = temp;
inFraction.denominator = 1;
}

inFraction.simplify();
return in;
}

bool operator< (const fraction& inLeft, const fraction& inRight) {

return (inLeft.numerator * inRight.denominator < inRight.numerator * inLeft.denominator);
}

bool operator<= (const fraction& inLeft, const fraction& inRight) {

return (inLeft.numerator * inRight.denominator <= inRight.numerator * inLeft.denominator);
}

bool operator> (const fraction& inLeft, const fraction& inRight) {

return (inLeft.numerator * inRight.denominator > inRight.numerator * inLeft.denominator);
}

bool operator>= (const fraction& inLeft, const fraction& inRight) {

return (inLeft.numerator * inRight.denominator >= inRight.numerator * inLeft.denominator);
}

bool operator== (const fraction& inLeft, const fraction& inRight) {

return (inLeft.numerator * inRight.denominator == inRight.numerator * inLeft.denominator);
}

bool operator!= (const fraction& inLeft, const fraction& inRight) {

return (inLeft.numerator * inRight.denominator != inRight.numerator * inLeft.denominator);
}

fraction operator+ (const fraction& inLeft, const fraction& inRight) {

fraction result(inLeft.numerator * inRight.denominator +
inRight.numerator * inLeft.denominator,
inLeft.denominator * inRight.denominator );
return result;
}

fraction operator- (const fraction& inLeft, const fraction& inRight) {

fraction result(inLeft.numerator * inRight.denominator –
inRight.numerator * inLeft.denominator,
inLeft.denominator * inRight.denominator);
return result;
}

fraction operator* (const fraction& inLeft, const fraction& inRight) {

fraction result(inLeft.numerator * inRight.numerator,
inLeft.denominator * inRight.denominator);
return result;
}

fraction operator/ (const fraction& inLeft, const fraction& inRight) {

fraction result(inLeft.numerator * inRight.denominator,
inLeft.denominator * inRight.numerator);
return result;
}

fraction fraction::operator+=(const fraction &inFraction) {

*this = *this + inFraction;
return *this;
}

fraction fraction::operator-=(const fraction &inFraction) {

*this = *this – inFraction;
return *this;
}

fraction fraction::operator*=(const fraction &inFraction) {

*this = *this * inFraction;
return *this;
}

fraction fraction::operator/=(const fraction &inFraction) {

*this = *this / inFraction;
return *this;
}

fraction fraction::operator++ () {

numerator += denominator;
return *this;
}

fraction fraction::operator++ (int) {

fraction temp(numerator, denominator);
numerator += denominator;
return temp;
}

fraction fraction::operator– () {

numerator -= denominator;
return *this;
}

fraction fraction::operator– (int) {

fraction temp(numerator, denominator);
numerator -= denominator;
return temp;
}

void fraction::simplify() {

for (int i = denominator; i > 1; i–) {

if ((numerator % i == 0) && (denominator % i == 0)) {

numerator = numerator / i;
denominator = denominator / i;
}
}
if(denominator < 0) {

denominator *= -1;
numerator *= -1;
}
}
}
————————————————————————-
fraction.h
——————————————-
#include <iostream>
#include <cassert>
#include <cmath>

namespace cs_fraction {

class fraction {

//variables
private: int numerator;
int denominator;
void simplify();

//functions
public:
fraction(int inNumerator = 0, int inDenominator = 1);

friend std::ostream& operator<<(std::ostream& out, const fraction& inFraction);
friend std::istream& operator>>(std::istream& in, fraction& inFraction);

friend bool operator== (const fraction& inLeft, const fraction& inRight);
friend bool operator!= (const fraction& inLeft, const fraction& inRight);
friend bool operator> (const fraction& inLeft, const fraction& inRight);
friend bool operator>= (const fraction& inLeft, const fraction& inRight);
friend bool operator< (const fraction& inLeft, const fraction& inRight);
friend bool operator<= (const fraction& inLeft, const fraction& inRight);

friend fraction operator+ (const fraction& inLeft, const fraction& inRight);
friend fraction operator- (const fraction& inLeft, const fraction& inRight);
friend fraction operator* (const fraction& inLeft, const fraction& inRight);
friend fraction operator/ (const fraction& inLeft, const fraction& inRight);

fraction operator+= (const fraction& inFraction);
fraction operator-= (const fraction& inFraction);
fraction operator*= (const fraction& inFraction);
fraction operator/= (const fraction& inFraction);

fraction operator++ ();
fraction operator++ (int);
fraction operator– ();
fraction operator– (int);

};   //end class
} //end namespace
—————————————————————————————–
fraction.txt
———————————-
1/3
3/6
3072/4096
-4/5
12/2
5
-8
21/15
-50/3
1+1/4
1+5/5
-4+3/12
-10+10/12

Homework Ocean
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents