1 answer

C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

Question:

C++ Question

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

Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is not set to 0. The easiest way to do this is to use an assert statement: assertinDenominator != 0); You can put this statement at the top of your constructor. Note that the variable in the assert() is the incoming parameter, not the data member. In order to use assert(, you must #include <cassert> For this assignment, you may assume that all Fractions are positive. We'll fix that next week. • Printing a Fraction to a stream with an overloaded << operator. Next week we will get fancy with this, but for now just print the numerator, a forward-slash, and the denominator. No need to change improper Fractions to mixed numbers, and no need to reduce. • All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare Fractions to other Fractions as well as Fractions to integers. Either Fractions or integers can appear on either side of the binary comparison operator. You should only use one function for each operator. • The four basic arithmetic operations (+,-- */) should be supported. Again, they should allow Fractions to be combined with other Fractions, as well as with integers. Either Fractions or integers can appear on either side of the binary operator. Only use one function for each operator. Note that no special handling is needed to handle the case of dividing by a Fraction that is equal to 0. If the client attempts to do this, they will get a runtime error, which is the same behavior they would expect if they tried to divide by an int or double that was equal to 0. • The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. Fractions can appear on the left-hand side, and Fractions or integers on the right-hand side. • The increment and decrement (++, --) operators should be supported in both prefix and postfix form for Fractions. To increment or decrement a Fraction means to add or subtract (respectively) one (1).

Answers

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

.

Similar Solved Questions

1 answer
What is the mass of a rectangular piece of copper 24.4 cm x 11.4 cm x...
What is the mass of a rectangular piece of copper 24.4 cm x 11.4 cm x 8.9 cm?  The density of copper is 8.92 g/cm3. Use significant figures....
1 answer
Please show all work and to follow directions. Only answers Problem 16. 16)(4 points) Analyze Circuit...
Please show all work and to follow directions. Only answers Problem 16. 16)(4 points) Analyze Circuit in problem 15 using PSPICE as follows: Use a DC Sweep analysis to find Vx in problem 15 above. Let the voltage source vary from 0 to 300V in increments of 50V. Add a voltage printer in the schematic...
1 answer
Need help finding the remaining cogs and ending inventory . Date Description 217 Indiana buys $3,000...
need help finding the remaining cogs and ending inventory . Date Description 217 Indiana buys $3,000 of office supplies in cash. 2/15 Indiana pays off $30,000 of accounts payable. 2/18 Indiana collects the amount owed from Bellog, Inc from the 1/19 sale outside the discount period 2/27 Indi...
1 answer
M m Liege Ammerman Campus. Department of Physical Science 2. During a fractional distillation, why is...
m m Liege Ammerman Campus. Department of Physical Science 2. During a fractional distillation, why is a better separation achieved when heat mation, why is a better separation achieved when heat is applied slowly and steadily? (2 pts) 3. Con Consider heating a pure substance, such as acetone, to its...
1 answer
Consider the following chemical reaction: CH4 (g) + Cl2(g) + CCl4 (1) + CCl4 (I) +...
Consider the following chemical reaction: CH4 (g) + Cl2(g) + CCl4 (1) + CCl4 (I) + HCl (g) Identify the type of chemical reaction that this is classified as: oxidation-reduction (redox) O precipitation + acid-base precipitation + redox precipitation acid-base...
1 answer
Two blocks with equal mass m = 2.1 kg are connected by a string that passes...
Two blocks with equal mass m = 2.1 kg are connected by a string that passes over a pulley wheel. Block A sits on a level table, with friction acting between the block and ramp surfaces with coefficient of kinetic friction µk = 0.27. Block B is suspended below the pulley wheel, initially at a h...
1 answer
Workplace Applications Using the knowledge you obtained from the chapter, provide narrative answers to the following...
Workplace Applications Using the knowledge you obtained from the chapter, provide narrative answers to the following cases. 1. You are the privacy officer for your practice, and a new employee complains to you about the excessive HIPAA training she is enduring. What can you tell this employee about ...
1 answer
The Electric field for a wave is given as: î + û ) Eo sin (...
The Electric field for a wave is given as: î + û ) Eo sin ( wt – kz What is the direction of the associated magnetic field? ĝ) O(- +9) (+9) o...
1 answer
3JUse Gauss' Law to solve this problem. In the below spherical arrangement of charges a small...
3JUse Gauss' Law to solve this problem. In the below spherical arrangement of charges a small point charge q +2 nC is placed at the center. 30 cm is centered at this point charge, and this shell has a charge -q spread out over it (that is, this shell has a charge of -2 nC on it). Finally, a thin...
1 answer
What is the difference between a combination and a permutation? Give supporting examples.
What is the difference between a combination and a permutation? Give supporting examples....
1 answer
Suppose Puerto Rico and Hawaii currently have the same production possibilities frontier. The PPF is for...
Suppose Puerto Rico and Hawaii currently have the same production possibilities frontier. The PPF is for hotels and consumption goods in the two areas. Hotels are a capital good that, once built, will help produce still more consumption goods. If Puerto Rico produces more hotels than Hawaii, A) ...
1 answer
For the following clamping circuit: Vc 17 +7V + Si f= 1000Hz of -Vm Input Waveform...
For the following clamping circuit: Vc 17 +7V + Si f= 1000Hz of -Vm Input Waveform www 2.2-ko Negative Clamper Circuit a) When the capacitor is charged, calculate Vc and Vout b) Draw the output voltage for the signal Vin 10.5 -3.5 10.5...
1 answer
On January 1, 2019, Rodgers Company purchased $100,000 face value, 9%, 3-year bonds for $97,462.11, a...
On January 1, 2019, Rodgers Company purchased $100,000 face value, 9%, 3-year bonds for $97,462.11, a price that yields a 10% effective annual interest rate. The bonds pay interest semiannually on June 30 and December 31. Required: 1. Record the purchase of the bonds. 2. Prepare an inves...
1 answer
What causes storms to happen?
What causes storms to happen?...
1 answer
The intangible assets section of Bramble Corporation's balance sheet at December 31, 2022 is presented here....
The intangible assets section of Bramble Corporation's balance sheet at December 31, 2022 is presented here. Patents (579,900 cost less $7.990 amortization) Copyrights ($53,000 cost less $41,500 amortization) Total $73.400 11.500 $84.900 The patent was acquired in January 2022 and has a useful l...
1 answer
1.* (10 points) You may assume these data are normal and have equal variances. You are...
1.* (10 points) You may assume these data are normal and have equal variances. You are examining concentration of phosphorus from two different samples of spinach leaves. Measurements are in milligrams per cup of raw leaves. Does sample A have greater amounts of phosphorus than sample B? B A 92.2 83...