I\'m not really sure what she is asking on this....... Using class RationalNumbe
ID: 3621748 • Letter: I
Question
I'm not really sure what she is asking on this.......Using class RationalNumbers, implement sum, product, division and LeastCommonMultiple of two rational numbers. Hint: the data members must at least have
numerator and denominator
Also, the member functions should be such that if r1 and r2 are two objects of the RationalNumbers, then r1.Product(r2) should return an object (say r3) of type RationalNumbers
such that r3 is the product of r1 and r2
This is a multifile program.
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) I assume by "multifile" you mean that there should be a header with the class declaration and a .cpp with the actual implementation. I also put a main to just test a few of the basic functions. /*******************************************************/ /* RationalNumbers.h */ /*******************************************************/ #ifndef _RATIONALNUMBERS_H #define _RATIONALNUMBERS_H class RationalNumbers { public: int numerator; int denominator; RationalNumbers Sum(RationalNumbers r2); RationalNumbers Product(RationalNumbers r2); RationalNumbers Division(RationalNumbers r2); RationalNumbers LeastCommonMultiple(RationalNumbers r2); RationalNumbers(); RationalNumbers(int number); RationalNumbers(int numerator, int denominator); void Reduce(); private: int GreatestCommonDivisor(int a, int b); }; #endif /*************************************************************/ /* RationalNumbers.cpp */ /*************************************************************/ #include "RationalNumbers.h" RationalNumbers RationalNumbers::Sum(RationalNumbers r2) { RationalNumbers r3, r4; r3.numerator = this->numerator * r2.denominator; r3.denominator = this->denominator * r2.denominator; r4.numerator = r2.numerator * this->denominator; r4.denominator = r2.denominator * this->denominator; r3.numerator += r4.numerator; r3.Reduce(); return r3; } RationalNumbers RationalNumbers::Product(RationalNumbers r2) { RationalNumbers r3; r3.numerator = r2.numerator * this->numerator; r3.denominator = r2.denominator * this->denominator; r3.Reduce(); return r3; } RationalNumbers RationalNumbers::Division(RationalNumbers r2) { RationalNumbers r3; r3.numerator = r2.denominator; r3.denominator = r2.numerator; return this->Product(r3); } RationalNumbers RationalNumbers::LeastCommonMultiple(RationalNumbers r2) { RationalNumbers r3; RationalNumbers temp; // if either of the numbers is 0, return 0 if (r2.numerator == 0 || this->numerator == 0) return r3; // otherwise, this is a simple way to find the least common multiple of fractional numbers temp = this->Division(r2); temp.denominator = 1; r3 = r2.Product(temp); r3.Reduce(); return r3; } RationalNumbers::RationalNumbers() { this->numerator = 0; this->denominator = 0; } RationalNumbers::RationalNumbers(int number) { this->numerator = number; this->denominator = 1; } RationalNumbers::RationalNumbers(int numerator, int denominator) { this->numerator = numerator; this->denominator = denominator; } void RationalNumbers::Reduce() { int x = 0; x = this->GreatestCommonDivisor(this->numerator, this->denominator); // if the greatest common divisor is 1, then this is already reduced while (x != 1) { this->numerator /= x; this->denominator /= x; x= this->GreatestCommonDivisor(this->numerator, this->denominator); } } int RationalNumbers::GreatestCommonDivisor(int a, int b) { int x; if (a > b) { x = b; } else { x = a; } for (int i = x; i > 0; i--) { if (a % i == 0 && b % i == 0) return i; } } /*******************************************************/ /* main.cpp */ /*******************************************************/ #include #include "RationalNumbers.h" using namespace std; int main() { RationalNumbers r1(5, 2), r2(2, 5), r3(3, 4); RationalNumbers temp; temp = r1.Product(r2); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.