Rational Numbers

1. Create a class called Rational for performing arithmetic with fractions.
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 would be stored the object 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 by the function called addition. The result should be in reduced form.
b. Subtracting two Rational numbers by the function called subtraction. The result should be in reduced form.
c. Multiplying two Rational numbers by the function called multiplication. The result should be in reduced form.
d. Dividing two Rational numbers by the function called division. The result should be 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.

Have user input the the 2 rational numbers.


I am new to c++. Any help will be appreciated.
Last edited on
Help with what?
3 things to google:
1. "C++ classes"
2. "C++ operator overloading"
Last edited on
Here's the code that I have so far. I am struggling to write a main function that would allow user to input the rational numbers. Any help would be appreciated

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;
}

Last edited on
I think you've done the hard bit, just ask the user for the numbers and pass them into your constructors.
http://www.cplusplus.com/doc/tutorial/basic_io/
Can you edit your post to use code tags?
http://www.cplusplus.com/articles/z13hAqkS/
mutexe - how do I do that? Please help
Please read the linked tutorial; it explains how to do it with examples.
What Zhuge said. Just read that link I posted.
I am still struggling. Thanks for the help though
Well your code:
Rational c( 1, 3 ), d( 7, 8 ), x;

instead of passing in hard-coded numbers, use the link i posted to populate some variables with ints and them pass them in.
Topic archived. No new replies allowed.