Question

C++ Question

Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero i

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 cont in

_________________Thank You

Add a comment
Know the answer?
Add Answer to:
C++ Question Your class should support the following operations on Fraction objects: • Construction of a...
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
  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

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

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less 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. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

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

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

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

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

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