Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class.
Use integer variables to represent the private data of the class—the numerator and the denominator. Provide a constructor function 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
Provide public member functions for each of the
following:
a) Addition of two Rational numbers. The result should be stored in reduced form.
b) Subtraction of two Rational numbers. The result should be stored in reduced form.
c) Multiplication of two Rational numbers. The result should be stored in reduced form.
d) Division of two Rational numbers. The result should be stored in reduced form.
e) Printing Rational numbers in the form a/b where a is the numerator and b is the denominator.
rational.h
#ifndef rational_H
#define rational_H
#include <iostream>
class Rational {
public:
Rational( int = 0, int = 1 );// constructor
Rational addition( const Rational & );
Rational subtraction( const Rational & );
Rational multiplication( const Rational & );
Rational division( Rational & );
void printRational( void );
void printRationalAsFloating( void );
private:
int numerator;
int denominator;
void reduction( void );
};
#endif
rational.cpp
#include <iostream>
#include "rational.h"
using namespace std;
Rational::Rational( int n, int d )
{
numerator = n;
denominator = d;
}
Rational Rational::addition( const Rational &a )
{
Rational t;
t.numerator = a.numerator * denominator + a.denominator * numerator;
t.denominator = a.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::subtraction( const Rational &s )
{
Rational t;
t.numerator = s.denominator * numerator - denominator * s.numerator;
t.denominator = s.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::multiplication( const Rational &m )
{
Rational t;
t.numerator = m.numerator * numerator;
t.denominator = m.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::division( Rational &v )
{
Rational t;
t.numerator = v.denominator * numerator;
t.denominator = denominator * v.numerator;
t.reduction();
return t;
}
void Rational::printRational( void )
{
if ( denominator == 0 )
cout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
else if ( numerator == 0 )
cout << 0;
else
cout << numerator << '/' << denominator;
}
void Rational::printRationalAsFloating( void )
{ cout << static_cast< double >( numerator ) / denominator; }
void Rational::reduction( void )
{
int largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; ++loop )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}
main.cpp
#include <iostream>
#include "rational.h"
using namespace std;
int main()
{
Rational c( 1, 3 ), d( 7, 8 ), x;
c.printRational();
cout << " + ";
d.printRational();
x = c.addition( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\n\n";
c.printRational();
cout << " - ";
d.printRational();
x = c.subtraction( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\n\n";
c.printRational();
cout << " x ";
d.printRational();
x = c.multiplication( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\n\n";
c.printRational();
cout << " / ";
d.printRational();
x = c.division( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << endl;
return 0;
}
Comments
Leave a comment