Question

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 the lowest terms.
public:
fraction(const int & = 0, const int & = 1);
// The numerator and the denominator
// fraction(5) = 5/1 = 5 :)
fraction(const fraction &);
// copy constructor

void operator=(const fraction &);

// You must know the meaning of +-*/, don't you ?
fraction operator+(const fraction &) const;
fraction operator-(const fraction &) const;
fraction operator*(const fraction &) const;
fraction operator/(const fraction &) const;

void operator+=(const fraction &);
void operator-=(const fraction &);
void operator*=(const fraction &);
void operator/=(const fraction &);

// Comparison operators
bool operator==(const fraction &) const;
bool operator!=(const fraction &) const;
bool operator<(const fraction &) const;
bool operator>(const fraction &) const;
bool operator<=(const fraction &) const;
bool operator>=(const fraction &) const;

friend std::istream & operator>>(std::istream &, fraction &);
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
friend std::ostream & operator<<(std::ostream &, const fraction &);
// Normally you should output "a/b" without any space and LF
// Sometims you may output a single integer (Why? Guess XD)
// If it is not a number (den = 0), output "NaN"
};

#endif

//main.cpp

#include "fraction.h"

void print(const bool & f) {
if (f)
std::cout << "True" << std::endl;
else
std::cout << "False" << std::endl;
}

int main() {
fraction f1, f2;
std::cin >> f1 >> f2;
std::cout << f1 + f2 << ' ' << f1 - f2 << ' '
<< f1 * f2 << ' ' << f1 / f2 << std::endl;
f1 += f2;
f1 -= f2;
f1 *= f2;
f1 /= f2;
std::cout << f1 << std::endl;
print(f1 == f2);
print(f1 != f2);
print(f1 < f2);
print(f1 > f2);
print(f1 <= f2);
print(f1 >= f2);
return 0;
}

//fraction.cpp

need to write.THNX!

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
_________________

#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 the lowest terms.
public:
fraction(const int & = 0, const int & = 1);
// The numerator and the denominator
// fraction(5) = 5/1 = 5 :)
fraction(const fraction &);
// copy constructor

void operator=(const fraction &);

// You must know the meaning of +-*/, don't you ?
fraction operator+(const fraction &) const;
fraction operator-(const fraction &) const;
fraction operator*(const fraction &) const;
fraction operator/(const fraction &) const;

void operator+=(const fraction &);
void operator-=(const fraction &);
void operator*=(const fraction &);
void operator/=(const fraction &);

// Comparison operators
bool operator==(const fraction &) const;
bool operator!=(const fraction &) const;
bool operator<(const fraction &) const;
bool operator>(const fraction &) const;
bool operator<=(const fraction &) const;
bool operator>=(const fraction &) const;

friend std::istream & operator>>(std::istream &, fraction &);
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
friend std::ostream & operator<<(std::ostream &, const fraction &);
// Normally you should output "a/b" without any space and LF
// Sometims you may output a single integer (Why? Guess XD)
// If it is not a number (den = 0), output "NaN"
};

#endif

____________________________

// fraction.cpp

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


int fraction::gcd(const int &a, const int &b) const
{
int divi = (a > b ? a : b);
int div = (a < b ? a : b);
int rem = divi % div;
while (rem != 0)
{
divi = div;
div = rem;
rem = divi % div;
}
return div;

}
// If you don't need this method, just ignore it.
void fraction::simp()
{
int n = _numerator < 0 ? -_numerator : _numerator;
int d = _denominator;
int largest = n > d ? n : d;
int gcdi = 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) {
gcdi = loop;
break;
}
//Dividing the Fraction class Numerator and Denominator with GCD to reduce to lowest terms
if (gcdi != 0) {
_numerator /= gcdi;
_denominator /= gcdi;
}

}
// To get the lowest terms.

fraction::fraction(const int &num, const int &den)
{
int n,d;
n=num;
d=den;
_numerator = (d < 0 ? -n : n);
_denominator = (d < 0 ? -d : d);
simp();

}
// The numerator and the denominator
// fraction(5) = 5/1 = 5 :)
fraction::fraction(const fraction& f)
{
this->_numerator=f._numerator;
this->_denominator=f._denominator;
}
// copy constructor

void fraction::operator=(const fraction& a)
{
this->_numerator=a._numerator;
this->_denominator=a._denominator;
}

// You must know the meaning of +-*/, don't you ?
fraction fraction::operator+(const fraction& a) const
{
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.simp();
return t;

}
fraction fraction::operator-(const fraction& a) const
{
//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);
frac.simp();
return frac;

}
fraction fraction::operator*(const fraction& a) const
{
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.simp();
return t;

}
fraction fraction::operator/(const fraction& a) const
{
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.simp();
return t;

}

void fraction::operator+=(const fraction& a)
{
this->_numerator=a._numerator * _denominator + a._denominator * _numerator;
this->_denominator= a._denominator * _denominator;
}
void fraction::operator-=(const fraction& a)
{
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
this->_numerator=(e*d - f*c);
//Setting the denominator to Fraction class
this->_denominator=(f*d);
  
}
void fraction::operator*=(const fraction& a)
{
//Setting the numerator to Fraction class
this->_numerator = a._numerator * _numerator;
//Setting the denominator to Fraction class
this->_denominator = a._denominator * _denominator;
}
void fraction::operator/=(const fraction &a)
{
//Setting the numerator to Fraction class
this->_numerator = _numerator * a._denominator;
//Setting the denominator to Fraction class
this->_denominator = a._numerator * _denominator;
}

// Comparison operators
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 == n2) && (d1 == 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;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != 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;
// 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;

}
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;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;

  
}

std::istream & operator>>(std::istream &din, fraction &c)
{

char ch;
din >> c._numerator;
din >> ch;
din >> c._denominator;
return din;
}
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
std::ostream & operator<<(std::ostream &dout, const fraction &c)
{
dout << c._numerator << "/" << c._denominator;
return dout;
  
}

_____________________________

// main.cpp

#include "fraction.h"

void print(const bool & f) {
if (f)
std::cout << "True" << std::endl;
else
std::cout << "False" << std::endl;
}

int main() {
fraction f1, f2;
std::cin >> f1 >> f2;
std::cout << f1 + f2 << ' ' << f1 - f2 << ' '
<< f1 * f2 << ' ' << f1 / f2 << std::endl;
f1 += f2;
f1 -= f2;
f1 *= f2;
f1 /= f2;
std::cout << f1 << std::endl;
print(f1 == f2);
print(f1 != f2);
print(f1 < f2);
print(f1 > f2);
print(f1 <= f2);
print(f1 >= f2);
return 0;
}

_________________________

Output:

/FractionAllOperatorOverload 2/3 4/5 22/15-2/15 8/15 5/6 1000/1500 False True True False True False Press any key to continue

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
In this assignment you are required to complete a class for fractions. It stores the numerator...
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...

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

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

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

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

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

  • C++ For this assignment you will be building on the Original Fraction class you began last...

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

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

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

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