Question

In: Computer Science

*********C++ Program************ This assignment is the continuation of project 4, with the modification and improvement with...

*********C++ Program************

This assignment is the continuation of project 4, with the modification and improvement with the following modifications:

  • In Frac.h, replace the methods addition (const Fraction & ), subtraction( const Fraction& ), multiply( const Fraction & ), divide( const Fraction & ), void printFraction() by overloading operators +, -, *, /, << respectively.
  • Add overloading operators > and < so that they can compare two Fractions.
  • Write your own implementation file which is named Frac2.cpp to replace Frac.cpp and implement the Fraction class and the necessary overloading operators.
  • Download the driver lab5driver.cpp to test your codes. If your codes in files Frac2.h and Frac2.cpp are correct, the standard output after the execution of your program should like the following:

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

}

Solutions

Expert Solution

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


Related Solutions

In this assignment, you will select a program, quality improvement initiative, or other project from your...
In this assignment, you will select a program, quality improvement initiative, or other project from your place of employment. Assume you are presenting this program to the board for approval of funding. Write an executive summary (850-1,000 words) to present to the board, from which they will make their decision to fund your program or project. The summary should include: 1. The purpose of the program or project. 2. The target population or audience. 3. The benefits of the program...
Cookie Creations (Chapter 4) This assignment is a continuation of the Cookie Creations case study from...
Cookie Creations (Chapter 4) This assignment is a continuation of the Cookie Creations case study from Chapters 1–3. You will use the information from the previous chapters and from this chapter to complete the actions for the case study and to apply what you have learned to this point. The Cookie Creations case study for Chapter 4 can be found below and also on pp. 4-51 to 4-52 in the textbook. The textbook provides you with the adjusted trial balance,...
This writing assignment will involve implementing and analyzing a behavior modification program on yourself. The behavior...
This writing assignment will involve implementing and analyzing a behavior modification program on yourself. The behavior is ANXIETY OF MEETING NEW PEOPLE. This assignment is to develop your own functional behavior assessment. A minimum of 3 paragraphs per question is required. Modification Plan: In this section, you need to describe your assessment and intervention plan. How will you collect data on the behavior in order to evaluate whether or not it is changing? Describe any forms or graphs you plan...
Description This assignment is a modification of the last assignment. In this assignment, you will input...
Description This assignment is a modification of the last assignment. In this assignment, you will input from a file called in.txt and output to a file called out.txt. Otherwise, the assignment will behave the same way as the last assignment. For doing this assignment, the statement doing input and output will be changed. Otherwise, the code will mostly remain unchanged. Creating Text IO Files In Eclipse, using the file menu, create the input file in.txt in project folder (not in...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
ACC 3010 Project 2 This project is a continuation of Project 1, KTZFIG Consulting Inc.   An...
ACC 3010 Project 2 This project is a continuation of Project 1, KTZFIG Consulting Inc.   An additional 11 months have passed since Project 1 (we are now at June 30, 2019 the company’s year end). The friends have expanded the shop to include sales of gidgits as well as the consulting services activities. The new company name is KTZFIG Consulting & Sales Inc. All transactions for the company through the end of the year have been posted to the accounts...
This assignment involves the use of text files, lists, and exception handling and is a continuation...
This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>> Enter gender (boy/girl): boy...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Select one improvement program that you have observed in an organization. The program of improvement may...
Select one improvement program that you have observed in an organization. The program of improvement may focus on quality, safety, absenteeism, costs, or other matters. With respect to implementation the program: A) What where its strength? B) What were the weaknesses in implementation? C) What recommendations would you make to implement an improvement program in the future?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT