Thursday, November 15, 2012

C++ classes: Rational

____________________________________

First: Header file.h (functions prototype).

____________________________________

// File: Rational.h
// Rational class definition


#ifndef RATIONAL_H            // used to avoid multiple definitions
#define RATIONAL_H            //    not part of the class
class rational
{
   public:
    // Member functions
    // Constructors

    rational();            // Default Costructor
    rational(int);        // Initialize numerator with denom = 1
    rational(int, int);    // Initialize both numerator and denom.

    void setNum(int);    // Set numerator and denominator
    void setDenom(int);
   
    rational multiply(const rational &f);    // Multiply fractions
    rational divide(const rational &f);        // Divide fractions
    rational add(const rational &f);        // Add Fractions
    rational subtract(const rational &f);    // Subtract Fractions
    void readRational();                    // Read a fraction
    void displayRational() const;            // Display a fraction
    rational reduce() const;                // Reduce rational

    // Accessors
    int getNum() const;
    int getDenom() const;

    // Operator Style
    rational operator + (const rational &); // Add object to parameter
    bool operator == (const rational &);    // Test equality of object and parameter

   private:
    // Data members (attributes)
    int num;    //  private data field
    int denom;    // private data field

};    // Note -- a class definition MUST end with a semicolon

#endif // RATIONAL_H


________________________________________________ 

second: Implementation file.cpp 

_____________________________________

 // File: Rational.cpp
// Rational class implementation


#include "rational.h"
#include <iostream>
using namespace std;

// Member functions
// Constructors

rational::rational()
{
   num = 0;
   denom = 0;
}

rational::rational(int n)
{
   num = n;
   denom = 1;
}

rational::rational(int n, int d)
{
   num = n;
   denom = d;
}

// Set numerator and denominator
void rational::setNum(int n)
{
   num = n;
}

void rational::setDenom(int d)
{
   denom = d;
}
// Multiply fractions
rational rational::multiply(const rational &f)
{
   rational temp(num * f.num, denom * f.denom);
   return temp;
}

// Divide fractions
rational rational::divide(const rational &f)
{
   rational temp(num * f.denom, denom * f.num);
   return temp;
}

// Add fractions
rational rational::add(const rational &f)
{
   rational temp(num * f.denom + f.num * denom,
                 denom * f.denom);
   return temp;
}

// Subtract Fractions
rational rational::subtract(const rational &f) {
   rational temp(num * f.denom - f.num * denom,
                 denom * f.denom);
   return temp;
}

// Read a fraction
void rational::readRational()
{
   char slash;       // storage for /
   do
   {
      cout << "Enter numerator / denominator: ";
      cin >> num >> slash >> denom;
   }
   while (slash != '/');
}

// Display a fraction
void rational::displayRational() const
{
   cout << num << '/' << denom;
}

// Reduce rational
rational rational::reduce() const
{
   int n,m,rem,gcd;

    // Get the two integers
    n = abs(num);  m = abs(denom);
    while (n > 0)
    {
        rem = m % n;
        m = n;
        n = rem;
    }
    gcd = m;
    rational g(num/gcd,    denom/gcd);
    return g;
}

// Accessors
int rational::getNum() const
{
   return num;
}

int rational::getDenom() const
{
   return denom;
}

rational rational::operator + (const rational &f2)   // IN: right-operand
{
   rational temp(num * f2.denom + f2.num * denom,
                 denom * f2.denom);
   return temp;
}

bool rational::operator == (const rational &f)
{
   return(num == f.num && denom == f.denom);
}
________________________________________________ 

finally class test

_____________________________________

// File: RationalTest.cpp
// Tests the rational class


#include <iostream>
#include "rational.h"
#include "rational.cpp"
using namespace std;

int main()
{
    rational f1, f2;
    rational f3;

    // Read two rational numbers
    cout << "Enter 1st fraction:" << endl;
    f1.readRational();
    cout << "Enter 2nd fraction:" << endl;
    f2.readRational();

    // Fraction Arithmetic

    f3 = f1.multiply(f2);
    f1.displayRational(); cout << " * ";
    f2.displayRational(); cout << " = ";
    f3.displayRational();
    cout << " = ";
    f3 = f3.reduce();
    f3.displayRational(); cout << endl;


    f3 = f1.divide(f2);
    f1.displayRational(); cout << " / ";
    f2.displayRational(); cout << " = ";
    f3.displayRational();
    cout << " = ";
    f3 = f3.reduce();
    f3.displayRational(); cout << endl;


    f3 = f1.add(f2);
    f1.displayRational(); cout << " + ";
    f2.displayRational(); cout << " = ";
    f3.displayRational();
    cout << " = ";
    f3 = f3.reduce();
    f3.displayRational(); cout << endl;


    f3 = f1 + f2;
    f1.displayRational(); cout << " + ";
    f2.displayRational(); cout << " = ";
    f3.displayRational();
    cout << " = ";
    f3 = f3.reduce();
    f3.displayRational(); cout << endl;


    f3 = f1.subtract(f2);
    f1.displayRational(); cout << " - ";
    f2.displayRational(); cout << " = ";
    f3.displayRational();
    cout << " = ";
    f3 = f3.reduce();
    f3.displayRational(); cout << endl;


    return 0;

}

No comments:

Post a Comment