(C++ language Data Structure class) Rational fractions are of the form a / b, wh
ID: 3726926 • Letter: #
Question
(C++ language Data Structure class)
Rational fractions are of the form a / b, where a and b are integers and b 6¼ 0.
In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b
and c / d are fractions. Arithmetic operations on fractions are defined by the
following rules:
a / b + c / d ¼ (ad + bc) / bd
a / b c / d ¼ (ad bc)/bd
a / b c / d ¼ ac / bd
(a / b) / (c / d) ¼ ad / bc, where c / d 6¼ 0.
Fractions are compared as follows: a / b op c / d if ad op bc, where op is any of
the relational operations. For example, a / b < c / d if ad < bc.
Design a class—say, fractionType—that performs the arithmetic and
relational operations on fractions. Overload the arithmetic and relational
operators so that the appropriate symbols can be used to perform the
operation. Also, overload the stream insertion and stream extraction operators
for easy input and output.
Explanation / Answer
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;
class Fraction
{
public:
Fraction(int = 0, int = 0, int = 1);
void reduce();
void printData();
Fraction operator +(Fraction);
Fraction operator -(Fraction);
Fraction operator *(Fraction);
Fraction operator /(Fraction);
bool operator ==(Fraction &f2);
bool operator != (Fraction &f2);
friend ostream &operator<<( ostream &,const Fraction & );
friend istream &operator>>( istream &, Fraction &) ;
void recip();
private:
int whole, num, denom;
};
ostream &operator<<( ostream &output,const Fraction &F ) {
output << "Whole : " << F.whole << " Numerator : " << F.num << "Denominator" << F.denom;
return output;
}
istream &operator>>( istream &input, Fraction &F ) {
input >> F.whole >> F.num >> F.denom;
return input;
}
bool Fraction::operator== (Fraction &f2) //operator function
{
return ( this->whole == f2.whole && this->num == f2.num && this->denom == f2.denom);
}
bool Fraction::operator!= (Fraction &f2) //operator function
{
return ( this->whole != f2.whole && this->num != f2.num && this->denom != f2.denom);
}
Fraction::Fraction(int a, int b, int c)
{
whole = a;
num = b;
denom = c;
reduce();
}
void Fraction::reduce()
{
whole += num / denom;
num = num%denom;
if (num < 0)
{
num = denom + num;
whole--;
}
for (int i = 2; i <= sqrt(denom); i++)
{
if (num%i == 0)
if (denom%i == 0)
{
num = num / i;
denom = denom / i;
}
}
}
Fraction Fraction::operator+(Fraction f1) //operator function
{
Fraction f;
f.whole = this->whole + f1.whole;
f.denom = this->denom * f1.denom;
f.num = (this->denom * f1.num) + (f1.denom * this->num);
reduce();
return f;
}
Fraction Fraction::operator -(Fraction f1)
{
Fraction f;
f.whole = this->whole - f1.whole;
f.denom = this->denom * f1.denom;
f.num = (f1.denom * this->num) - (this->denom * f1.num);
reduce();
return f;
}
Fraction Fraction::operator *(Fraction f1)
{
Fraction f;
f.num = (this->whole * this->denom + this->num) * (f1.whole * f1.denom + f1.num);
f.denom = this->denom * f1.denom;
reduce();
return f;
}
Fraction Fraction::operator /(Fraction f1)
{
Fraction f;
f.num = (this->whole * this->denom + this->num) * f1.denom;
f.denom = this->denom * (f1.whole * f1.denom + f1.num);
reduce();
return f;
}
void Fraction::printData()
{
cout << whole << " " << num << "/" << denom << endl;
}
void Fraction::recip()
{
int t;
num = (whole * denom + num);
t = num;
num = denom;
denom = t;
whole = 0;
reduce();
}
int main()
{
Fraction f1, f2, sum, diff, prod, quo;
cin >> f1;
cin >> f2;
cout << "The first fraction is: " <<f1 << endl << endl;
cout << "THe second fraction is: " <<f2 << endl << endl;
sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;
cout << "The sum is: " << sum << endl << endl;
cout << "The difference is: " << diff << endl << endl;
cout << "The product is: " << prod << endl << endl;
cout << "The quotient is: " << quo << endl << endl;
if(f1==f2)
cout<< "The two fractions are equal!";
else
cout<< "The two fractions are not equal!";
if(f1!=f2)
cout << "The two fractions are not equal!";
else
cout << "The two fractions are equal.";
return 0;
}
Output:
4 3 2
2 1 2
The first fraction is: Whole : 4 Numerator : 3Denominator2
THe second fraction is: Whole : 2 Numerator : 1Denominator2
The sum is: Whole : 6 Numerator : 8Denominator4
The difference is: Whole : 3 Numerator : 0Denominator4
The product is: Whole : 0 Numerator : 55Denominator4
The quotient is: Whole : 0 Numerator : 22Denominator10
The two fractions are not equal!The two fractions are equal.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.