C++ CODE
/* This is program project 2 on page 695.
* Before you begin the project, please read the project description
* on page 695 first.
*
* Author: Your Name
* Version: Dates
*/
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
class Fraction
{
public:
// constructor
Fraction(int a, int b);
// generate a fraction which is a/b
Fraction(int a);
// generate a fraction which is a/1
Fraction();
// generate a fraction which is 0/1. i.e 0
// member functions
int get_numerator() const;
// return the numerator of the fraction
int get_denominator() const;
// return the denominator of the fraction
void reduce();
// reduce this fraction to simplest form. For instance,
// 2/4 will be reduced to 1/2
Fraction reciprocal() const;
// return the reciprocal of this Fraction
// friend functions
friend Fraction operator +(const Fraction& f1, const Fraction& f2);
// return the sum of f1 and f2,
// the result is reduced
friend Fraction operator -(const Fraction& f1, const Fraction& f2);
// return the difference of f1 and f2,
// the result is reduced
friend Fraction operator *(const Fraction& f1, const Fraction& f2);
// return the product of f1 and f2,
// the result is reduced
friend Fraction operator /(const Fraction& f1, const Fraction& f2);
// return the quotient of f1 and f2,
// the result is reduced
friend Fraction operator -(const Fraction& f);
// return the negation of f
friend bool operator < (const Fraction& f1, const Fraction& f2);
// return true if f1 is less than f2.
// False otherwise
friend bool operator > (const Fraction& f1, const Fraction& f2);
// return true if f1 is greater than f2.
// False otherwise
friend bool operator <= (const Fraction& f1, const Fraction& f2);
// return true if f1 is less or equal to f2.
// False otherwise
friend bool operator >= (const Fraction& f1, const Fraction& f2);
// return true if f1 is greater or equal to f2.
// False otherwise
friend bool operator == (const Fraction& f1, const Fraction& f2);
// return true if f1 is equal to f2.
// False otherwise
friend bool operator != (const Fraction& f1, const Fraction& f2);
// return true if f1 is not equal to f2.
// False otherwise
friend istream& operator >> (istream& in, Fraction& f);
// input f in the form of a/b, where b cannot be zero. Also,
// if b is negative, the Fraction will change b to be positive.
// So, again, 1/-3 will be changed to -1/3
friend ostream& operator << (ostream& out, Fraction& f);
// output a Fraction f in form of a/b
private:
int num; // numerator of the fraction
int den; // denominator of the fraction
int gcd();
// A prvate function that is used to find the gcd of numerator
// and denominator by using Euclidean Algorithm
};
// all following test functions are given
double test1();
// test all constructors and two get methods.
// All these functions worth 1.5 points
double test2();
// test neg, reduce, and reciprocal functions.
// All these functions worth 1.5 points
double test3();
// test add, sub, mul, and div functions.
// All these functions worth 3 points
double test4();
// test less, greater, equal, less_or_equal, greater_or_equal,
// not_equal. All these functions worth 2 points
double test5();
// test input and output function. This two functions worth 1 points
int main()
{
double totalScore = 0.0;
cout <<"Let's see your grade. \n\n";
system("pause");
cout << endl;
// Test constructors, and get functions
totalScore += test1();
cout << "Your points so far is " << totalScore << "\n\n";
system("pause");
cout << endl;
// Test neg, reciprocal, and reduce functions
totalScore += test2();
cout << "Your points so far is " << totalScore << "\n\n";
system("pause");
cout << endl;
// Test +, -, *, / operators
totalScore += test3();
cout << "Your points so far is " << totalScore << "\n\n";
system("pause");
cout << endl;
// Test all >, <, >=, <=, ==, != operators
totalScore += test4();
cout << "Your points so far is " << totalScore << "\n\n";
system("pause");
cout << endl;
// Test >> and << operators
totalScore += test5();
cout << "Your points so far is " << totalScore << "\n\n";
system("pause");
cout << endl;
cout << "If you turn in your program to Dr. Zeng now, your will get " << totalScore << " out of 9\n";
cout << "Dr. Zeng will read your program and decide if you will get 1 more point for program style\n";
system("pause");
return 0;
}
// implement all class member functions here, gcd function is given
int Fraction::gcd()
{
if(num == 0) return 1;
int a = max(abs(num), abs(den));
int b = min(abs(num), abs(den));
while(b != 0)
{
int result = a%b;
a = b;
b = result;
}
return a;
}
// test all constructors and two get methods.
// All these functions worth 1 points
double test1()
{
double result = 1.5;
// Test no argument constructor
Fraction f1; // the fraction should be 0/1
if(f1.get_numerator() != 0 || f1.get_denominator() != 1)
{
cout << "The constructor with no argument was wrong.\n";
result -= 0.5;
}
// Test one argument constructor
Fraction f2(2); // the fraction should shoule be 2/1
if(f2.get_numerator() != 2 || f2.get_denominator() != 1)
{
cout << "The constructor with one argument was wrong.\n";
result -= 0.5;
}
// Test two arguments constructors
Fraction f3(3, -4); // the fraction should be -3/4
if(f3.get_numerator() != -3 || f3.get_denominator() != 4)
{
cout << "The constructor with two argument was wrong. Make sure that the denomunator always positive.\n";
result -= 0.5;
}
// finish test
if(result < 1.5 && result > 0)
cout << "Test 1 partially failed. You got " << result << " more points\n";
else if(result > 1.4)
cout << "Test 1 fully passed. You got 1.5 more points\n";
else
cout << "Test 1 completely failed. You may check your constructors and get methods\n";
return result;
}
// test neg, reduce, and reciprocal functions.
// All these functions worth 1.5 points
double test2()
{
double result = 1.5;
Fraction f1(12, -15), f2(2, 3), f3, f4;
// Test neg
f3 = -f1; // f3 is 12/15
f4 = -f2; // f4 is -2/3
if(f3.get_numerator() != 12 || f3.get_denominator() != 15
|| f4.get_numerator() != -2 || f4.get_denominator() != 3)
{
cout <<"The negation function was wrong.\n";
result -= 0.5;
}
// Test reciprocal
f3 = f1.reciprocal(); // f3 is -15/12
f4 = f2.reciprocal(); // f4 is 3/2
if(f3.get_numerator() != -15 || f3.get_denominator() != 12
|| f4.get_numerator() != 3 || f4.get_denominator() != 2)
{
cout <<"The reciprocal function was wrong.\n";
result -= 0.5;
}
// Test reduce
f1.reduce(); // f1 is -4/5
f2.reduce(); // f2 is 2/3
if(f1.get_numerator() != -4 || f1.get_denominator() != 5 || f2. get_numerator() != 2 || f2.get_denominator() != 3)
{
cout <<"The reduce function was wrong.\n";
result -= 0.5;
}
// finish test
if(result < 1.5 && result > 0)
cout << "Test 2 partially failed. You got " << result << " more points\n";
else if(result > 1.4)
cout << "Test 2 fully passed. You got 1.5 more points\n";
else
cout << "Test 2 completely failed. \n";
return result;
}
// test +, -, *, and / operators.
// All these functions worth 3 points
double test3()
{
Fraction f1(-1, 2), f2(3, 5), f;
double result = 3.0;
// Test +
f = f1 + f2; // f is -1/2+3/5 = 1/10
if(f.get_numerator() != 1 || f.get_denominator() != 10)
{
cout <<"The add function was wrong.\n";
result -= 1.0;
}
// Test subtraction
f = f1 - f2; // f is -1/2 - 3/5 = -11/10
if(f.get_numerator() != -11 || f.get_denominator() != 10)
{
cout <<"The sub function was wrong.\n";
result -= 0.5;
}
// Test multiplication
f = f1 * f2; // f is -3/10
if(f.get_numerator() != -3 || f.get_denominator() != 10)
{
cout <<"The mul function was wrong.\n";
result -= 1.0;
}
// Test division
f = f1 / f2; // f is -5/6
if(f.get_numerator() != -5 || f.get_denominator() != 6)
{
cout <<"The div function was wrong.\n";
result -= 0.5;
}
// finish test
if(result < 3.0 && result > 0)
cout << "Test 3 partially failed. You got " << result << " more points\n";
else if(result > 2.9)
cout << "Test 3 fully passed. You got 3.0 more points\n";
else
cout << "Test 3 completely failed. \n";
return result;
}
// test less, greater, equal, less_or_equal, greater_or_equal,
// not_equal. All these functions worth 2 points
double test4()
{
Fraction f1(-1, 2), f2(3, 5), f3(2, -4);
double result = 2.0;
// Test <
if(!(f1 < f2) || f1 < f3 || f2 < f3)
{
cout <<"The < operator was wrong.\n";
result -= 0.5;
}
// Test >
if(f1 > f2 || !(f2 > f1) || f1 > f3)
{
cout <<"The > operator was wrong.\n";
result -= 0.5;
}
// Test ==
if(f1 == f2 || !(f1 == f3) || f2 == f3)
{
cout <<"The equal function was wrong.\n";
result -= 0.5;
}
// Test <=
if(!(f1 <= f2) || !(f1 <= f3) || f2 <= f3)
{
cout <<"The less_or_equal function was wrong.\n";
result -= 0.5;
}
// Test >=
if(f1 >= f2 || !(f1 >= f3) || !(f2 >= f3))
{
cout <<"The greater_or_equal function was wrong.\n";
result -= 0.5;
}
// Test !=
if(!(f1 != f2) || f1 != f3 || !(f2 != f3))
{
cout <<"The not_equal function was wrong.\n";
result -= 0.5;
}
// finish test
if(result < 2.0 && result > 0)
cout << "Test 4 partially failed. You got " << result << " more points\n";
else if(result > 1.9)
cout << "Test 4 fully passed. You got 2.0 more points\n";
else
cout << "Test 4 completely failed. \n";
return max(result, 0.0); // the student will not get negative points for any test
}
// test input and output function. This two functions worth 1 points
double test5()
{
double result = 1.0;
Fraction f;
cout << "This test has to be interactive, please follow the instruction\n";
cout << "Enter a fraction exactly as 3/-4:";
cin >> f;
if(f.get_denominator() != 4 || f.get_numerator() != -3)
{
cout << "The input function is not correct\n";
result -= 0.5;
}
Fraction f1(3, -4);
cout << f1;
cout << "\nDoes the previous line show you -3/4? Y or N: ";
char choice;
cin >> choice;
if(choice != 'y' && choice != 'Y')
{
cout << "The output function is not correct\n";
result -= 0.5;
}
// finish test
if(result < 1.0 && result > 0)
cout << "Test 5 partially failed. You got " << result << " more points\n";
else if(result > 0.9)
cout << "Test 5 fully passed. You got 1.0 more points\n";
else
cout << "Test 5 completely failed. \n";
return result;
}

#include <iostream>
using namespace std;
class Rational
{
public:
Rational();
Rational(int numer, int denom);
// Setter and getter methods
void setNumerator(int);
void setDenominator(int);
int getNumerator() const;
int getDenominator() const;
Rational operator+(const Rational& f) const; //
operator+()
Rational operator-(const Rational& f) const; //
operator-()
Rational operator*(const Rational& f) const; //
operator*()
Rational operator/(const Rational& f) const; //
operator*()
bool operator!=(const Rational& f) const; // operator!=()
bool operator==(const Rational& f) const; // operator==()
bool operator<=(const Rational& f) const; //
operator<=()
bool operator>=(const Rational& f) const; //
operator>=()
bool operator<(const Rational& f) const; //
operator<()
bool operator>(const Rational& f) const; //
operator>()
/**
* Friend function declarations
* Output Rational number to an output stream, Example 3+4i
*/
friend std::ostream& operator<<(std::ostream& dout,
const Rational&);
/**
* read Rational number to an input stream
*/
friend std::istream& operator>>(std::istream& din,
Rational&);
private:
int numer;
int denom;
};
Rational::Rational()
{
this->numer = 0;
this->denom = 1;
}
Rational::Rational(int num, int den)
{
numer = num;
denom = den;
}
// Setters and getters
void Rational::setNumerator(int n)
{
this->numer = n;
}
void Rational::setDenominator(int n)
{
this->denom = n;
}
int Rational::getNumerator() const
{
return numer;
}
int Rational::getDenominator() const
{
return denom;
}
Rational Rational::operator+(const Rational& f) const
{
Rational frac;
int a = numer;
int b = denom;
int c = f.numer;
int d = f.denom;
int sumnumer = (a * d + b * c);
int denom = (b * d);
frac.setNumerator(sumnumer);
frac.setDenominator(denom);
return frac;
}
Rational Rational::operator-(const Rational& f) const
{
Rational frac;
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
int subnumer = (a * d - b * c);
int denom = (b * d);
frac.setNumerator(subnumer);
frac.setDenominator(denom);
return frac;
}
Rational Rational::operator*(const Rational& f) const
{
Rational frac;
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
int mulnumer = (a * c);
int muldenom = (b * d);
frac.setNumerator(mulnumer);
frac.setDenominator(muldenom);
return frac;
}
Rational Rational::operator/(const Rational& f) const
{
Rational frac;
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
int divnumer = (a * d);
int divdenom = (c * b);
frac.setNumerator(divnumer);
frac.setDenominator(divdenom);
return frac;
}
/*
void Rational::print()const
{
cout<<getNumerator()<<"/"<<getDenominator();
}
*/
bool Rational::operator<=(const Rational& f) const
{
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;
}
bool Rational::operator>=(const Rational& f) const
{
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;
}
bool Rational::operator<(const Rational& f) const
{
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;
}
bool Rational::operator>(const Rational& f) const
{
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;
}
// Function implementation which display Rational numbers by using
the operator overloading '<<'
ostream& operator<<(ostream& dout, const
Rational& c)
{
dout << c.numer << "/" << c.denom;
return dout;
}
// Function implementation which read Rational numbers by using the
operator overloading '>>'
istream& operator>>(istream& din, Rational&
c)
{
char ch;
din >> c.numer;
din >> ch;
din >> c.denom;
return din;
}
bool Rational::operator!=(const Rational& f) const
{
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;
}
bool Rational::operator==(const Rational& f) const
{
int a = this->numer;
int b = this->denom;
int c = f.numer;
int d = f.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;
}
int main()
{
Rational f1, f2, result;
// int tmpN, tmpD;
// char divideSign;
// Rational 1
cout << "Enter Rational 1:
<numerator>/<denominator>" << endl;
cin >> f1;
// cin >> tmpN >> divideSign >> tmpD;
// f1.setNumerator( tmpN );
// f1.setDenominator( tmpD );
// Rational 2
cout << "Enter Rational 2:
<numerator>/<denominator>" << endl;
// cin >> tmpN >> divideSign >> tmpD;
// f2.setNumerator( tmpN );
// f2.setDenominator( tmpD );
cin >> f2;
cout<<endl;
result = f1 + f2;
cout << f1;
cout << "+";
cout << f2;
cout << "=";
cout << result;
cout << endl;
result = f1 - f2;
cout << f1;
cout << "-";
cout << f2;
cout << "=";
cout << result;
cout << endl;
result = f1 * f2;
cout << f1;
cout << "*";
cout << f2;
cout << "=";
cout << result;
cout << endl;
result = f1 / f2;
cout << f1;
cout << "/";
cout << f2;
cout << "=";
cout << result;
cout << endl;
bool boolean = f1 < f2;
if (boolean)
cout << f1 << " is less than " << f2 <<
endl;
else
cout << f1 << " is not less than " << f2 <<
endl;
boolean = f1 > f2;
if (boolean)
cout << f1 << " is greater than " << f2 <<
endl;
else
cout << f1 << " is not greater than " << f2
<< endl;
boolean = f1 <= f2;
if (boolean)
cout << f1 << " is less than or equal to " << f2
<< endl;
else
cout << f1 << " is not less than or equal to " <<
f2 << endl;
boolean = f1 >= f2;
if (boolean)
cout << f1 << " is greater than or equal to " <<
f2 << endl;
else
cout << f1 << " is not greater than or equal to "
<< f2 << endl;
boolean = f1 == f2;
if (boolean)
cout << f1 << " is equal to " << f2 <<
endl;
else
cout << f1 << " is not equal to " << f2 <<
endl;
boolean = f1 != f2;
if (boolean)
cout << f1 << " is not equal to " << f2 <<
endl;
else
cout << f1 << " is equal to " << f2 <<
endl;
return 0;
}
_________________________
Output:

___________________Thank You
C++ CODE /* This is program project 2 on page 695. * Before you begin the...
C++ CODE
/* This is program project 2 on page 695.
* Before you begin the project, please read the project description
* on page 695 first.
*
* Author: Your Name
* Version: Dates
*/
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
class Fraction
{
public:
// constructor
Fraction(int a, int b);
// generate a fraction which is a/b
Fraction(int a);
// generate a fraction which is a/1
Fraction();
// generate a fraction which is 0/1. i.e...
Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...
Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...
Please zoom in so the pictures become high resolution. I need
three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The
programming language is C++. I will provide the Sample Test Driver
Code as well as the codes given so far in text below.
Sample Testing Driver Code:
cs52::FlashDrive empty;
cs52::FlashDrive drive1(10, 0, false);
cs52::FlashDrive drive2(20, 0, false);
drive1.plugIn();
drive1.formatDrive();
drive1.writeData(5);
drive1.pullOut();
drive2.plugIn();
drive2.formatDrive();
drive2.writeData(2);
drive2.pullOut();
cs52::FlashDrive combined = drive1 + drive2;
// std::cout << "this drive's filled to " << combined.getUsed( )...
In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...
You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...
Hello, im looking for help on adding overload operators to my code. It needs to consist of + (addition) for the Fraction class * (multiplication), no need not reduce == (equality operator) = (assignment operator) << and >> so in main you can use << and >> as follows: cin>>frac1; cout<< frac1; where frac1 is a an object of the Fraction class the original code is: #include <iostream> using namespace std; class Fractions { private: int numerator; int denominator;...
C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...
13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...