Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have been developing a Fraction class for Teacher’s Pet Software that contai

ID: 3717158 • 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.

e. Write a main()program that declares an array of 10 randomly generated Fraction values,

for which each numerator is a random value between 1 and 5 inclusive. (Instructions on

generating random numbers appear in Appendix E.) Assign the value 10 to the denominator

of each Fraction. Reduce each of the 10 randomly generated Fractions to its proper

form (for example, 2/10 is 1/5) and display them. Save the file as FractionPartE.cpp.

f. Remove the statements from main()that display the 10 Fraction objects. Add statements

to the main()function that prompt the user to choose between four operations

for an arithmetic drill—addition, subtraction, multiplication, or division. After the user

has selected an operation, generate five problems using five pairs of Fractions from the

10-element array. (If the user selects the subtraction option, make sure the first operand

in the problem is not smaller than the second so that the correct answer is not negative.)

Display a problem (for example, if the user chooses addition, the problem might be “1/10 +

3/10”) and allow the user to enter an answer from the keyboard. After each keyboard answer,

notify the user whether the answer was correct. Save the file as FractionPartF.cpp.

g. Add code to the main()function that allows the user up to three attempts to correctly

answer each problem. Save the file as FractionPartG.cpp.

h. Add code to the main()function that keeps score. At the end of the program, display a

message similar to “You got 3 correct out of 5 problems”. Save the file as FractionPartH.cpp.

i. Alter the main()function to generate random numbers between 6 and 10 inclusive for

the denominators. Save the file as FractionPartI.cpp.

Explanation / Answer

// FileName: FractionOperatorOverloading.cpp
#include<iostream>
#include<stdlib.h>
using namespace std;

//Class Fraction definition
class Fraction
{
//Data member for numerator and denominator
int numerator;
int denominator;
public:
// Default constructor
Fraction()
{
numerator = denominator = 1;
}// End of default constructor

// Input redirection operator overloaded
friend ostream& operator<<(ostream &os, const Fraction &f)
{
// Stores numerator, operator symbol (/) and denominator in os stream
os<<f.numerator<<" / " <<f.denominator;
// Returns output stream
return os;
}// End of function

// Output redirection operator overloaded
friend istream& operator >> (istream &is, Fraction &f)
{
// To store symbol
char ch;
// Stores numerator in is stream
is>>f.numerator;
// To discard fraction symbol
is>>ch;
// Stores denominator in is stream
is>>f.denominator;
// Returns input stream
return is;
}// End of function

// Overloading + operator
// Addition operator overloaded
Fraction operator + (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Add both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator + f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function

// Overloading - operator
// Subtraction operator overloaded
Fraction operator - (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Subtracts both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator - f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function

// Overloading * operator
// Function to perform multiplication operation and return the result object
Fraction operator * (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Multiplies the implicit object numerator with parameter object denominator and stores it in temporary object numerator
t.numerator = numerator * f.numerator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function

// Overloading / operator
// Function to return the object after division
Fraction operator / (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Checks for divide by zero error
if(f.denominator == 0 || denominator == 0)
{
cout<<" Cannot divide by zero";
exit(0);
}// End of if condition
// Otherwise denominator is not zero
else
{
// Multiply implicit object numerator with parameter object denominator and store it in the temporary object numerator
t.numerator = numerator * f.denominator;
// Multiply parameter object numerator with implicit object denominator and store it in the temporary object denominator
t.denominator = f.numerator * denominator;
// Return the result object
return t;
}// End of else
}// End of function

// Overloading > operator
//Returns true if both numerator and denominator cross multiplication is greater
bool operator > (Fraction f)
{
return (numerator * f.denominator > denominator * f.numerator);
}// End of function

// Overloading < operator
//Returns true if both numerator and denominator cross multiplication is less
bool operator < (Fraction f)
{
return (numerator * f.denominator < denominator * f.numerator);
}// End of function

// Overloading = operator
// Returns the object after assignment
bool operator == (Fraction f)
{
if((numerator == f.numerator) && (denominator == f.denominator))
return true;
else
return false;
}// End of function

// Function to minimize
void minimize()
{
// Loops from 2 to denominator value
for(int x = 2; x < denominator; x++)
{
// Checks if x is divisible by both numerator and denominator
if((numerator % x == 0) && (denominator % x == 0))
{
// Divide numerator by x and store the quotient in numerator
numerator = numerator / x;
// Divide denominator by x and store the quotient in denominator
denominator = denominator / x;
}// End of if condition
}// End of for loop
// Displays the result
cout<<numerator<<"/"<<denominator;
}// End of function
};//End of class

//Displays the menu for Operator
void selectOperator()
{
cout<<" + for Addition - for Subtraction * for Multiplication / for Division
> - for greater than < for less than == for check equals $ - for exit.";
}// End of function

// Main method
int main()
{
// To store operator symbol
string operatorSymbol;
// Creates two Fraction class object
Fraction f1, f2;
// Creates an object to store result
Fraction t;

// Loops till operator symbol is not "$"
do
{
// Displays the menu for operator symbol
selectOperator();

// Accepts the symbol
cout<<" Enter the expression symbol: ";
cin>>operatorSymbol;

// Checks if operator symbol is "$" exit the program
if(operatorSymbol == "$")
exit(0);
cout<<" Example First fraction 2/3 + second fraction 3/2 ";

// Accepts first fraction
cout<<"Enter an First Fraction: ";
cin>>f1;

// Accepts second fraction
cout<<" Enter an Second Fraction: ";
cin>>f2;

// Checks if the operator symbol is "+"
if(operatorSymbol == "+")
{
// Overloads the + operator to perform addition and store the result in t
t = f1 + f2;
// Displays the result
cout<<f1<<operatorSymbol<<f2<<" = "<<t;
// Calls the function to minimize the fraction
cout<<" Minimized Fraction -> ";
t.minimize();
}// End of if condition

// Otherwise checks if the operator symbol is "-"
else if(operatorSymbol == "-")
{
// Overloads the - operator to perform subtraction and store the result in t
t = f1 - f2;
// Displays the result
cout<<f1<<operatorSymbol<<f2<<" = "<<t;
// Calls the function to minimize the fraction
cout<<" Minimized Fraction -> ";
t.minimize();
}// End of if condition

// Otherwise checks if the operator symbol is "*"
else if(operatorSymbol == "*")
{
// Overloads the * operator to perform multiplication and store the result in t
t = f1 * f2;
// Displays the result
cout<<f1<<operatorSymbol<<f2<<" = "<<t;
// Calls the function to minimize the fraction
cout<<" Minimized Fraction -> ";
t.minimize();
}// End of if condition

// Otherwise checks if the operator symbol is "/"
else if(operatorSymbol == "/")
{
// Overloads the / operator to perform division and store the result in t
t = f1 / f2;
// Displays the result
cout<<f1<<operatorSymbol<<f2<<" = "<<t;
// Calls the function to minimize the fraction
cout<<" Minimized Fraction -> ";
t.minimize();
}// End of if condition

// Otherwise checks if the operator symbol is ">"
else if(operatorSymbol == ">")
{
// Overloads the > operator to check if object f1 is greater than f2
if(f1 > f2)
// If true display the result
cout<<" "<<f1<<" is greater than "<<f2;
// Otherwise f1 is not greater than f2
else
// If false display the result
cout<<" "<<f2<<" is greater than "<<f1;
}// End of if condition

// Otherwise checks if the operator symbol is "<"
else if(operatorSymbol == "<")
{
// Overloads the < operator to check if object f1 is less than f2
if(f1 < f2)
// If true display the result
cout<<" "<<f1<<" is smaller than "<<f2;
// Otherwise f1 is not less than f2
else
// If false display the result
cout<<" "<<f2<<" is smaller than "<<f1;
}// End of if condition

// Otherwise checks if the operator symbol is "=="
else if(operatorSymbol == "==")
{
// Overloads the == operator to check if object f1 is equals to f2
if(f1 == f2)
// If true display the result
cout<<f1<<" is equals to "<<f2;
// Otherwise f1 is not equals to f2
else
// If false display the result
cout<<f1<<" is not to "<<f2;
}// End of if condition

// Otherwise display invalid symbol
else
cout<<" Invalid symbol";
}while(1); // End of do while
}//End of main

Sample 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: 12/2

Enter an Second Fraction: 2/4
12 / 2+2 / 4 = 52 / 8
Minimized Fraction -> 26/4
+ 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: 4/5

Enter an Second Fraction: 2/3
4 / 5-2 / 3 = 2 / 15
Minimized Fraction -> 2/15
+ 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: 4/8

Enter an Second Fraction: 3/5
4 / 8*3 / 5 = 12 / 40
Minimized Fraction -> 6/20
+ 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: 45/2

Enter an Second Fraction: 12/2
45 / 2/12 / 2 = 90 / 24
Minimized Fraction -> 15/4
+ 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: 12/2

Enter an Second Fraction: 3/4

12 / 2 is greater than 3 / 4
+ 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: 6/2

Enter an Second Fraction: 1/2

1 / 2 is smaller than 6 / 2
+ 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: 2/3
2 / 3 is equals to 2 / 3
+ 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: 4/5

Enter an Second Fraction: 2/6
4 / 5 is not to 2 / 6
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: $

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote