Fraction Library
A C++ Fraction library.
|
00001 00007 #ifndef FRACTION_H 00008 #define FRACTION_H 00009 00010 #include "PartialLib.h" 00011 00012 #if !defined(FRACTION_ONLY) || !defined(MFRACTION_ONLY) 00013 00014 #include <iostream> 00015 #include "FracError.h" 00016 using namespace std; 00017 00042 class Fraction 00043 { 00044 #ifndef MFRACTION_ONLY 00045 00046 public: 00047 00050 enum FracFormat 00051 { 00052 IM_FRAC = 0, 00053 DECI = 1 00054 }; 00055 00056 private: 00057 00059 00061 static FracFormat FORMAT; 00062 00063 public: 00064 00065 //static functions to set and get the %Fraction format 00066 //static functions cant access non-static variables 00067 00073 static void setFormat(const FracFormat &format); 00074 00079 static FracFormat getFormat(); 00080 00081 #endif /* #ifndef MFRACTION_ONLY */ 00082 00083 protected: 00084 /* THE DATA MEMBERS: */ 00085 //protected allows only %Fraction class and classes tht inherites from %Fraction class access 00086 00088 bool sign; 00089 unsigned int numerator; 00090 unsigned int denominator; 00091 00092 private: 00093 /* HELPER METHODS PRIVATE TO CLASS */ 00094 00099 unsigned int getGCD() const; 00100 00102 void reduce(); 00103 00104 public: 00105 00110 00112 00115 Fraction(); 00116 00121 Fraction(const double &number); 00122 00130 Fraction(const int &numerator, const int &denominator); 00131 00132 #ifndef MFRACTION_ONLY 00133 00141 Fraction(const char *frac); 00142 00143 #endif /* #ifndef MFRACTION_ONLY */ 00144 00149 Fraction(const Fraction &frac); 00150 00152 00153 ~Fraction(); 00154 00156 00163 00165 00166 void setNum(int numerator); 00167 00169 00172 void setDen(int denominator); 00173 00175 00184 00186 00194 int operator [] (const unsigned int &subscript) const; 00195 00197 00204 Fraction &operator = (const Fraction& right); 00205 00206 #ifndef MFRACTION_ONLY 00207 00214 Fraction operator - () const; 00215 00222 Fraction operator ++ (); 00223 00230 Fraction operator -- (); 00231 00243 Fraction operator ++ (int inc); 00244 00256 Fraction operator -- (int dec); 00257 00259 00268 Fraction operator += (const Fraction &right); 00269 00281 Fraction operator -= (const Fraction &right); 00282 00294 Fraction operator *= (const Fraction &right); 00295 00307 Fraction operator /= (const Fraction &right); 00308 00309 #endif /* #ifndef MFRACTION_ONLY */ 00310 00312 00313 #ifndef MFRACTION_ONLY 00314 00315 /* FRIENDS: OVERLOADED OPERATORS */ 00316 00323 00325 00329 friend bool operator == (const Fraction &left, const Fraction &right); 00330 00332 00336 friend bool operator != (const Fraction &left, const Fraction &right); 00337 00339 00343 friend bool operator < (const Fraction &left, const Fraction &right); 00344 00352 friend bool operator <= (const Fraction &left, const Fraction &right); 00353 00361 friend bool operator > (const Fraction &left, const Fraction &right); 00362 00370 friend bool operator >= (const Fraction &left, const Fraction &right); 00371 00373 00381 00387 friend Fraction operator + (const Fraction &left, const Fraction &right); 00388 00394 friend Fraction operator - (const Fraction &left, const Fraction &right); 00395 00401 friend Fraction operator * (const Fraction &left, const Fraction &right); 00402 00408 friend Fraction operator / (const Fraction &left, const Fraction &right); 00409 00411 00414 00420 friend ostream &operator << (ostream &out, const Fraction &fraction); 00421 00427 friend istream &operator >> (istream &in, Fraction &fraction); 00428 00430 00431 #endif /* #ifndef MFRACTION_ONLY */ 00432 }; 00433 #endif /* #if !defined(FRACTION_ONLY) || !defined(MFRACTION_ONLY) */ 00434 #endif /* #ifndef FRACTION_H */