This problem needs to be completed in C++ and be able to run in Visual Studio 20
ID: 670387 • Letter: T
Question
This problem needs to be completed in C++ and be able to run in Visual Studio 2012 or later. It cannot use any #include statements that end in .h, such as conio.h, stdlib.h, etc. PLEASE DO NOT ANSWER UNLESS YOU CAN COMPLETE ALL PARTS OF THE PROBLEM!
(1) 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 symbol ("/").
(2) Create a public 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.
(3) Create a public 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 Fraciton with the value 4 18/4 would be reduced to 8 1/2.
(4) Create a public displayFraciton() function that displays the Fraction with whole number, numerator, slash symbol ("/") and denominator.
(5) Add any other functions to the Fraction class that might be useful.
(6) 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 numer and numerator are 0). For EACH fraction that the user has entered, display the Fraction, reduce the Fraction, and display the Fraction again.
Explanation / Answer
#include <iostream>
using namespace std;
class Fraction{
public:
int wholeNumber;
int numerator;
int denominator;
static const char seperator = '/';
void enterFractionValue();
void reduceFraction();
void displayFraction();
};
void Fraction::enterFractionValue(){
cout << "Whole Number: ";
cin >> wholeNumber;
cout << "Numerator: ";
cin >> numerator;
do{
cout << "Denominator: ";
cin >> denominator;
} while (denominator == 0);
}
void Fraction::reduceFraction(){
if (numerator > denominator){
wholeNumber += (numerator / denominator);
numerator %= denominator;
}
int min = numerator;
for (int i = 2; i <= min; ++i){
while ((numerator % i == 0) && (denominator % i == 0)){
numerator /= i;
denominator /= i;
}
}
}
void Fraction::displayFraction(){
cout << wholeNumber << " " << numerator << seperator << denominator << endl;
}
int main(){
Fraction f;
while (true){
f.enterFractionValue();
if (f.numerator == 0 && f.wholeNumber == 0){
cout << "Exiting..." << endl;
break;
}
f.displayFraction();
f.reduceFraction();
f.displayFraction();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.