I need help with a C++ problem using Classes. Any help will be appreciated! Now
ID: 3635669 • Letter: I
Question
I need help with a C++ problem using Classes. Any help will be appreciated!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
Take a look. Compiled under VS2010. Tell me if any problems still arise with it.
#include<iostream>
#include<string>
using namespace std;
class Fraction
{
private:
int number;
int numerator;
int denominator;
public:
const static char seperator = '/';
// Fraction();
// ~Fraction();
void enterFractionValue();
void reduceFraction();
void displayFraction();
};
void Fraction::enterFractionValue()
{
string fr;
again:
cout << "Enter the fractional value: ";
cin >> number >> numerator >> fr;
bool state = false;
int i, sl;
for (i = 0; i < fr.size(); i++)
{
if (fr[i] == '/')
{
state = true;
sl = i;
break;
}
}
if (state == false)
{
cout << "Illegal input, ";
goto again;
}
else if (fr[i+1] == '0')
{
cout << "Zero is not permited in denominator, ";
goto again;
}
char *denom = new char [fr.size()+1];
int r = 0;
for (int i = sl+1; i < fr.size(); i++)
{
denom[r] = fr[i];
r++;
}
denom[r] = '';
denominator = atoi(denom);
}
int gcd(int a, int b)
{
for (;;)
{
if (a == 0) return b;
b %= a;
if (b == 0) return a;
a %= b;
}
}
void Fraction::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;
}
}
void Fraction::displayFraction()
{
cout<<number<<" "<<numerator<<seperator<<denominator<<endl;
}
int main()
{
Fraction frac1;
frac1.enterFractionValue();
frac1.reduceFraction();
frac1.displayFraction();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.