I need assitance with the my solution created help me fix it in order and please
ID: 3739071 • Letter: I
Question
I need assitance with the my solution created
help me fix it in order and please explain how it was done
#include <iostream>
using namespace std;
class fraction; {
int wholenumber;
int numerator;
int denominator;
public:
void enterfractionValue();
void displayfraction();
void reducefraction();
int getNumerator();
int getWholenumber();
}
int fraction::getWholenumber() {
return wholenumber;
}
int fraction::getNumerator() {
return numerator;
}
void fraction::enterfractionValue() {
cout << " Enter the whole number";
cin >> wholenumber;
cout << " Enter the numerator";
cin >> numerator;
}
do {
cout << " Enter the denominator:";
cin >> denominator;
if (denominator == 0) {
cout << "Invalid denominator";
Cout << "Please re enter";
}
else
break;
} while ()
}void fraction::displayfraction() {
cout << wholenumber << "" << numerator << "/" << denominator;
}void fraction::reducefraction() {
int complete = wholenumber * denominator + numerator;
wholenumber = complete / denominator;
numerator = complete % denominator;
if ((numerator % 2 == 0) && denominator % 2 == 0)){
numerator = numerator / 2;
denominator = denominator / 2;
}
int main() {
fraction fristNo;
do {
fristNo.enterfractionValue();
if ((firstNo.getwholenumber() == 0) && (firstNo.getnumerator() == 0))
}break;
} firstNo.displayfraction();
fristNo.reducefraction();
firstNo.displayfraction();
}while()
}
In 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.
Explanation / Answer
#include<iostream>
using namespace std;
class Fraction
{
public:
Fraction(int = 0, int = 0, int = 1);
void reduceFraction();
void enterFractionValue();
void displayFraction();
int gcd(int,int);
private:
int whole, num, denom;
};
Fraction::Fraction(int a, int b, int c)// argument constructor
{
whole = a;
num = b;
denom = c;
reduceFraction();
}
int Fraction::gcd (int x, int y) { //function to find greatest common divisor
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}
void Fraction::reduceFraction()
{
int factor;
factor = gcd(num, denom);
num = num / factor;
denom= denom / factor;
}
void Fraction::displayFraction()
{
cout << whole << " " << num << "/" << denom << endl;
}
void Fraction::enterFractionValue()
{
int w,n,d;
cout<<"Enter whole number : ";
cin>>w;
cout<<"Enter numerator : ";
cin>>n;
cout<<" Enter denominator : ";
cin>>d;
while(d == 0)
{
cout<<" Enter denominator : ";
cin>>d;
}
whole = w;
num = n;
denom = d;
}
int main()
{
Fraction f ;
f.enterFractionValue();
cout<<endl;
f.displayFraction();
f.reduceFraction();
cout<<" After reducing fraction ";
f.displayFraction();
}
Output:
Enter whole number :3 Enter numerator :16
Enter denominator :20
3 16/20
After reducing fraction 3 4/5
Do ask if any query. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.