In: Computer Science
c++ Programming
For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions.
Please keep all of your code in one file for this week. We will separate things into three files for the next assignment. Your class will go first, then your class member function definitions, then main().
Here are the client program and correct output.
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: assert(inDenominator != 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. Only use one function for each operator.
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).
Additional Requirements and Hints:
Getting Started
Here are some suggestions for those of you who have trouble just figuring out where to start with assignment 1. Remember to use iterative development. That means start with the smallest, simplest subset of the final product that you can, make sure it works, and then start adding things to it one at a time (preferably the simple things first, if possible).
Start with just a default constructor and a stream insertion operator. For now, don't even worry about mixed numbers, just write the stream insertion operator so that it works with proper fractions. Test this out with a client program something like this:
int main(){ Fraction f1; cout << f1; }
(You should get output of "0/1" because you should have initialized the fraction to 0/1 in your constructor.)
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:
_____________________Thank You