Question
You have been developing a Fraction class for Teachers Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *,
You have been developing a Fraction class for Teachers 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 format; for example,
1/2 * 2/3 results in 1/3, not 2/6.
b. Add an operator==()function that compares the value of two Fractions.
c. Add operator>()and operator<()functions that compare the values of two Fractions.
d. Add extraction and insertion operators for the Fraction class.
Having a problem with running the code on visual code
#include
using namespace std;
class Fraction
{
int numerator;
int denominator;
public:
Fraction()
{
numerator = denominator = 1;
}
friend ostream & operator<<(ostream &os, const Fraction &f)
{
os << f.numerator << " / " << f.denominator;
return os;
}
friend istream& operator >> (istream &is, Fraction &f)
{
char ch;
is >> f.numerator;
is >> ch;
is >> f.denominator;
return is;
}
Fraction operator + (Fraction f)
{
Fraction t;
t.numerator = numerator * f.denominator + f.numerator * denominator;
t.denominator = denominator * f.denominator;
return t;
}
Fraction operator - (Fraction f)
{
Fraction t;
t.numerator = numerator * f.denominator - f.numerator * denominator;
t.denominator = denominator * f.denominator;
return t;
}
Fraction operator * (Fraction f)
{
Fraction t;
t.numerator = numerator * f.numerator;
t.denominator = denominator * f.denominator;
return t;
}
Fraction operator / (Fraction f)
{
Fraction t;
if (f.denominator == 0 || denominator == 0)
{
cout << " Cannot divide by zero";
exit(0);
}
else
{
t.numerator = numerator * f.denominator;
t.denominator = f.numerator * denominator;
return t;
}
}
bool operator > (Fraction f)
{
return (numerator * f.denominator > denominator * f.numerator);
}
bool operator < (Fraction f)
{
return (numerator * f.denominator < denominator * f.numerator);
}
bool operator == (Fraction f)
{
if ((numerator == f.numerator) && (denominator == f.denominator))
return true;
else
return false;
}
void minimize()
{
for (int x = 2; x < denominator; x++)
{
if ((numerator % x == 0) && (denominator % x == 0))
{
numerator = numerator / x;
denominator = denominator / x;
}
}
cout << numerator << "/" << denominator;
}
};
void selectOperator()
{
cout << " + for Addition - for Subtraction * for Multiplication / for Division \
> - for greater than < for less than == for check equals $ - for exit.";
}
int main()
{
string operatorSymbol;
Fraction f1, f2;
Fraction t;
do
{
selectOperator();
cout << " Enter the expression symbol: ";
cin >> operatorSymbol;
if (operatorSymbol == "$")
exit(0);
cout << " Example First fraction 2/3 + second fraction 3/2 ";
cout << "Enter an First Fraction: ";
cin >> f1;
cout << " Enter an Second Fraction: ";
cin >> f2;
if (operatorSymbol == "+")
{
t = f1 + f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
}
else if (operatorSymbol == "-")
{
t = f1 - f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
}
else if (operatorSymbol == "*")
{
t = f1 * f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
}
else if (operatorSymbol == "/")
{
t = f1 / f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
}
else if (operatorSymbol == ">")
{
if (f1 > f2)
cout << " " << f1 << " is greater than " << f2;
else
cout << " " << f2 << " is greater than " << f1;
}
else if (operatorSymbol == "<")
{
if (f1 < f2)
cout << " " << f1 << " is smaller than " << f2;
else
cout << " " << f2 << " is smaller than " << f1;
}
else if (operatorSymbol == "==")
{
if (f1 == f2)
cout << f1 << " is equals to " << f2;
else
cout << f1 << " is not to " << f2;
}
else
cout << " Invalid symbol";
} while (1);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started