Jump to content

Help, multiple classes + constructors

- - - - -

  • Please log in to reply
3 replies to this topic

#1
xXAlphaXx

xXAlphaXx

    Learning Programmer

  • Members
  • PipPipPip
  • 85 posts
This is homework and I am pretty stumped. Here is the description of what we have to do:

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 the problem, and the correct Fraction answer. The MathProblem[ class also contains a character field that stores an operator. (for now, assume the operator will be either + or *) and contains a bool field named isAnswerCorrect, indicating whether the user correctly answered the problem. 

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 false. Include a displaProblem() 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 appropiate.

Write a main() function that declares five MathProblem objects  you can use to test a student's fraction arithmetic skills. Assign vlaues to the MathProblems. Display the problems and accept the answer. 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.

The Fraction class is based off another program and is already implemented into my current code but it needs some polishing up, as I need to figure out how to get it to output in fraction notation. Right now it is outputting as a decimal.

Here is my current code:

#include "stdafx.h"
#include <iostream>

class Fraction
{
	private:
		double num;
		double den;
		int whole;
		double frac;
	public:
		Fraction(double, double);
		Fraction(int, double, double);
};

class MathProblem
{
	private:
		Fraction frac1;
		Fraction frac2;
		Fraction userAnswer;
		Fraction correctAnswer;
		char op;
		bool corr;
	public:
		void setProblem(Fraction, Fraction, char);
		void displayProblem();
		void askUserForAnswer();


};
void MathProblem::setProblem(Fraction frac1, Fraction frac2, char op)
{
	
}

Fraction::Fraction(double num, double den)
{
	if (den <= 0)
		den = 1;
	frac = num / den;
}

Fraction::Fraction(int whole, double num, double den)
{
	if (den <= 0)
		den = 1;
	frac = (num / den) + whole;
}

int main()
{
	MathProblem prob1;
	MathProblem prob2;
	MathProblem prob3;
	MathProblem prob4;
	MathProblem prob5;
	Fraction frac1;
	Fraction frac2;
	char op;


	std::cin.ignore();
	std::cin.get();
}

I appreciate everyones help on this, I think its pretty rough for me. :sleep:

Edited by xXAlphaXx, 25 April 2010 - 08:48 AM.
Typos.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why do you have a double value in your fraction class? I would ONLY have a numerator and denominator (not even a whole).

Output would simply be cout<<num<<'/'<<den;
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
xXAlphaXx

xXAlphaXx

    Learning Programmer

  • Members
  • PipPipPip
  • 85 posts
The whole is for mixed numbers

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Mixed numbers are a representation, not a value. I would strongly recommend that you only use a numerator and denominator for the internal representation. That will allow you to use output in mixed or improper representation at will. For the purposes of calculations, improper fractions are the most natural mode to operate in.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users