Question

If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only!

In Frac.h, replace the methodes addition(const Fraction &),subtraction(const Fraction&), multiply(const Fraction &); divide(c

Frac.h:

// a Fraction object holds one Fraction number, one fraction
#ifndef FRAC_H
#define FRAC_H
#include <iostream>
using namespace std;

//Creaing a Fraction class
class Fraction {
public:
Fraction(int = 0, int = 1);
// Function Declarations which performs operations on Fraction class
Fraction add(const Fraction &);
Fraction subtract(const Fraction& a);
Fraction multiply(const Fraction& a);
Fraction divide(const Fraction& a);
//This function will display the Fraction
void printFraction();
//This function will display the Fraction in terms of decimal
void printFractionAsFloat();
private:
//Declaring variables
int numerator;
int denominator;
//Function will reduce Fraction to Lowest terms
void reduce();
};
#endif

Frac.cpp:

#include <iostream>
using namespace std;
#include "Frac.h"

/* This is a Parameterized constructor
* which is used to initialize instance variables.
*/
Fraction::Fraction(int n, int d) {
numerator = (d < 0 ? -n : n);
denominator = (d < 0 ? -d : d);
reduce();
}

/*
* This function will add Two Fraction class objects and
* return the result as Fraction in lowest Terms.
*/
Fraction Fraction::add(const Fraction& a) {
Fraction t;
//Setting the numerator to Fraction class
t.numerator = a.numerator * denominator + a.denominator * numerator;
//Setting the denominator to Fraction class
t.denominator = a.denominator * denominator;
t.reduce();
return t;
}
/*
* This function will subtract Two Fraction class objects and
* return the result as Fraction in lowest Terms.
*/
Fraction Fraction::subtract(const Fraction& a)
{

//Getting numerator and Denominator of Current class
int e=this->numerator;
int f=this->denominator;
//Getting numerator and Denominator of Parameter Fraction class
int c=a.numerator;
int d=a.denominator;
//Setting the numerator to Fraction class
int subnumer=(e*d - f*c);
//Setting the denominator to Fraction class
int denom=(f*d);
Fraction frac(subnumer,denom);
return frac;
}

/*
* This function will multiply Two Fraction class objects and
* return the result as Fraction in lowest Terms.
*/
Fraction Fraction::multiply(const Fraction& a)
{
Fraction t;
//Setting the numerator to Fraction class
t.numerator = a.numerator * numerator;
//Setting the denominator to Fraction class
t.denominator = a.denominator * denominator;
t.reduce();
return t;
}
/*
* This function will divide Two Fraction class objects and
* return the result as Fraction in lowest Terms.
*/
Fraction Fraction::divide(const Fraction& a)
{
Fraction t;
//Setting the numerator to Fraction class
t.numerator = numerator * a.denominator;
//Setting the denominator to Fraction class
t.denominator = a.numerator * denominator;
t.reduce();
return t;
}
/*
* This function will display the fraction class object
*/
void Fraction::printFraction()
{
cout<<numerator<<"/"<<denominator;
}
/*
* This function will display the Fraction as in decimal values
*/
void Fraction::printFractionAsFloat() {
if (denominator == 0)
cout << endl << "DIVIDE BY ZERO ERROR!!!" << endl;
else
cout << float(numerator) / float(denominator);
}
/*
* This function will reduce the Fraction to lowest terms
*/
void Fraction::reduce() {
int n = numerator < 0 ? -numerator : numerator;
int d = denominator;
int largest = n > d ? n : d;
int gcd = 0; // greatest common divisor

//This loop will find the GCD of two numbers
for (int loop = largest; loop >= 2; loop--)
if (numerator % loop == 0 && denominator % loop == 0) {
gcd = loop;
break;
}

//Dividing the Fraction class Numerator and Denominator with GCD to reduce to lowest terms
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}

Frac2Driver.cpp (DON'T CHANGE):

// driver for Fraction project part 2

#include "Frac2.h"

int main()

{

Fraction c( 7, 3 ), d( 3, 9 ), x;

// c.printFraction();

cout << c;

cout << " + " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c + d;

// x.printFraction();

cout << x;

cout << '\n';

// c.printFraction();

cout <<c;

cout << " - " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c - d;

// x.printFraction();

cout <<x;

cout << '\n';

// c.printFraction();

cout << c;

cout << " * " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c * d;

// x.printFraction();

cout <<x;

cout << '\n';

// c.printFraction();

cout <<c;

cout << " / " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c / d;

// x.printFraction();

cout << x;

cout << '\n';

// c.printFraction();

cout << c;

cout << " is:\n";

cout << ( ( c > d ) ? " > " : " <= " );

// d.printFraction();

cout << d;

cout << " according to the overloaded > operator\n";

cout << ( ( c < d ) ? " < " : " >= " );

// d.printFraction();

cout << d;

cout << " according to the overloaded < operator\n";

return 0;

}

Please add comments!! And please give NEW solution, I've seen the others! If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

Frac.h

// a Fraction object holds one Fraction number, one fraction

#ifndef FRAC_H

#define FRAC_H

#include <iostream>

using namespace std;

class Fraction { // not fully commented

public:

Fraction(int = 0, int = 1); // default constructor

Fraction operator+(const Fraction &);

Fraction operator-(const Fraction& a);
Fraction operator*(const Fraction& a);
Fraction operator/(const Fraction& a);

friend ostream & operator << (ostream & cout, Fraction right);
bool operator<(const Fraction& f) const; // operator<()
bool operator>(const Fraction& f) const; // operator>()

private:

int numerator;

int denominator;

void reduce(); // utility function, reduce to lowest terms

};

#endif

________________

Frac.cpp


#include <iostream>
using namespace std;
#include "Frac.h"

//------------------------------ 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
Fraction Fraction::operator -(const Fraction& a)
{


int e=this->numerator;
int f=this->denominator;
int c=a.numerator;
int d=a.denominator;
int subnumer=(e*d - f*c);
int denom=(f*d);
Fraction frac(subnumer,denom);
return frac;
}


//(c)------------------------------ multiply ------------------------------------

// multiplication of 2 Fractions, current object and parameter
Fraction Fraction::operator *(const Fraction& a)
{
Fraction t;
  
t.numerator = a.numerator * numerator;

t.denominator = a.denominator * denominator;

t.reduce();

return t;

}

//(d)-------------------------------- divide ------------------------------------

// division of 2 Fractions, current object and parameter,

// division by zero crashes
Fraction Fraction::operator /(const Fraction& a)
{

Fraction t;
  
t.numerator = numerator * a.denominator;

t.denominator = a.numerator * denominator;

t.reduce();

return t;
}


//-------------------------------- reduce ------------------------------------

// reduce fraction to lowest terms

void Fraction::reduce() {

int n = numerator < 0 ? -numerator : numerator;

int d = denominator;

int largest = n > d ? n : d;

int gcd = 0; // greatest common divisor

for (int loop = largest; loop >= 2; loop--)

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

gcd = loop;

break;

}

if (gcd != 0) {

numerator /= gcd;

denominator /= gcd;

}

}

ostream & operator << (ostream & cout, Fraction right) {
if(right.denominator==1)
cout << right.numerator;
else
cout << right.numerator<< "/" << right.denominator ;
return cout;
}

bool Fraction::operator<(const Fraction& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
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 Fraction::operator>(const Fraction& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
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;
}

___________________

main.cpp

#include <iostream>
using namespace std;
#include "Frac.h"

int main(int argc, char **argv)
{
Fraction c( 7, 3 ), d( 3, 9 ), x;

// c.printFraction();

cout << c;

cout << " + " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c + d;

// x.printFraction();

cout << x;

cout << '\n';

// c.printFraction();

cout <<c;

cout << " - " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c - d;

// x.printFraction();

cout <<x;

cout << '\n';

// c.printFraction();

cout << c;

cout << " * " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c * d;

// x.printFraction();

cout <<x;

cout << '\n';

// c.printFraction();

cout <<c;

cout << " / " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c / d;

// x.printFraction();

cout << x;

cout << '\n';

// c.printFraction();

cout << c;

cout << " is:\n";

cout << ( ( c > d ) ? " > " : " <= " );

// d.printFraction();

cout << d;

cout << " according to the overloaded > operator\n";

cout << ( ( c < d ) ? " < " : " >= " );

// d.printFraction();

cout << d;

cout << " according to the overloaded < operator\n";

return 0;
}

_________________

Output:

/FracOperatorOverload LessGreater /31/32 73 is: 1/3 according to the overloaded > operator 1/3 according to the overloaded <

__________Thank You

Add a comment
Know the answer?
Add Answer to:
If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    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...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    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...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I 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...

  • Having to repost this as the last answer wasn't functional. Please help In the following program...

    Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    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...

    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...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...

    c++ format Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Science Linux systems. If your code does not compile on CS machines you will get 0 for the assignment. Organize your files into folders by lab assignment. For this assignment, create a folder called Lab9. A. Read Chapter 18 B. Create a new class called RationalNumber to represent fractions. The class should have the following capabilities: 1) It should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT