| tjmohney (7) | |
|
I need to create a RationalNumber class to implement the storage and manipulation of rational numbers.I need to implement the following actions: A constructor that takes no parameters and initializes the object to have a value of zero A constructor (can you have two? Sure! As long as they have different parameters) that takes two integers (a numerator and denominator) and uses them to initialize the object A constructor that takes a double-precision number and uses a process based on the Stern-Brocot system to initialize the object. This will be discussed in class A constructor that takes a reference to an existing RationalNumber object and initializes the current object to be a copy of the given object A destructor, which does nothing Two functions, one returning the numerator and one returning the denominator Functions to add, subtract, multiply and divide RationalNumber objects. These will each have two forms; one form has one RationalNumber parameter and the other has two. The first form adds (subtracts, etc.) the parameter to the current object, while the second form adds (subtracts, etc.) the two parameters and stores the answer in the current object A function that takes one RationalNumber parameter and returns true if the current object is equal to the parameter, false if not A function that takes one RationalNumber parameter and returns true if the current object is less than the parameter, false if not Can anyone help ?! | |
|
|
|
| clanmjc (661) | |||
Step 1:
Step 2: implement 2 param constructor. Put forth some effort, post back what you have, we will not do it for you. | |||
|
Last edited on
|
|||
| tjmohney (7) | |||
|
//#if !defined(_RATIONAL_H) #define _RATIONAL_H class RationalNumber { public: RationalNumber(); RationalNumber(int,int); RationalNumber(double); RationalNumber(RationalNumber &); // copies an object that already exists ~RationalNumber(); int getNumerator(); int getDenominator(); void add(RationalNumber &); void add(RationalNumber &,RationalNumber &); void sub(RationalNumber &); void sub(RationalNumber &,RationalNumber &); void mul(RationalNumber &); void mul(RationalNumber &,RationalNumber &); void div(RationalNumber &); void div(RationalNumber &,RationalNumber &); bool isEqual(RationalNumber &); bool isLessThan(RationalNumber &); private: int num,den; }; #endif // ensures that its only ran once this is where im at so far... Thanks | |||
|
|
|||
| tjmohney (7) | |
|
as well as my .cc file.... #include "Rational.h" RationalNumber::RationalNumber() { num = 0; den = 1; } RationalNumber(int n,int d){ } RationalNumber(double); // stern brocott // low = 0/1, high = 0/1 // find mediant, add num and den // replace low endpoint when larger RationalNumber::RationalNumber(RationalNumber &q) { // copies an object that already exists num = q.num; den = q.den; } ~RationalNumber(); int getNumerator(); int getDenominator(); void add(RationalNumber &q) { num = q.den * num + den * q.num; den *= q.den; // *= q.den * q.den int d = prvGCD(num,den); num /= d; den /= d; } void add(RationalNumber &,RationalNumber &); void sub(RationalNumber &); void sub(RationalNumber &,RationalNumber &); void mul(RationalNumber &); void mul(RationalNumber &,RationalNumber &); void div(RationalNumber &); void div(RationalNumber &,RationalNumber &); bool isEqual(RationalNumber &); bool isLessThan(RationalNumber &); int RationalNumber::prvGCD(int a,int b) { int r; while (b !=0) { r = a % b; a = b; b = r; } return a; private: int prvGCD(int,int); int num,den; }; | |
|
|
|