The following needs to be done in C++. The code must be able to accept and perfo
ID: 668984 • Letter: T
Question
The following needs to be done in C++. The code must be able to accept and perform calculations on whole numbers as specified. Please make sure all conditions described are met - Thanks for your help!
You are developing a Fraction structure for Teacher’s Pet Software. The structure contains
three public data fields for whole number, numerator, and denominator. Using the same
structure, write the functions described below:
» An enterFractionValue()function that declares a local Fraction object and
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. The function returns a data-filled
Fraction object to the calling function.
» A reduceFraction()function that accepts a Fraction object and reduces it to proper
form, returning the reduced Fraction. For example, a Fraction entering the function as 0
2/6 would be reduced to 0 1/3, and a Fraction entered as 4 18/4 would be reduced to 8 1/2.
» A displayFraction()function that displays any Fraction object passed to it. This
function displays a fraction with a slash between the numerator and denominator.
» 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
the numerator are 0). For each Fraction entered, display the Fraction, reduce the
Fraction, and display the Fraction again.
Explanation / Answer
The code for your requirement is given below:
#include <iostream>
using namespace std;
class FractionConvert
{
int wholeNumber;
int numerator;
int denominator;
public:
FractionConvert enterFractionValue() //Reads the Fraction.
{
FractionConvert fc;
cout <<"Enter the whole number part of the fraction: ";
cin>>fc.wholeNumber;
cout<<"Enter the numerator part of the fraction: ";
cin>>fc.numerator;
fc.denominator = 0;
while(fc.denominator == 0)
{
cout<<"Enter the denominator part of the fraction.(Should be a non zero value): ";
cin>>fc.denominator;
}
return fc;
}
FractionConvert reduceFraction(FractionConvert f) //Reduces the Fraction.
{
int temp;
int i;
temp = f.wholeNumber * f.denominator + f.numerator; //Converting a mixed fraction into proper fraction.
f.wholeNumber = temp / f.denominator; //Updating the wholeNumber with new quotient.
f.numerator = temp % f.denominator; //Updating the numerator with new remainder.
for(i=2;i<=f.numerator;i++)
{
if(f.numerator % i == 0 && f.denominator % i == 0) //Further reducing the numerator and denominator if there are factors.
{
f.numerator = f.numerator / i;
f.denominator = f.denominator / i;
}
}
return f;
}
void displayFraction(FractionConvert f) //Displays the Fraction in the prescribed format.
{
cout<<"The reduced fraction is: "<<f.wholeNumber<<" "<<f.numerator<<"/"<<f.denominator<<endl;
}
};
int main()
{
FractionConvert fc;
fc = fc.enterFractionValue();
fc = fc.reduceFraction(fc);
fc.displayFraction(fc);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.