In: Computer Science
*********C++ Program************
This assignment is the continuation of project 4, with the modification and improvement with the following modifications:
7/3 + 1/3 = 8/3
7/3 - 1/3 = 2
7/3 * 1/3 = 7/9
7/3 / 1/3 = 7
7/3 is:
> 1/3 according to the overloaded > operator
>= 1/3 according to the overloaded < operator
Press any key to continue
******Fraction.h*********
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction {
// not
fully commented
public:
Fraction(int = 0, int = 1); // default
constructor
Fraction add(const Fraction&); //addition
function
Fraction subtract(const Fraction&); //subtraction
function
Fraction multiply(const Fraction&);
//multiplication function
Fraction division(const Fraction&); //division
function
void display(ostream& out) const; //display
function
void printFractionAsFloat(ostream& out) const;
//print the fraction as a float function
private:
int numerator;
int denominator;
void reduce();
// utility function, reduce
to lowest terms
};
#endif
*****Fraction.cpp*******
#include <cmath>
#include "Fraction.h"
using namespace std;
//------------------------------ Fraction
------------------------------------
// default constructor: parameters are numerator and denominator
respectively
// if the number is negative, the negative is always stored in the
numerator
Fraction::Fraction(int n, int d)
{
numerator = (d < 0 ? -n : n);
denominator = (d < 0 ? -d : d);
reduce();
}
//(a)--------------------------------- add
--------------------------------------
// overloaded +: addition of 2 Fractions, current object and
parameter
Fraction Fraction::add(const Fraction& a)
{
Fraction t;
t.numerator = a.numerator * denominator +
a.denominator * numerator;
t.denominator = a.denominator * denominator;
t.reduce();
return t;
}
//(b)------------------------------ subtract
------------------------------------
// subtraction of 2 Fractions, current object and parameter
//overload operator -
Fraction Fraction::subtract(const Fraction& a)
{
Fraction t;
int num = this->numerator; //the value of numerator
equals the num
int denom = this->denominator; //the value of
denominator equals the denom
int second_num = a.numerator; //second numerator
equals the second num
int second_denom = a.denominator; //the second
denominator equals the second denom
int overall_numerator = (num* second_denom - denom *
second_num); //equation for the numerator
int den = (denom * second_denom); //equation for the
denominator
t.numerator = overall_numerator; //setting the value
of the overall numerator to t.numerator
t.denominator = den; //setting the value of the den to
t.denominator
t.reduce(); //reduce the fration
return t; //return the fraction value
}
//Multpilication function that overrides the multiplication
function
Fraction Fraction::multiply(const Fraction& a)
{
Fraction t; //declared variable
t.numerator = a.numerator * numerator; //simple
equation for multiplication
t.denominator = a.denominator * denominator;
t.reduce();
return t;
}
//division function that overrides the / operator
Fraction Fraction::division(const Fraction& a)
{
Fraction t;
t.numerator = numerator * a.denominator;
t.denominator = a.numerator * denominator;
t.reduce();
return t;
}
//the display function the displays the fraction as numerator/denominator
void Fraction::display(ostream& out) const
{
out << numerator << "/" <<
denominator;
}
//the float function that turns the fration into a decimal
void Fraction::printFractionAsFloat(ostream& out)
const
{
if (denominator == 0)
{
out << "The fraction has a
denomintor of 0!";
}
else
out << float(numerator) /
float(denominator);
}
void Fraction::reduce()
{
int i;
i = abs(denominator * numerator); //set the value of
integer i to the denominator multiplied by the
//absolute value of numerator
while (i > 1) //will cntinue through the loop until
i is equal equal to or less than one
{
if ((denominator % i == 0)
&& (numerator % i == 0))
{
denominator /=
i;
numerator /=
i;
}
i--;
}
}
//--------- Frac2.h -----------
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction { // not fully commented
public:
Fraction(int = 0, int = 1); // default constructor
Fraction operator + (const Fraction&); //addition
function
Fraction operator - (const Fraction&); //subtraction
function
Fraction operator *(const Fraction&); //multiplication
function
Fraction operator /(const Fraction&); //division function
bool operator <(const Fraction& f);
bool operator >(const Fraction& f);
void printFractionAsFloat(ostream& out) const; //print the
fraction as a float function
friend ostream & operator << (ostream &out, const
Fraction &f);
private:
int numerator;
int denominator;
void reduce(); // utility function, reduce to lowest terms
};
#endif
//--------- Frac2.cpp -----------
#include <cmath>
#include "Frac2.h"
using namespace std;
//------------------------------ Fraction
------------------------------------
// default constructor: parameters are numerator and denominator
respectively
// if the number is negative, the negative is always stored in the
numerator
Fraction::Fraction(int n, int d)
{
numerator = (d < 0 ? -n : n);
denominator = (d < 0 ? -d : d);
reduce();
}
//(a)--------------------------------- add
--------------------------------------
// overloaded +: addition of 2 Fractions, current object and
parameter
Fraction Fraction:: operator +(const Fraction& a)
{
Fraction t;
t.numerator = a.numerator * denominator + a.denominator *
numerator;
t.denominator = a.denominator * denominator;
t.reduce();
return t;
}
//(b)------------------------------ subtract
------------------------------------
// subtraction of 2 Fractions, current object and parameter
//overload operator -
Fraction Fraction::operator -(const Fraction& a)
{
Fraction t;
int num = this->numerator; //the value of numerator equals the
num
int denom = this->denominator; //the value of denominator equals
the denom
int second_num = a.numerator; //second numerator equals the second
num
int second_denom = a.denominator; //the second denominator equals
the second denom
int overall_numerator = (num* second_denom - denom * second_num);
//equation for the numerator
int den = (denom * second_denom); //equation for the
denominator
t.numerator = overall_numerator; //setting the value of the overall
numerator to t.numerator
t.denominator = den; //setting the value of the den to
t.denominator
t.reduce(); //reduce the fration
return t; //return the fraction value
}
//Multpilication function that overrides the multiplication
function
Fraction Fraction::operator *(const Fraction& a)
{
Fraction t; //declared variable
t.numerator = a.numerator * numerator; //simple equation for
multiplication
t.denominator = a.denominator * denominator;
t.reduce();
return t;
}
//division function that overrides the / operator
Fraction Fraction::operator /(const Fraction& a)
{
Fraction t;
t.numerator = numerator * a.denominator;
t.denominator = a.numerator * denominator;
t.reduce();
return t;
}
//the float function that turns the fration into a decimal
void Fraction::printFractionAsFloat(ostream& out)
const
{
if (denominator == 0)
{
out << "The fraction has a denomintor of 0!";
}
else
out << float(numerator) / float(denominator);
}
void Fraction::reduce()
{
int i;
i = abs(denominator * numerator); //set the value of integer i to
the denominator multiplied by the
//absolute value of numerator
while (i > 1) //will cntinue through the loop until i is equal
equal to or less than one
{
if ((denominator % i == 0) && (numerator % i == 0))
{
denominator /= i;
numerator /= i;
}
i--;
}
}
bool Fraction::operator < (const Fraction &f)
{
float res = numerator/(float)denominator;
float res2 = f.numerator/(float)f.denominator;
if(res < res2)
{
return true;
}
return false;
}
bool Fraction::operator > (const Fraction &f)
{
float res = numerator/(float)denominator;
float res2 = f.numerator/(float)f.denominator;
if(res > res2)
{
return true;
}
return false;
}
//--------- lab5driver.cpp -----------
#include <iostream>
#include "Frac2.h"
using namespace std;
ostream & operator << (ostream & out, const Fraction
& f)
{
out << f.numerator;
if (f.denominator != 1)
{
out << "/" << f.denominator;
}
return out;
}
int
main ()
{
Fraction a (7, 3);
Fraction b (1, 3);
Fraction c;
cout << "Fraction A: " << a << endl;
cout << "Fraction B: " << b << endl;
c = a + b;
cout << "a + b = " << c << endl;
c = a - b;
cout << "a - b = " << c << endl;;
c = a * b;
cout << "a * b = " << c << endl;;
c = a / b;
cout << "a / b = " << c << endl;;
cout << "Is " << a << " < " << b
<< "?: ";
if (a < b)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
cout << "Is " << a << " > " << b
<< "?: ";
if (a > b)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}