Question

You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// 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<<"\n 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<<"\n + for Addition \n - for Subtraction \n * for Multiplication \n / for Division \
\n > - for greater than \n < for less than \n == for check equals \n $ - 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<<"\n Enter the expression symbol: ";
cin>>operatorSymbol;

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

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

// Accepts second fraction
cout<<"\n 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<<"\n 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<<"\n 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<<"\n 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<<"\n 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<<"\n "<<f1<<" is greater than "<<f2;
// Otherwise f1 is not greater than f2
else
// If false display the result
cout<<"\n "<<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<<"\n "<<f1<<" is smaller than "<<f2;
// Otherwise f1 is not less than f2
else
// If false display the result
cout<<"\n "<<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<<"\n 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: $

Add a comment
Know the answer?
Add Answer to:
You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    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...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question 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...

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...

  • Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different...

    Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different class and functions from problem 1. The main function calls different functions as instructed in the problem description. Submit the CPP file during submission Problem 1. Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and the denominator (one for each data). Also...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT