I need some serious help with the following problem for my final project. Any he
ID: 3635932 • Letter: I
Question
I need some serious help with the following problem for my final project. Any help is greatly appreciated and answers will be rated!You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions.
Add two constructors to the class. The first accepts two integer values representing the numerator and denominator. If a single integer is passed to the constructor, use it as the numerator, and use a default value of 1 for the denominator. If no values are passed to the constructor, use a default value of 0 for the numerator and 1 for the denominator. When any Fraction is constructed with a 0 argument for the denominator, force the denominator value to 1.
The second constructor requires three arguments: a whole number portion for a Fraction, a numerator, and a denominator. This constructor executes when any Fraction object is instantiated using three integer arguments. As with the other constructor, when any Fraction is constructed with a 0 argument for the denominator, force the denominator value to 1.
Whenever a Fraction object is constructed, automatically reduce the Fraction to the proper format. For example, a Fraction created using 0, 2, and 4 as arguments should be reduced to 1/2, and a Fraction created as 3 10/2 should be reduced to 8 0/1.
Write a main()function that declares several Fraction objects, and confirm that the class works correctly.
Save the file as Fraction.cpp.
Create a MathProblem class that contains four Fraction objects: the first Fraction operand in a problem, the second Fraction operand in a problem, the user’s Fraction answer to a problem, and the correct Fraction answer to a problem. The MathProblem class also contains a character field that stores an operator (for now, assume the operator will be either + or *) and contains an integer or bool field named isAnswerCorrect, indicating whether the user correctly answered the problem. For example, a MathProblem object containing 1/2, 1/4, and + for the operands and operators, 3/4 for the correct answer, and 3/8 for the user’s answer would contain a 0 or false in the isAnswerCorrect field because the correct answer and user’s answer do not match. Set the isAnswerCorrect field to true if the user’s answer is equivalent to the correct answer; for example, if 3/4 is the correct answer, then 3/4, 6/8, and 9/12 are all correct.
Include a function named setProblem()that sets a MathProblem’s values with arguments that include two Fraction operands and an operation. This function calculates and stores the correct answer, assigns 0 to the user’s answer, and sets isAnswerCorrect to 0 or false. Include a displayProblem()function that displays the math problem as a question, and an askUserForAnswer()function that accepts the user’s answer from the keyboard and assigns an appropriate value to isAnswerCorrect.
Include any other MathProblem functions you feel are useful and appropriate.
Write a main()function that declares five MathProblem objects you can use to test a student’s fraction arithmetic skills. Assign values to the MathProblems. Display the problems and accept the answers. When the five problems are completed, display each of the problems, along with the student’s answer, the correct answer, and a message indicating whether the student is right or wrong. Finally, show the student a score indicating the percentage of problems answered correctly.
Save the file as MathProblem.cpp.
Explanation / Answer
Here Fraction.cpp. Compiled in VS2010. Completely follows the project requirement and it has Fraction class additional member function for scanning the answer from the user for the second part. The answer and input for problem part2 should be in the form: 0 1/3 for example. Check if everything's ok.
#include
#include
using namespace std;
class Fraction
{
private:
int number;
int numerator;
int denominator;
public:
const static char seperator = '/';
Fraction(int num = 0, int denom = 1) { number = 0; numerator = num; denominator = denom; reduceFraction();}
Fraction(double n, int num, int denom = 1) { number = n; numerator = num; denominator = denom; reduceFraction();}
// ~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<}
int main()
{
Fraction frac1(2, 4, 10);
frac1.displayFraction();
Fraction frac2(200, 420);
frac2.displayFraction();
return 0;
}
then MathProblem.cpp:
#include<iostream>
#include<string>
using namespace std;
class Fraction
{
private:
int number;
int numerator;
int denominator;
public:
const static char seperator = '/';
Fraction(int num = 0, int denom = 1) { number = 0; numerator = num; denominator = denom; reduceFraction();}
Fraction(double n, int num, int denom = 1) { number = n; numerator = num; denominator = denom; reduceFraction();}
// ~Fraction();
void enterFractionValue();
void reduceFraction();
void displayFraction();
friend class MathProblem;
};
void Fraction::enterFractionValue()
{
string fr;
again:
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, enter again ";
int k;
goto again;
}
else if (fr[i+1] == '0')
{
cout << "Zero is not permited in denominator, enter again";
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;
}
class MathProblem
{
public:
Fraction operand1;
Fraction operand2;
Fraction userAnswer;
Fraction correctAnswer;
char operation;
bool isAnswerCorrect;
void setProblem(Fraction op1, char operat, Fraction op2);
void displayProblem();
void askUserForAnswer();
};
void MathProblem::setProblem(Fraction op1, char operat, Fraction op2)
{
userAnswer = 0;
operation = operat;
if (operat == '+' )
{
correctAnswer.number = op1.number + op2.number;
correctAnswer.denominator = op1.denominator + op2.denominator;
correctAnswer.numerator = op1.numerator + op2.numerator;
}
else if (operat == '*')
{
correctAnswer.number = op1.number * op2.number;
correctAnswer.denominator = op1.denominator * op2.denominator;
correctAnswer.numerator = op1.numerator * op2.numerator;
}
isAnswerCorrect = false;
correctAnswer.reduceFraction();
}
void MathProblem::askUserForAnswer()
{
cout << "Enter the answer: ";
userAnswer.enterFractionValue();
if ((userAnswer.denominator == correctAnswer.denominator) && (userAnswer.numerator == correctAnswer.numerator) && (userAnswer.number == correctAnswer.number))
{
isAnswerCorrect = true;
}
}
void MathProblem::displayProblem()
{
cout << "Problem: "; operand1.displayFraction(); cout << " " << operation << " " ; operand2.displayFraction(); cout << ". ";
}
int main()
{
int n = 5;
MathProblem problem[5];
for (int i = 0; i < n; i++)
{
Fraction oper1,oper2;
char operation;
cout << "Please insert the first operand for problem "<<i+1<<": ";
oper1.enterFractionValue();
cout << "Please enter the oparation(either + or *): ";
cin >> operation;
cout << "Please insert the second operand: ";
oper2.enterFractionValue();
problem[i].operand1 = oper1;
problem[i].operand2 = oper2;
problem[i].setProblem(oper1,operation,oper2);
}
double average = 0;
for (int i = 0; i < n; i++)
{
problem[i].displayProblem();
problem[i].askUserForAnswer();
if (problem[i].isAnswerCorrect == true)
average++;
}
cout<<endl;
for (int i = 0; i < n; i++)
{
problem[i].displayProblem();
cout << ", Student answer: "; problem[i].userAnswer.displayFraction(); cout << "Correct answer: "; problem[i].correctAnswer.displayFraction(); cout << endl;
}
cout << "Score = " << (average / n) * 100 << "%";
return 0;
}