Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class calledrational for performing arithmetic with fractions. Useinteg

ID: 3611298 • Letter: C

Question

Create a class calledrational for performing arithmetic with fractions. Useinteger

variables to representthe private data of the class – thenumeratorand the

denominator. Provide a constructor that enables an object ofthis class to be

initialized when it isdeclared. Your program should store the fraction inreduced

form. For example, thefraction 2/4 would be stored in the object as 1/2.

Provide publicmemberfunctions that performeach of the following tasks:

a.Multiplyingtwo rational numbers. The result should be stored inreduced

form.

b.Dividingtwo rational numbers. The resultshould be stored in reduced

form.

c.Addingtwo rational numbers. The result should be stored inreduced

form.

d.Subtractingtwo rational numbers. The result should be stored inreduced

form.

Write a program to test yourclass.

Explanation / Answer


#ifndef RATIONAL_H
#define RATIONAL_H

class rational {
public :
rational();
void get_f1(int, int);
void get_f2(int, int);
void add(rational&, int, int, int );


private :
int numer;
int denom;
int result;
int result_n;
int result_d;
};

#endif

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

using namespace std;

rational :: rational() {numer = denom = result = result_n =result_d = 0
;}

void rational :: get_f1(int numer, int denom)
{
do {
cout << "please input the first fraction's numerator: " ;
cin >> numer;
cout << "please input the first fraction's denominator:";
cin >> denom;
cout << "the fraction is: " << numer << "/"<< denom <<endl;
} while (denom == 0);
}

void rational :: get_f2(int numer, int denom)
{
do {
cout << "please input the second fraction's numerator: ";
cin >> numer;
cout << "please input the second fraction's denominator:";
cin >> denom;
cout << "the fraction is: " << numer << "/"<< denom <<endl;
} while (denom == 0);
}

void rational :: add(rational &rational_1, int result_n, intresult_d, int
result)
{
result_n = (rational_1.get_f1.numer * rational_1.get_f2.denom)+
(rational_1.get_f2.numer * rational_1.get_f1.denom); /* here i wantto add
the two fractions, but i don't know how since my teach ask me touse two
class object to implement the addition */
result_d = rational_1.get_f1.denom * rational_1.get_f2.denom;
cout << rational_1.get_f1.numer <<result_d;

if (result_n==result_d)result=1;
for (int n=1 ; n <= result_d; n++)
{
for (int t=2; t <= result_n; t++)
{
if (result_n % t == 0 && result_d % t == 0)
{
result_n = result_n/t;
result_d = result_d/t;
}
}
}
if (result_d ==1)
cout <<endl <<result_n <<endl;
else if (result_d == 0)
cout <<endl << 0;
else
cout <<endl<< result_n << "/" << result_d<<endl;
}


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

using namespace std;

int main ()
{
rational rational_1, rational_result;
int numer, denom, result, result_n, result_d;

rational_1.get_f1(numer, denom);
rational_1.get_f2(numer, denom);
rational_result.add(rational_1, result, result_n, result_d);


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote