n previous chapters you have been developing a Fraction stru

n previous chapters, you have been developing a Fraction structure for Teacher’s Pet Software. Now you will develop a class that contains the fields and functions that a Fraction needs. Create a Fraction class with three private data fields for whole number, numerator, and denominator. Also create a constant static public field to hold the symbol that separates a numerator and denominator when a Fraction is displayed—the slash.

Create three public member functions for the class, as follows:

» An enterFractionValue()function that prompts the user to enter values for the Fraction. Do not allow the user to enter a value of 0 for the denominator of any Fraction; continue to prompt the user for a denominator value until a valid one is entered.

» A reduceFraction()function that reduces a Fraction to proper form. For example, a Fraction with the value 0 2/6 would be reduced to 0 1/3 and a Fraction with the value 4 18/4 would be reduced to 8 1/2.

» A displayFraction()function that displays the Fraction whole number, numerator, slash, and denominator. Add any other functions to the Fraction class that will be useful to you. Create a main() function that declares a Fraction object, and continues to get Fraction values from the user until the user enters a Fraction with value 0 (both the whole number and numerator are 0). For each Fraction entered, display the Fraction, reduce the Fraction, and display the Fraction again.

Solution

Solution:

//Include Header files

#include<iostream>

#include<string>

using namespace std;

//Declare class name

class Fraction

{

private:

int number;

int numerator;

int denominator;

public:

const static char seperator = \'/\';

/*Define the enterFractionValue()function to declares a local Fraction object and get the user to enter values for the Fraction and returns a data-filled Fraction object.*/

void enterFractionValue()

{

string localFraction;

repeat:

//Prompt the user input

cout << \"Enter the Fractional value: \";

cin >> number >> numerator >> localFraction;

bool state = false;

int i, sl;

for (i = 0; i < localFraction.size(); i++)

{

if (localFraction[i] == \'/\')

{            

state = true;

sl = i;

break;

}

}

if (state == false)

{

cout << \"Given input wrong. \";

goto repeat;

}

else if (localFraction[i+1] == \'0\')

{

cout <<\"Zero is not permitted in denominator, \";

goto repeat;

}

char *denom = new char [localFraction.size()+1];

int r = 0;

for (int i = sl+1; i < localFraction.size(); i++)

{

denom[r] = localFraction[i];

r++;

}

denom[r] = \'\\0\';

denominator = atoi(denom);

}

int gcd(int x, int y)

{

for (;;)

{

if (x == 0) return y;

y %= x;

if (y == 0) return x;

x %= y;

}

}

/*Define the reduceFraction() function to accepts a Fraction object and reduces it to proper form, and then return the reduced Fraction.*/

void reduceFraction()

{

if (number > 0)

{

if (numerator % number > 0)

{

number += numerator / number;

numerator = numerator % number;

}

}

if(denominator > numerator)

{

int gcdi = gcd(denominator, numerator);

cout << endl;

numerator = numerator/gcdi;

denominator = denominator/gcdi;

}

if (numerator > denominator && numerator%denominator == 0)

{

numerator = numerator/denominator;

denominator = 0;

}

}

/*Define the displayFraction() function to displays any Fraction object passed to it.*/

void displayFraction()

{

cout<<number<<\" \" << numerator << separator << denominator <<endl;

}

};

// Define main()function

int main()

{

     //create object for class

Fraction frac;

//Call the enterFractionValue() function

frac.enterFractionValue();

//Call reduceFraction() function

frac.reduceFraction();

//Call displayFraction() function

frac.displayFraction();

//Pause the system

system(\"Pause\");

return 0;

}

Sample Output:

Enter the Fractional value: 0 2/4

0 1/2

n previous chapters, you have been developing a Fraction structure for Teacher’s Pet Software. Now you will develop a class that contains the fields and functio
n previous chapters, you have been developing a Fraction structure for Teacher’s Pet Software. Now you will develop a class that contains the fields and functio
n previous chapters, you have been developing a Fraction structure for Teacher’s Pet Software. Now you will develop a class that contains the fields and functio
n previous chapters, you have been developing a Fraction structure for Teacher’s Pet Software. Now you will develop a class that contains the fields and functio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site