Question: Skill set: Operator Overloading You will create a class to represent fractions and do fraction math. A fraction has a numerator and a denominator and

Skill set: Operator Overloading

You will create a class to represent fractions and do fraction math. A fraction has a numerator

and a denominator and is typically understood to look like: numerator/denominator, for example 3/4. Typically, when we try do fraction arithmetic using a computer, we see decimal equivalentresults.For example

float x=1, y=5, z; z = x/y + x/y; // 1/5 + 1/5 would calculate to .4, not 2/5

Wouldnt it be nice if we had software that could actually do fraction arithmetic and provide answers in reduced fractional form? This is your task!

The user will enter two fractions in the form: (numerator), then a single character (slash), then another integer (denominator), such as 3/4. There should be no spaces in the input.(Remember you need to store this input into 3 different variables.) The class will check if the are fraction is valid (i.e.denominator cannot be 0). All answers are displayed as a fraction, in reduced form. You will write this program using a class called Fraction. Here is the start of the class definition:

class Fraction { private: int num; //represents numerator int den; //represents denominator void simplify(); //reduces to lowest terms //note simplify should only be used by other member functions //so it is not a public function

public: Fraction() { int num = 0; int den = 1; } //constructor to set num, den; has default values //make sure to validate, set bad data to default values };

Note that the numerator defaults to zero, the denominator defaults to 1 (if no values are set or invalid values are entered) If 0 is entered for the denominator, change it to 1 (no loop to prompt for new value)

Create a Fraction Calculator that can add, subtract, multiply, divide and compare fractions by adding the following member functions to your class:

operator+ //returns the sum of two fractions

operator- //returns the difference of two fractions

operator* //returns the product of two fractions operator/ //returns the quotient of twofractions An overloaded stream insertion operator<< // displays the data as a fraction. (note: 4/1 should print as 4, 0/8 should print as 0)

An overloaded stream extraction operator>> // inputs the fraction (store numerator and denominator) Overloaded >, <, == //to compare fractions

I suggest you approach this problem step by step. Start with additi on only! DONT try to code and test the entire problem at one time!

Step1: Assume the fractions have the same denominator (easy!). (Dont validate this, just use something like 1/5 plus 2/5 to test) Concentrate on the input and output.

Step 2: Add code to address different denominators. You need to determine the ALGORITHM! (Hint: multiply the denominators to get a common denominator. It will not be the least common multiple, but you will get a correct answer.)

Step 3: Add code to display the result in reduced form (you will need to determine the algorithm to do this! The mod function (%) will be useful!).

NOTE: If you have problems with the simplify algorithm, dont spend too much time on it! If all else works but the result is not simplified it will be only a small point deduction!

Your program should provide a menu and function according to the sample output below:

Welcome to the fraction calculator Enter all fractions in the form numerator/denominator (ex. 3/4): 1- Addition 2- Subtraction 3- Multiplication 4 Division 5- Compare 6- Exit

Enter operation: 1 Enter first fraction: 4/5 Enter second fraction: 2/3

4/5 + 2/3 = 22/15

1- Addition 2- Subtraction 3- Multiplication 4 Division 5- Compare 6- Exit

Enter operation: 2 Enter first fraction: 1/8 Enter second fraction: 1/4

1/8-1/4 =-1/8

1- Addition 2- Subtraction 3- Multiplication 4 Division 5- Compare 6- Exit

Enter operation: 3 Enter first fraction: 1/2 Enter second fraction: 2/3 1/2 * 2/3 = 1/3

1- Addition 2- Subtraction 3- Multiplication 4 Division 5- Compare 6- Exit

Enter operation: 4 Enter first fraction: 5/6 Enter second fraction: 1/3 5/6 / 1/3 = 5/2

1- Addition 2- Subtraction 3- Multiplication 4 Division 5- Compare 6- Exit

Enter operation: 5 Enter first fraction: 1/2 Enter second fraction: 1/3

1/2 is greater than 1/3

1- Addition 2- Subtraction 3- Multiplication 4 Division 5- Compare 6- Exit

Enter operation: 6 Goodbye!

PLEASE ADD COMENTS. THANK YOU

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!