Create a class called Rational for performing arithmetic with fractions. Write a
ID: 641108 • Letter: C
Question
Create a class called Rational for performing arithmetic with fractions. Write a program to test this 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's declared. the constructor should contain default values in case no initializers are provided and should store the fraction in reduuced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator.
Provide public member frunctions that perform each of the following tasks:
a) Adding two rational numbers. The result should be stored in reduced form.
b) Subtracting two rational numbres. 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 the numerator and b is the denominator.
f) Printing rational numbers in floating point format.
g) Compute (aX + b)2 (the rational class should perform the arithmetic and the function should do the rest)
Some changes to the specification:
?
Explanation / Answer
// Exercise 9.6 Solution: Ex09_06.cpp
#include <iostream>
#include "Rational.h" // include definition of class Rational
using namespace std;
int main()
{
Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects
c.printRational(); // prints rational object c
cout << " + ";
d.printRational(); // prints rational object d
x = c.addition( d ); // adds object c and d; sets the value to x
cout << " = ";
x.printRational(); // prints rational object x
cout << ' ';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << " ";
c.printRational(); // prints rational object c
cout << " - ";
d.printRational(); // prints rational object d
x = c.subtraction( d ); // subtracts object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << ' ';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << " ";
c.printRational(); // prints rational object c
cout << " x ";
d.printRational(); // prints rational object d
x = c.multiplication( d ); // multiplies object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << ' ';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << " ";
c.printRational(); // prints rational object c
cout << " / ";
d.printRational(); // prints rational object d
x = c.division( d ); // divides object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << ' ';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << endl;
} // end main
Rational.cpp
// Member-function definitions for class Rational.
#include <iostream>
#include "Rational.h" // include definition of class Rational
using namespace std;
Rational::Rational( int n, int d )
: numerator( n ), denominator( d )
{
reduce(); // store the fraction in reduced form
} // end Rational constructor
Rational Rational::addition( const Rational &a ) const
{
Rational t( a.numerator * denominator + a.denominator * numerator,
a.denominator * denominator );
t.reduce(); // store the fraction in reduced form
return t;
} // end function addition
Rational Rational::subtraction( const Rational &s ) const
{
Rational t( s.denominator * numerator - denominator * s.numerator,
s.denominator * denominator );
t.reduce(); // store the fraction in reduced form
return t;
} // end function subtraction
Rational Rational::multiplication( const Rational &m ) const
{
Rational t( m.numerator * numerator, m.denominator * denominator );
t.reduce(); // store the fraction in reduced form
return t;
} // end function multiplication
Rational Rational::division( const Rational &v ) const
{
Rational t( v.denominator * numerator, denominator * v.numerator );
t.reduce(); // store the fraction in reduced form
return t;
} // end function division
void Rational::printRational() const
{
if ( denominator == 0 ) // validates denominator
cout << " DIVIDE BY ZERO ERROR!!!" << ' ';
else if ( numerator == 0 ) // validates numerator
cout << 0;
else
cout << numerator << '/' << denominator;
} // end function printRational
void Rational::printRationalAsDouble() const
{
cout << static_cast< double >( numerator ) / denominator;
} // end function printRationalAsDouble
void Rational::reduce()
{
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;
} // end if
} // end function reduction
Rational.h
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function addition
Rational subtraction( const Rational & ) const; // function subtraction
Rational multiplication( const Rational & ) const; // function multi.
Rational division( const Rational & ) const; // function division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as double format
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce(); // utility function
}; // end class Rational
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.