Question

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

5) Modify the operator>> function so that after it reads the denominator it sets it to 1 if it is 0

6) Modify the main program so that it tests the 3 functions you created and the 2 functions you modified

#include "Fraction.h"

int main ()

{

Fraction f1,f2,f3;

cout<< "Enter a fraction in the form a/b ";

cin >> f1;

cout<< "Enter another fraction in the form a/b ";

cin >> f2;

if ( f1 == f2 )

cout << "\nThe two fractions are equal\n";

else cout <<"\nThe two fractions are different\n";

f3 = f1 + f2;

cout << f1 << " + " << f2 << " = " << f3;

return 0;

}

#include "Fraction.h"


Fraction :: Fraction()

{

numerator = 0;

denominator = 1;

}

ostream& operator<< ( ostream& ofile, const Fraction &f )

{

ofile<< f.numerator;

ofile<< " / ";

ofile<<f.denominator;

return ofile;

}

istream& operator>> ( istream &ifile, Fraction &f )

{

char ch;

ifile>> f.numerator;

ifile>> ch; // read and discard the symbol /

ifile>> f.denominator;

return ifile;

}

Fraction Fraction :: operator+ ( const Fraction &other)

{

Fraction temp;

temp.numerator = numerator*other.denominator +

denominator*other.numerator;

temp.denominator = denominator * other.denominator;

return temp;

}

bool Fraction :: operator== ( const Fraction &other )

{

return ( numerator*other.denominator ==

denominator*other.numerator);

}

#include <iostream>

using namespace std;

#ifndef FRACTION_H

#define FRACTION_H

class Fraction

{

friend ostream& operator<< (ostream&, const Fraction& );

friend istream& operator>> (istream &, Fraction &);

private:

int numerator;

int denominator;

public:

Fraction();

Fraction operator+ ( const Fraction& );

bool operator== ( const Fraction& );

};

#endif

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

In case of any query do comment. Please rate answer as well. Thanks

Code:

===========main.cpp==========

#include "Fraction.h"
int main ()
{
int num, den;
Fraction f1,f2,f3,f4,f5;
cout<< "Enter a fraction in the form a/b ";
cin >> f1;
cout<< "Enter another fraction in the form a/b ";
cin >> f2;
if ( f1 == f2 )
    cout << "\nThe two fractions are equal\n";
else 
    cout <<"\nThe two fractions are different\n";

if (f1<f2)
    cout << f1 << " is less than " << f2 << "\n";
else
    cout << f2 << " is less than " << f1 << "\n";

f3 = f1 + f2;
f4 = f1 * f2;
cout << f1 << " + " << f2 << " = " << f3 << "\n";
cout << f1 << " * " << f2 << " = " << f4 << "\n";
//using constructor with parameter
cout << "Using constructor with two parameter: \n";
cout << "Enter numerator: ";
cin >> num;
cout <<"Enter denomerator: ";
cin >> den;
f5 = Fraction(num,den);
cout << "Fraction constructor with two parameter is: " << f5;
return 0;
}


=============Fraction.cpp==========

#include "Fraction.h"

Fraction :: Fraction()
{
numerator = 0;
denominator = 1;
}

Fraction :: Fraction(int num, int den){
    numerator = num;
    if(den == 0)
        denominator = 1;
    else
        denominator = den;
}

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

istream& operator>> ( istream &ifile, Fraction &f )
{

char ch;
ifile>> f.numerator;
ifile>> ch; // read and discard the symbol /
ifile>> f.denominator;
if(f.denominator == 0)
    f.denominator = 1;
return ifile;
}

Fraction Fraction :: operator+ ( const Fraction &other)
{
    Fraction temp;
    temp.numerator = numerator*other.denominator + denominator*other.numerator;
    temp.denominator = denominator * other.denominator;    
    return temp;
}

bool Fraction :: operator== ( const Fraction &other )
{
    return ( numerator*other.denominator ==denominator*other.numerator);
}

bool Fraction :: operator< ( const Fraction &other )
{
    //make denominator same and then compare the difference of numerator  
    int difference = numerator * other.denominator - denominator * other.numerator;
    return difference < 0;
}

//function for multiplication
Fraction Fraction :: operator* ( const Fraction &other)
{
    Fraction temp;
    temp.numerator = numerator*other.numerator;
    temp.denominator = denominator * other.denominator;    
    return temp;
}

============Fraction.h===========

#include <iostream>
using namespace std;
#ifndef FRACTION_H
#define FRACTION_H
class Fraction
{
friend ostream& operator<< (ostream&, const Fraction& );
friend istream& operator>> (istream &, Fraction &);
private:
int numerator;
int denominator;
public:
Fraction();
Fraction(int numerator, int denominator); //constructor with two parameter
Fraction operator+ ( const Fraction& );
bool operator== ( const Fraction& );
bool operator< ( const Fraction&); //added for < operator
Fraction operator* ( const Fraction& ); //added for * operator
};
#endif

output:

input Enter a fraction in the form a/b 2/0 Enter another fraction in the form a/b 2/2 The two fractions are different 1 is le

Add a comment
Know the answer?
Add Answer to:
Please use C++ and add comments to make it easier to read. Do the following: 1)...
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
  • 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 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...

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

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

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

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

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    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 the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

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

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