 arvie21Newbie
 Posts : 23
Join date : 20091204
rational
on Tue Sep 07, 2010 7:07 am
/******************************************************
Name:
Filename: RationalNum.h
******************************************************/
#ifndef RATIONALNUM_H
#define RATIONALNUM_H
class RationalNumber {
public:
RationalNumber( int = 0, int = 1 );
const RationalNumber operator+( const RationalNumber& );
const RationalNumber operator( const RationalNumber& );
const RationalNumber operator*( const RationalNumber& );
const RationalNumber operator/( RationalNumber& );
bool operator>( const RationalNumber& ) const;
bool operator<( const RationalNumber& ) const;
bool operator>=( const RationalNumber& ) const;
bool operator<=( const RationalNumber& ) const;
bool operator==( const RationalNumber& ) const;
bool operator!=( const RationalNumber& ) const;
void printRational( void ) const;
private:
int numerator;
int denominator;
void reduction( void );
};
#endif

/******************************************************
Name:
Filename: RationalNum.cpp
******************************************************/
#include
#include
#include "RationalNum.h"
using namespace std;
RationalNumber::RationalNumber( int n, int d )
{
numerator = n;
denominator = d;
reduction();
}
const RationalNumber RationalNumber::operator+(
const RationalNumber &s )
{
RationalNumber sum;
sum.numerator = numerator * s.denominator + denominator *
s.numerator;
sum.denominator = denominator * s.denominator;
sum.reduction();
return sum;
}
const RationalNumber RationalNumber::operator(
const RationalNumber &s )
{
RationalNumber sub;
sub.numerator = numerator * s.denominator  denominator *
s.numerator;
sub.denominator = denominator * s.denominator;
sub.reduction();
return sub;
}
const RationalNumber RationalNumber::operator*(const RationalNumber &m)
{
RationalNumber multiply;
multiply.numerator = numerator * m.numerator;
multiply.denominator = denominator * m.denominator;
multiply.reduction();
return multiply;
}
const RationalNumber RationalNumber::operator/( RationalNumber &d )
{
RationalNumber divide;
if ( d.numerator !=0 ) {
divide.numerator = numerator * d.denominator;
divide.denominator = denominator * d.numerator;
divide.reduction();
}
else {
cout << "Divide by zero error: terminating program\n";
exit( 1 );
}
return divide;
}
bool RationalNumber::operator>(const RationalNumber &gr ) const
{
if ( static_cast< double >( numerator ) / denominator >
static_cast< double >( gr.numerator ) / gr.denominator )
return true;
else
return false;
}
bool RationalNumber::operator<(const RationalNumber &lr ) const
{
return !(*this > lr);
}
bool RationalNumber::operator>=(
const RationalNumber &rat ) const
{ return *this == rat  *this > rat; }
bool RationalNumber::operator<=(
const RationalNumber &rat ) const
{ return *this == rat  *this < rat;}
bool RationalNumber::operator==(
const RationalNumber &rat ) const
{ if (numerator == rat.numerator &&
denominator == rat.denominator)
return true;
else
return false;
}
bool RationalNumber::operator!=(
const RationalNumber &rat ) const
{
return !(*this == rat);
}
void RationalNumber::printRational( void ) const
{
if ( numerator == 0 ) // Fraction as zero
cout << numerator;
else if ( denominator == 1 ) // Fraction as integer
cout << numerator;
else
cout << numerator << '/' << denominator;
}
void RationalNumber::reduction( void )
{
int smallest, gcd = 1; // GCD
smallest = ( numerator < denominator ) ? numerator
: denominator;
for ( int loop = 2; loop <= smallest; ++loop )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
numerator /= gcd;
denominator /= gcd;
}

/******************************************************
** Name:
** Filename: RationalNumDrv.cpp
** Project #: Deitel & Deitel 8.17
** Project Description: Create a class RationalNumber
(fractions) with the following capabilities:
a)Create a constructor that prevents a 0
denominator in a fraction, reduces or
simplifies fractions that are not in reduced
form and avoids negative denominators.
b)Overload the addition, subtraction,
multiplication and division operators for this
class.
c)Overload the relational and quality operators
for this class.
** Output: The result of the first and second fraction
using: Addition
Subtraction
Multiplication
Division
> or <
>= or <=
!= or ==
** Input: First fraction numerator and denominator
Second fraction numerator and denominator
** Algorithm: Create class RationalNumber.cpp with operator all
operator functions.
Create header file RationalNumber.h
Print out results using RationalNumberDrv.cpp
Ask user for first fraction
Ask user for second fraction
Use operator+ produce result
Use operator produce result
Use operator* produce result
Use operator/ produce result
Use operator> and operator< produce result
Use operator>= and operator<= produce result
Use operator!= and operator== produce result
Print result of all operator functions
End program
******************************************************/
// Include files
#include
#include
#include "RationalNum.h"
using namespace std;
// Global Type Declarations
// Function Prototypes
void instruct (void);
void pause ();
//Global Variables  should not be used without good reason.
int main()
{
// Declaration section
int a,
b,
c,
d;
// Executable section
instruct ();
cout << "Enter first fraction numerator: ";
cin >> a;
cout << " ";
cout << "\nEnter first fraction denominator: ";
cin >> b;
cout << "\nEnter second fraction numerator: ";
cin >> c;
cout << " ";
cout << "\nEnter second fraction denominator: ";
cin >> d;
cout << "\n";
RationalNumber x( a, b ), y( c, d ), z;
x.printRational();
cout << " + " ;
y.printRational();
cout << " = ";
z = x + y;
z.printRational();
cout << '\n';
x.printRational();
cout << "  " ;
y.printRational();
cout << " = ";
z = x  y;
z.printRational();
cout << '\n';
x.printRational();
cout << " * " ;
y.printRational();
cout << " = ";
z = x * y;
z.printRational();
cout << '\n';
x.printRational();
cout << " / " ;
y.printRational();
cout << " = ";
z = x / y;
z.printRational();
cout << "\n\n";
x.printRational();
cout << " is:";
cout << "\t";
cout << ( ( x > y ) ? " > " : " <= " );
y.printRational();
cout << " according to the overloaded > operator\n";
cout << "\t";
cout << ( ( x < y ) ? " < " : " >= " );
y.printRational();
cout << " according to the overloaded < operator\n";
cout << "\t";
cout << ( ( x >= y ) ? " >= " : " < " );
y.printRational();
cout << " according to the overloaded >= operator\n";
cout << "\t";
cout << ( ( x <= y ) ? " <= " : " > " );
y.printRational();
cout << " according to the overloaded <= operator\n";
cout << "\t";
cout << ( ( x == y ) ? " == " : " != " );
y.printRational();
cout << " according to the overloaded == operator\n";
cout << "\t";
cout << ( ( x != y ) ? " != " : " == " );
y.printRational();
cout << " according to the overloaded != operator" << endl;
pause();
return 0;
}
void instruct (void)
{
// Declaration section
cout << "Display the use of class RationalNumber to ";
cout << "perform various operator functions.\n";
cout << "___________________________________________";
cout << "___________________________________\n\n";
// Executable section
}
void pause ()
{
// Declaration section
// Executable section
cout << "\nPress any key to continue...";
getch();
cout << "\r";
cout << " ";
cout << "\r";
}
/*
Program Output
Display the use of class RationalNumber to perform various operator functions.
______________________________________________________________________________
Enter first fraction numerator: 2

Enter first fraction denominator: 3
Enter second fraction numerator: 1

Enter second fraction denominator: 2
2/3 + 1/2 = 7/6
2/3  1/2 = 1/6
2/3 * 1/2 = 1/3
2/3 / 1/2 = 4/3
2/3 is: > 1/2 according to the overloaded > operator
>= 1/2 according to the overloaded < operator
>= 1/2 according to the overloaded >= operator
> 1/2 according to the overloaded <= operator
!= 1/2 according to the overloaded == operator
!= 1/2 according to the overloaded != operator
Enter first fraction numerator: 1

Enter first fraction denominator: 5
Enter second fraction numerator: 1

Enter second fraction denominator: 5
1/5 + 1/5 = 2/5
1/5  1/5 = 0
1/5 * 1/5 = 1/25
1/5 / 1/5 = 1
1/5 is: <= 1/5 according to the overloaded > operator
< 1/5 according to the overloaded < operator
>= 1/5 according to the overloaded >= operator
<= 1/5 according to the overloaded <= operator
== 1/5 according to the overloaded == operator
== 1/5 according to the overloaded != operator
Press any key to continue...
*/
Name:
Filename: RationalNum.h
******************************************************/
#ifndef RATIONALNUM_H
#define RATIONALNUM_H
class RationalNumber {
public:
RationalNumber( int = 0, int = 1 );
const RationalNumber operator+( const RationalNumber& );
const RationalNumber operator( const RationalNumber& );
const RationalNumber operator*( const RationalNumber& );
const RationalNumber operator/( RationalNumber& );
bool operator>( const RationalNumber& ) const;
bool operator<( const RationalNumber& ) const;
bool operator>=( const RationalNumber& ) const;
bool operator<=( const RationalNumber& ) const;
bool operator==( const RationalNumber& ) const;
bool operator!=( const RationalNumber& ) const;
void printRational( void ) const;
private:
int numerator;
int denominator;
void reduction( void );
};
#endif

/******************************************************
Name:
Filename: RationalNum.cpp
******************************************************/
#include
#include
#include "RationalNum.h"
using namespace std;
RationalNumber::RationalNumber( int n, int d )
{
numerator = n;
denominator = d;
reduction();
}
const RationalNumber RationalNumber::operator+(
const RationalNumber &s )
{
RationalNumber sum;
sum.numerator = numerator * s.denominator + denominator *
s.numerator;
sum.denominator = denominator * s.denominator;
sum.reduction();
return sum;
}
const RationalNumber RationalNumber::operator(
const RationalNumber &s )
{
RationalNumber sub;
sub.numerator = numerator * s.denominator  denominator *
s.numerator;
sub.denominator = denominator * s.denominator;
sub.reduction();
return sub;
}
const RationalNumber RationalNumber::operator*(const RationalNumber &m)
{
RationalNumber multiply;
multiply.numerator = numerator * m.numerator;
multiply.denominator = denominator * m.denominator;
multiply.reduction();
return multiply;
}
const RationalNumber RationalNumber::operator/( RationalNumber &d )
{
RationalNumber divide;
if ( d.numerator !=0 ) {
divide.numerator = numerator * d.denominator;
divide.denominator = denominator * d.numerator;
divide.reduction();
}
else {
cout << "Divide by zero error: terminating program\n";
exit( 1 );
}
return divide;
}
bool RationalNumber::operator>(const RationalNumber &gr ) const
{
if ( static_cast< double >( numerator ) / denominator >
static_cast< double >( gr.numerator ) / gr.denominator )
return true;
else
return false;
}
bool RationalNumber::operator<(const RationalNumber &lr ) const
{
return !(*this > lr);
}
bool RationalNumber::operator>=(
const RationalNumber &rat ) const
{ return *this == rat  *this > rat; }
bool RationalNumber::operator<=(
const RationalNumber &rat ) const
{ return *this == rat  *this < rat;}
bool RationalNumber::operator==(
const RationalNumber &rat ) const
{ if (numerator == rat.numerator &&
denominator == rat.denominator)
return true;
else
return false;
}
bool RationalNumber::operator!=(
const RationalNumber &rat ) const
{
return !(*this == rat);
}
void RationalNumber::printRational( void ) const
{
if ( numerator == 0 ) // Fraction as zero
cout << numerator;
else if ( denominator == 1 ) // Fraction as integer
cout << numerator;
else
cout << numerator << '/' << denominator;
}
void RationalNumber::reduction( void )
{
int smallest, gcd = 1; // GCD
smallest = ( numerator < denominator ) ? numerator
: denominator;
for ( int loop = 2; loop <= smallest; ++loop )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
numerator /= gcd;
denominator /= gcd;
}

/******************************************************
** Name:
** Filename: RationalNumDrv.cpp
** Project #: Deitel & Deitel 8.17
** Project Description: Create a class RationalNumber
(fractions) with the following capabilities:
a)Create a constructor that prevents a 0
denominator in a fraction, reduces or
simplifies fractions that are not in reduced
form and avoids negative denominators.
b)Overload the addition, subtraction,
multiplication and division operators for this
class.
c)Overload the relational and quality operators
for this class.
** Output: The result of the first and second fraction
using: Addition
Subtraction
Multiplication
Division
> or <
>= or <=
!= or ==
** Input: First fraction numerator and denominator
Second fraction numerator and denominator
** Algorithm: Create class RationalNumber.cpp with operator all
operator functions.
Create header file RationalNumber.h
Print out results using RationalNumberDrv.cpp
Ask user for first fraction
Ask user for second fraction
Use operator+ produce result
Use operator produce result
Use operator* produce result
Use operator/ produce result
Use operator> and operator< produce result
Use operator>= and operator<= produce result
Use operator!= and operator== produce result
Print result of all operator functions
End program
******************************************************/
// Include files
#include
#include
#include "RationalNum.h"
using namespace std;
// Global Type Declarations
// Function Prototypes
void instruct (void);
void pause ();
//Global Variables  should not be used without good reason.
int main()
{
// Declaration section
int a,
b,
c,
d;
// Executable section
instruct ();
cout << "Enter first fraction numerator: ";
cin >> a;
cout << " ";
cout << "\nEnter first fraction denominator: ";
cin >> b;
cout << "\nEnter second fraction numerator: ";
cin >> c;
cout << " ";
cout << "\nEnter second fraction denominator: ";
cin >> d;
cout << "\n";
RationalNumber x( a, b ), y( c, d ), z;
x.printRational();
cout << " + " ;
y.printRational();
cout << " = ";
z = x + y;
z.printRational();
cout << '\n';
x.printRational();
cout << "  " ;
y.printRational();
cout << " = ";
z = x  y;
z.printRational();
cout << '\n';
x.printRational();
cout << " * " ;
y.printRational();
cout << " = ";
z = x * y;
z.printRational();
cout << '\n';
x.printRational();
cout << " / " ;
y.printRational();
cout << " = ";
z = x / y;
z.printRational();
cout << "\n\n";
x.printRational();
cout << " is:";
cout << "\t";
cout << ( ( x > y ) ? " > " : " <= " );
y.printRational();
cout << " according to the overloaded > operator\n";
cout << "\t";
cout << ( ( x < y ) ? " < " : " >= " );
y.printRational();
cout << " according to the overloaded < operator\n";
cout << "\t";
cout << ( ( x >= y ) ? " >= " : " < " );
y.printRational();
cout << " according to the overloaded >= operator\n";
cout << "\t";
cout << ( ( x <= y ) ? " <= " : " > " );
y.printRational();
cout << " according to the overloaded <= operator\n";
cout << "\t";
cout << ( ( x == y ) ? " == " : " != " );
y.printRational();
cout << " according to the overloaded == operator\n";
cout << "\t";
cout << ( ( x != y ) ? " != " : " == " );
y.printRational();
cout << " according to the overloaded != operator" << endl;
pause();
return 0;
}
void instruct (void)
{
// Declaration section
cout << "Display the use of class RationalNumber to ";
cout << "perform various operator functions.\n";
cout << "___________________________________________";
cout << "___________________________________\n\n";
// Executable section
}
void pause ()
{
// Declaration section
// Executable section
cout << "\nPress any key to continue...";
getch();
cout << "\r";
cout << " ";
cout << "\r";
}
/*
Program Output
Display the use of class RationalNumber to perform various operator functions.
______________________________________________________________________________
Enter first fraction numerator: 2

Enter first fraction denominator: 3
Enter second fraction numerator: 1

Enter second fraction denominator: 2
2/3 + 1/2 = 7/6
2/3  1/2 = 1/6
2/3 * 1/2 = 1/3
2/3 / 1/2 = 4/3
2/3 is: > 1/2 according to the overloaded > operator
>= 1/2 according to the overloaded < operator
>= 1/2 according to the overloaded >= operator
> 1/2 according to the overloaded <= operator
!= 1/2 according to the overloaded == operator
!= 1/2 according to the overloaded != operator
Enter first fraction numerator: 1

Enter first fraction denominator: 5
Enter second fraction numerator: 1

Enter second fraction denominator: 5
1/5 + 1/5 = 2/5
1/5  1/5 = 0
1/5 * 1/5 = 1/25
1/5 / 1/5 = 1
1/5 is: <= 1/5 according to the overloaded > operator
< 1/5 according to the overloaded < operator
>= 1/5 according to the overloaded >= operator
<= 1/5 according to the overloaded <= operator
== 1/5 according to the overloaded == operator
== 1/5 according to the overloaded != operator
Press any key to continue...
*/

Permissions in this forum:
You cannot reply to topics in this forum

