Question: Using the code posted below, the following please use the following alterations of: 1)Create a constructor that prevents a 0 denominator in a fraction, reduces

Using the code posted below, the following please use the following alterations of:

1)Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.

2)Overload the addition, subtraction, multiplication and division operators for this class.

3)Overload the relational and equality operators for this class.

Rational.h

#ifndef _RATIONAL_H_ #define _RATIONAL_H_

class Rational { private: int num; int denom;

public:

Rational(int =0, int=0); int* reducedForm(int num, int denom); Rational add(const Rational &);//read as result = ("this"+ arg) Rational subtract(const Rational &); //read as result = ("this" - arg) Rational multiply(const Rational &); //read as result = ("this" * arg) Rational divide(const Rational &); //read as result ("this" / arg) void print(); void printFloat();

};

#endif

Rational.cpp

#include "Rational.h" #include #include

Rational::Rational(int anum, int adenom){ int * reduced = reducedForm(anum, adenom); num = reduced[0]; denom = reduced[1]; delete reduced; //delete the array that was on the heap (garbage collect) }

int* Rational::reducedForm(int num, int denom){ //first get the GCD for a and b int a = num, b = denom, c; int* ans = new int[2]; //array created on heap while ( a != 0 ) { c = a; a = b%a; b = c; } //then reduce ans[0]= num/b; //reduced numerator ans[1]= denom/b; //reduced denominator return ans; }

Rational Rational::add(const Rational & r2){//read as result = (this + arg)

int numerator = (num * r2.denom) + (r2.num * denom); int denominator = denom * r2.denom; Rational sum(numerator, denominator); return sum; }

Rational Rational::subtract(const Rational & r2){ //read as result = (this - arg)

int numerator = (num * r2.denom) - (r2.num * denom); int denominator = denom * r2.denom; Rational sum(numerator, denominator); return sum;

} Rational Rational::multiply(const Rational & r2){ //read as result = (this * arg)

int numerator = (num * r2.num); int denominator = denom * r2.denom; Rational sum(numerator, denominator); return sum; }

Rational Rational::divide(const Rational & r2){ //read as result (this / arg)

int numerator = num * r2.denom; int denominator = denom * r2.num; Rational sum(numerator, denominator); return sum; }

void Rational::print(){

std::cout<< num<<"/" << denom << std::endl; }

void Rational::printFloat(){ double f = (double) num/denom; std::cout<

main.cpp

#include #include "Rational.h" using namespace std;

int main() { int numOne, denOne, numTwo, denTwo;

cout<<"Please enter the first fraction's numerator and denominator"<>numOne; cin>>denOne; Rational r1(numOne, denOne); r1.print(); r1.printFloat(); cout<<"============================="<

cout<<"Pleae enter the second fraction's numerator and denominaytor"<>numTwo; cin>>denTwo; Rational r2(numTwo,denTwo); r2.print(); r2.printFloat(); cout<<"============================="<

cout<<"The addition is: "<

cout<<"The subtraction is: "<

cout<<"The multiplication is: "<

cout <<"The division is: " <

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!