Please check my solution and assist me with it and show me where i made my mista
ID: 3740240 • Letter: P
Question
Please check my solution and assist me with it and show me where i made my mistakes :
#include
using namespace std;
class fraction {
int numerator;
int denominator;
double wholenumber;
fraction( int numerator = 0, int denominator = 1);
fraction(double wholenumber, int numerator = 0, int denominator = 1);
void reduceFraction();
};
fraction::fraction(numerator, denominator) {
numerator = numerator;
denominator = denominator;
wholenumber = 0;
reducefraction();
}
fraction::fraction(double wholenumber, int numerator, int denominator) {
numerator = numerator;
denominator = denominator;
wholenumber = wholenumber;
reduceFraction();
};
void fraction::reducefraction() {
if (wholenumber == 0) {
for (int index = denominator * numerator; index--) {
if ((denominator % index == 0) && (numerator % index == 0)) {
denominator /= index;
numerator / = index;
}
}
}
else {
int number = denominator * wholenumber + numerator;
for (int index = denominator * number; index > 1; index--) {
if ((denominator % index == 0) && (number% index == 0)) {
denominator /= index;
number /= index;
}
}
wholenumber = number / denominator;
numerator = number % denominator;
}
cout << wholenumber << "" << numerator << "/" << denominator;
}
void main() {
int numerator;
int denominator;
double wholenumber;
cout << " Program to reduce fraction" << " Enter 0 when you don't want to enter value";
cout << "Enter a whole number";
cin >> wholenumber;
cout << "Enter numerator";
cin >> numerator;
cout << "Enter denominator";
cin >> denominator;
if (wholenumber == 0 && denominator == 0) {
cout << "Denominator has been set to defult value 1"
fraction(numerator);
}
}
else if (denominator == 0) {
cout << " Denominator has been set to defult value 1";
fraction(wholenumber, numerator);
}
else fraction(wholenumber, numerator, denominator)
getch();
}
a. 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.
b. 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 operatorwill 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
ScreeShot
--------------------------------------------------------------------------------------------
a) Frction Program
Program
//HeaderFiles
#include<iostream>
#include<math.h>
//For input output(cin,cout)
using namespace std;
//Fraction class
class fraction {
//member variables
int numerator;
int denominator;
double wholenumber;
//You have to declare member functions as public
public:
//Default constructor
fraction();
//Construction with parameters
fraction(double wholenumber, int numerator = 0, int denominator = 1);
//method
void reduceFraction();
};//end of the class
//method definitions
fraction::fraction() {
//put default values to numerator and denominator
numerator = 0;
denominator = 1;
reduceFraction();
}
fraction::fraction(double wholenumber, int numerator, int denominator) {
//set values to numerator,denominator and wholenumber with user given
numerator = numerator;
denominator = denominator;
wholenumber = wholenumber;
reduceFraction();
}
//fraction calculation expansion
void fraction::reduceFraction() {
if (wholenumber == 0) {
for (int index = 0; index = denominator * numerator; index--) {
if ((denominator % index == 0) && (numerator % index == 0)) {
denominator /= index;
numerator /= index;
}
}
}
else {
int number = denominator * wholenumber + numerator;
for (int index = denominator * number; index > 1; index--) {
if ((denominator % index == 0) && (number% index == 0)) {
denominator /= index;
number /= index;
}
}
wholenumber = number / denominator;
numerator = number % denominator;
}
cout << wholenumber << "" << numerator << "/" << denominator;
}
//main function
int main() {
//variable declaration
int numerator;
int denominator;
double wholenumber;
//user prompt
cout << " Program to reduce fraction" << " Enter 0 when you don't want to enter value";
cout << "Enter a whole number";
cin >> wholenumber;
cout << "Enter numerator";
cin >> numerator;
cout << "Enter denominator";
cin >> denominator;
//check denominator
if (wholenumber == 0 && denominator == 0) {
cout << "Denominator has been set to defult value 1";
fraction f(wholenumber, numerator, 1);
}
else if (denominator == 0) {
cout << " Denominator has been set to defult value 1";
fraction(wholenumber, numerator);
}
else
fraction(wholenumber, numerator, denominator);
return 0;
}
------------------------------------------------------------------------------------------------------------------
b)MathProblem Program
Code
//HeaderFiles
#include<iostream>
#include<math.h>
#include<string>
//For input output(cin,cout)
using namespace std;
//MathClass
class Math {
//Member variablez
char op;
int numerator1,denominator1, numerator2, denominator2;
//member functions
public:
//constructor
Math(char ch,int num1, int denom1, int num2, int denom2) {
op = ch;
numerator1 = num1;
numerator2 = num2;
denominator1 = denom1;
denominator2 = denom2;
}
//function
string setproblem() {
//Variable declaration
int numerator, denominator,num,denom;
//If operator+ do calculations
if (op=='+') {
if (denominator1 == denominator2) {
numerator = numerator1 + numerator2;
denominator = denominator1;
}
else {
numerator = numerator1 * denominator2 + numerator2 * denominator1;
denominator = denominator1 * denominator2;
}
}
//if operator=* do calculations
else if (op=='*') {
numerator = numerator1*numerator2 ;
denominator = denominator1*denominator2;
}
//User prompt
cout << "Please enter your answer for "<<numerator1<<"/"<<denominator1<<" "<<op<<" "<<numerator2<<"/"<<denominator2<<" = ";
cin >> num >> denom;
//Answer comparison
if (numerator == num && denominator == denom || numerator/2==num && denominator/2 == denom || numerator+( numerator / 2) == num && (denominator/ 2)+denominator == denom) {
return "true";
}
else
return "false";
}
};
//main function
int main() {
//Variable declaration
int c = 0;
//Object creation and function call
Math m1('+',1,2,1,4);
string answer1 = m1.setproblem();
cout<<"answer1 ="<<answer1<<endl;
if (answer1 == "true") {
c += 1;
}
Math m2('+', 2, 4, 1, 4);
string answer2 = m2.setproblem();
cout << "answer2 =" << answer2 << endl;
if (answer2 == "true") {
c += 1;
}
Math m3('+', 1, 2, 2, 4);
string answer3 = m3.setproblem();
cout << "answer3 =" << answer3 << endl;
if (answer3 == "true") {
c += 1;
}
Math m4('+', 6, 2, 1, 4);
string answer4 = m4.setproblem();
cout << "answer4 =" <<answer4 << endl;
if (answer4== "true") {
c += 1;
}
Math m5('+', 1, 2, 1, 4);
string answer5 = m5.setproblem();
cout << "answer5 =" << answer5<< endl;
if (answer5 == "true") {
c += 1;
}
//Score display
if (c==5) {
cout << "Your Score is 100%" << endl;
}
else if (c==4) {
cout << "Your Score is 80%" << endl;
}
else if (c == 3) {
cout << "Your Score is 60%" << endl;
}
else if (c == 2) {
cout << "Your Score is 40%" << endl;
}
else if (c == 1) {
cout << "Your Score is 20%" << endl;
}
else{
cout << "Your Score is 0" << endl;
}
return 0;
}
----------------------------------------------------
Note