You have been developing a Fraction class for Teacher’s Pet Software that contai
ID: 3718978 • Letter: Y
Question
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 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<iostream>
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);
}
Explanation / Answer
#include<iostream>
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)
{
is >> f.numerator>> 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;
}
int gcd(int x, int y) // compute greatest common divisor
{
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}
void minimize() {
int factor;
if(numerator > 0)
{
factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}
else
{
this->numerator = numerator;
this->denominator = 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();
cout<<t;
}
else if (operatorSymbol == "-")
{
t = f1 - f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
cout<<t;
}
else if (operatorSymbol == "*")
{
t = f1 * f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
cout<<t;
}
else if (operatorSymbol == "/")
{
t = f1 / f2;
cout << f1 << operatorSymbol << f2 << " = " << t;
cout << " Minimized Fraction -> ";
t.minimize();
cout<<t;
}
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);
}
Output:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol:+
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction:2 3
Enter an Second Fraction: 3 2
2/3+3/2 = 13/6
Minimized Fraction -> 13/6
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol:-
Example First fraction 2/3 - second fraction 3/2
Enter an First Fraction: 2 3
Enter an Second Fraction: 3 2
2/3-3/2 = -5/6
Minimized Fraction -> -5/6
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol:*
Example First fraction 2/3 * second fraction 3/2
Enter an First Fraction:2 3
Enter an Second Fraction: 3 2
2/3*3/2 = 6/6
Minimized Fraction -> 1/1
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol:/
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction:2 3
Enter an Second Fraction:3 2
2/3/3/2 = 4/9
Minimized Fraction -> 4/9
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol:==
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction:2 3
Enter an Second Fraction:3 2
2/3 is not to 3/2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol:$
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.