Main Page | Modules | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | Related Pages

TRational.h

00001 /*
00002   Copyright © Grame 2003
00003 
00004   This library is free software; you can redistribute it and modify it under
00005   the terms of the GNU Library General Public License as published by the
00006   Free Software Foundation version 2 of the License, or any later version.
00007 
00008   This library is distributed in the hope that it will be useful, but
00009   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00010   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License
00011   for more details.
00012 
00013   You should have received a copy of the GNU Library General Public License
00014   along with this library; if not, write to the Free Software
00015   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016 
00017   Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
00018   grame@grame.fr
00019   
00020 */
00021 
00022 #ifndef __TRational__
00023 #define __TRational__
00024 
00025 #include <string>
00026 #include "exports.h"
00027 
00028 using namespace std;
00029 
00034 class EXP TRational {
00035 
00036    private:    
00037         long int fNumerator;
00038         long int fDenominator;        
00039         
00041         long int gcd(long int a, long int b); 
00042   
00043     public:    
00044         TRational(long int num = 0, long int denom = 1);
00045         TRational(const TRational& d);
00046         TRational(const string &str);
00047     
00048         long int getNumerator() const       { return fNumerator; }
00049         long int getDenominator() const     { return fDenominator; }
00050         void setNumerator(long int d)       { fNumerator = d; }
00051         void setDenominator(long int d)     { fDenominator = d; }
00052         void set( long int n, long int d )  { fNumerator = n; fDenominator = d; }
00053 
00054         TRational operator +(const TRational &dur) const;
00055         TRational operator -(const TRational &dur) const;
00057         TRational operator *(const TRational &dur) const; 
00058         TRational operator /(const TRational &dur) const;
00059         // (i.e. dur * 3/2 or dur * 7/4)
00060   
00061         TRational operator *(int num) const; 
00062         TRational operator /(int num) const;
00063   
00064         TRational& operator +=(const TRational &dur);
00065         TRational& operator -=(const TRational &dur);
00067         TRational& operator *=(const TRational &dur); 
00068         TRational& operator /=(const TRational &dur);
00069        // (i.e. dur * 3/2 or dur * 7/4)
00070 
00071         TRational& operator *=(long int num) { fNumerator *= num; return *this; }
00072         TRational& operator /=(long int num) { fDenominator *= num; return *this; }
00073  
00074         TRational& operator =(const TRational& dur);
00075     
00076         bool operator >(const TRational &dur) const;
00077         bool operator >=(const TRational &dur) const    {return ! (*this < dur);}
00078         bool operator <(const TRational &dur) const;
00079         bool operator <=(const TRational &dur) const    {return ! (*this > dur);}
00080             
00081         bool operator ==(const TRational &dur) const;
00082         bool operator !=(const TRational &dur) const    {return ! (*this == dur);}
00083       
00084         bool operator > (double num) const;
00085         bool operator >=(double num) const;
00086         bool operator < (double num) const;
00087         bool operator <=(double num) const;    
00088         bool operator ==(double) const;
00089 
00090         // Used to "rationalise" TRational.
00091         void rationalise();
00092        
00093         operator string () const;
00094         operator double () const;
00095         operator float () const;
00096         operator int () const; 
00097 
00098         virtual string toString() const;
00099         double  toDouble() const;
00100         float   toFloat() const;
00101         int     toInt() const;
00102 };
00103 
00104 #endif
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 

Generated on Tue Mar 23 09:49:43 2004 for LibMusicXML by doxygen 1.3.3