S
surrealtrauma
the requirement is :
Create a class called Rational (rational.h) for performing arithmetic with
fractions. Write a program to test your class.
Use Integer variables to represent the private data of the class – the
numerator and the denominator. Provide a constructor that enables an
object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form. For example, the
fraction 2/4 should be stored in the object as 1 in the numerator and 2 in
the denominator. Provide public member functions that perform each of the
following tasks:
a) Adding two Rational numbers. The result should be stored in reduced
form.
b) Subtracting two Rational numbers. The result should be stored in
reduced form.
c) Multiplying two Rational numbers. The result should be stored in
reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced
form.
e) Printing Rational numbers in the form a/b, where a is numerator and b
is denominator.
f) Printing Rational numbers in floating-point format.
I have writen the code like the following:
#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, int result_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 want to add
the two fractions, but i don't know how since my teach ask me to use 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;
}
Create a class called Rational (rational.h) for performing arithmetic with
fractions. Write a program to test your class.
Use Integer variables to represent the private data of the class – the
numerator and the denominator. Provide a constructor that enables an
object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form. For example, the
fraction 2/4 should be stored in the object as 1 in the numerator and 2 in
the denominator. Provide public member functions that perform each of the
following tasks:
a) Adding two Rational numbers. The result should be stored in reduced
form.
b) Subtracting two Rational numbers. The result should be stored in
reduced form.
c) Multiplying two Rational numbers. The result should be stored in
reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced
form.
e) Printing Rational numbers in the form a/b, where a is numerator and b
is denominator.
f) Printing Rational numbers in floating-point format.
I have writen the code like the following:
#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, int result_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 want to add
the two fractions, but i don't know how since my teach ask me to use 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;
}