📄 rational.cpp
字号:
// Chapter 8 of C++ How to Program
// rational.cpp
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include "rational.h"
// constructor
RationalNumber::RationalNumber( int n, int d )
{
numerator = n;
denominator = d;
/* Write function call to reduction function */
} // end class RationalNumber constructor
/* Write definition for overloaded operator+ */
// function operator- definition
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;
} // end function operator-
/* Write definition for overloaded operator * */
// function operator/ definition
const RationalNumber RationalNumber::operator/(
const RationalNumber &d )
{
RationalNumber divide;
if ( /* Write condition to test for zero numerator */ ) {
divide.numerator = numerator * d.denominator;
divide.denominator = denominator * d.numerator;
divide.reduction();
} // end if
else {
cout << "Divide by zero error: terminating program\n";
exit( 1 ); // cstdlib function
} // end else
return divide;
} // end function operator/
// function operator> definition
bool RationalNumber::operator>( const RationalNumber &gr ) const
{
// compare double values of two rational numbers
if ( static_cast< double >( numerator ) / denominator >
static_cast< double >( gr.numerator ) / gr.denominator )
return true;
else
return false;
} // end function operator>
// function operator< definition
bool RationalNumber::operator<( const RationalNumber &lr ) const
{
return !( *this > lr );
} // end function operator<
// function operator>= definition
bool RationalNumber::operator>=( const RationalNumber &rat ) const
{
return *this == rat || *this > rat;
} // end function operator>=
/* Write definition for operator <= */
/* Write definition for operator == */
/* Write definition for operator != */
// function printRational definition
void RationalNumber::printRational() const
{
if ( numerator == 0 ) // print fraction as zero
cout << numerator;
else if ( denominator == 1 ) // print fraction as integer
cout << numerator;
else
cout << numerator << '/' << denominator;
} // end function printRational
// function reduction definition
void RationalNumber::reduction()
{
int smallest;
int gcd = 1; // greatest common divisor
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;
} // end function reduction
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -