fraction.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00010 /*
00011   AUTHOR(S): Darryl Kang (dk)
00012 
00013   RCS information
00014    $Name: OSB_060808 $
00015    $Revision: 1.13 $
00016 
00017   License
00018    OSB rating and billing library for communication networks
00019    Copyright (C) 2004, 2005, 2006  OSB systems
00020 
00021    This file may be distributed and/or modify under the terms of the
00022    GNU General Public License (GPL) as published by the Free Software
00023    Foundation which is provided in the file LICENSE.GPL included in the
00024    packaging of this file.
00025 
00026    The file is distributed in the hope that it will be useful, but WITHOUT
00027    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00028    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00029    for more details.
00030 
00031    Holders of a OSB Commercial License may use this file under the terms
00032    and conditions of this commercial license.
00033  */
00034 #ifndef _FRACTION_H_
00035 #define _FRACTION_H_
00036 
00037 // *********************************************************************
00038 // included header files
00039 // + standard includes
00040 #include <string>
00041 #include <iosfwd>
00042 #include <utility>
00043 #include <cmath>
00044 
00045 // + local headers
00046 #include "osberror.h"
00047 
00048 // *********************************************************************
00049 // namespace extensions
00050 namespace OSB_LIB {
00051 
00061     class Fraction {
00062     public:
00064         Fraction()
00065           : nominator_(0), denominator_(1)
00066         {
00067             // empty
00068         }
00069 
00071         Fraction(long nominator, long denominator = 1);
00072 
00074         long nominator() const { return nominator_; }
00076         long denominator() const { return denominator_; }
00078         double dbl() const
00079         {
00080             double rc = nominator_;
00081             rc /= denominator_;
00082             return rc;
00083         }
00084 
00086         std::string str() const;
00087 
00089         inline Fraction& operator-();
00091         Fraction& operator+=(const Fraction&);
00093         Fraction& operator-=(const Fraction&);
00095         Fraction& operator*=(const Fraction&);
00097         Fraction& operator/=(const Fraction&);
00098 
00099     private:
00101         void reduce();
00102 
00104         void divide(long& higher, long& lower);
00105     private:
00107         long nominator_;
00109         long denominator_;
00110     };                                  // class Fraction
00111 
00113     inline Fraction operator*(const Fraction&, const Fraction&);
00115     inline Fraction operator/(const Fraction&, const Fraction&);
00117     inline Fraction operator+(const Fraction&, const Fraction&);
00119     inline Fraction operator-(const Fraction&, const Fraction&);
00121     inline bool operator==(const Fraction& lhs, const Fraction& rhs);
00123     inline bool operator!=(const Fraction& lhs, const Fraction& rhs);
00124 
00130     std::ostream& operator<<(std::ostream& os, const Fraction& f);
00131 }                                   // namespace OSB_LIB
00132 
00133 // ************************************************************************
00134 // inline definitions
00135 namespace OSB_LIB {
00136     inline Fraction& Fraction::operator-()
00137     {
00138         nominator_ *= -1;
00139         return *this;
00140     }
00141 
00142     inline Fraction operator+(const Fraction& f1, const Fraction& f2)
00143     {
00144         Fraction r(f1);
00145         return r += f2;
00146     }
00147 
00148     inline Fraction operator-(const Fraction& f1, const Fraction& f2)
00149     {
00150         Fraction r(f1);
00151         return r -= f2;
00152     }
00153 
00154     inline Fraction operator*(const Fraction& f1, const Fraction& f2)
00155     {
00156         Fraction r(f1);
00157         return r *= f2;
00158     }
00159 
00160     inline Fraction operator/(const Fraction& f1, const Fraction& f2)
00161     {
00162         Fraction r(f1);
00163         return r /= f2;
00164     }
00165 
00166     inline bool operator==(const Fraction& lhs, const Fraction& rhs)
00167     {
00168         return (lhs.nominator()   == rhs.nominator() &&
00169                 lhs.denominator() == rhs.denominator());
00170     }
00171 
00172     inline bool operator!=(const Fraction& lhs, const Fraction& rhs)
00173     {
00174         return lhs != rhs;
00175     }
00176 }                                       // namespace OSB_LIB
00177 #endif                              // #ifndef _FRACTION_H_

Generated on Sat Sep 2 14:06:32 2006 for OSB Library by  doxygen 1.4.7