00001
00002
00019 #ifndef rational_h
00020 #define rational_h // evita la inclusión múltiple
00021
00022 #ifdef __BORLANDC__
00023 #include "bool.h"
00024 #include <iostream.h>
00025 class ostream; class istream;
00026 #endif
00027
00028 #ifdef _MSC_VER
00029 #if (_MSC_VER >= 1300)
00030
00031 namespace std {}
00032 #include <iostream>
00033 using namespace std;
00034 #endif
00035
00036 #if (_MSC_VER < 1300)
00037
00038 #include <iostream.h>
00039 #endif
00040 #endif
00041
00047 class rational {
00048 private:
00049 long m_num;
00050 long m_den;
00051
00052 void simplifique();
00053
00054 public:
00055
00056 rational() : m_num(0), m_den(1) { }
00057 rational(long num) : m_num(num), m_den(1) { }
00058 rational(long num, long den)
00059 : m_num(num), m_den(den) { simplifique(); }
00060 rational(const rational& o)
00061 { m_num = o.m_num, m_den = o.m_den; }
00062 ~rational() { }
00063
00064 void asigne(long num=0, long den=1);
00065
00066 long num() const { return m_num; }
00067 long den() const { return m_den; }
00068
00069
00070
00071
00072 rational& operator = (const rational&);
00073 rational& operator = (long);
00074 rational& Swap ( rational& );
00075
00076 rational& operator += (const rational&);
00077 rational& operator -= (const rational&);
00078 rational& operator *= (const rational&);
00079 rational& operator /= (const rational&);
00080
00081 rational operator - ();
00082
00083 friend rational operator + (const rational&, const rational&);
00084 friend rational operator - (const rational&, const rational&);
00085 friend rational operator * (const rational&, const rational&);
00086 friend rational operator / (const rational&, const rational&);
00087
00088 friend bool operator == (const rational&, const rational&);
00089 friend bool operator < (const rational&, const rational&);
00090 friend bool operator != (const rational&, const rational&);
00091 friend bool operator <= (const rational&, const rational&);
00092 friend bool operator >= (const rational&, const rational&);
00093 friend bool operator > (const rational&, const rational&);
00094
00095 friend ostream& operator << (ostream &, const rational& );
00096 friend istream& operator >> (istream &, rational& );
00097
00098 friend double real (const rational& );
00099 friend long integer(const rational& );
00100
00101 friend bool check_ok( const rational& r );
00102
00103
00104
00105 };
00106
00107 long mcd(long x, long y);
00108
00109 inline long gcd(long x, long y) { return mcd(x,y); }
00110
00112 inline void rational::asigne(long n, long d) {
00113 m_num = n;
00114 m_den = d;
00115 simplifique();
00116 }
00117
00129 inline rational& rational::operator = (const rational& o) {
00130
00131
00132 m_num = o.m_num,
00133 m_den = o.m_den;
00134
00135
00136 return *this;
00137 }
00138
00148 inline rational& rational::Swap ( rational& o) {
00149 #if 1
00150 rational tmp = o;
00151 o = *this;
00152 *this = tmp;
00153 #else
00154
00155 char tmp[ sizeof( *this ) ];
00156 memcpy( tmp, o, sizeof( *this ) );
00157 memcpy( o, *this, sizeof( *this ) );
00158 memcpy( *this, tmp, sizeof( *this ) );
00159 #endif
00160 return *this;
00161 }
00162
00164 inline rational& rational::operator = (long entero) {
00165
00166 m_num = entero;
00167 m_den = 1;
00168 return *this;
00169 }
00170
00172 inline rational& rational::operator *= (const rational& num) {
00173
00174 m_num *= num.m_num;
00175 m_den *= num.m_den;
00176 simplifique();
00177 return *this;
00178 }
00179
00185 inline rational& rational::operator /= (const rational& num) {
00186 m_num *= num.m_den;
00187 m_den *= num.m_num;
00188 simplifique();
00189 return *this;
00190 }
00191
00196 inline rational rational::operator - () {
00197
00198 rational tmp = (*this);
00199 tmp.m_num = - tmp.m_num;
00200 return tmp;
00201 }
00202
00204 inline bool operator == (const rational &x, const rational &y) {
00205 return (x.m_num == y.m_num) && (x.m_den == y.m_den);
00206 }
00207
00209 inline bool operator < (const rational &x, const rational &y) {
00210 return (x.m_num * y.m_den) < (x.m_den * y.m_num);
00211 }
00212
00214 inline bool operator > (const rational &x, const rational &y) {
00215 return (y < x);
00216 }
00217
00219 inline bool operator != (const rational& x, const rational& y) {
00220 return !(x == y);
00221 }
00222
00224 inline bool operator <= (const rational& x, const rational& y) {
00225 return !(y < x);
00226 }
00227
00229 inline bool operator >= (const rational& x, const rational& y) {
00230 return !(x < y);
00231 }
00232
00234 inline double real(const rational& num) {
00235 return double (num.m_num) / double (num.m_den);
00236 }
00237
00239 inline long integer(const rational& num) {
00240 return long (num.m_num / num.m_den);
00241 }
00242
00243 #if 0
00244 inline rational::operator long() {
00245
00246 return long (m_num / m_den);
00247 }
00248 #endif
00249
00250 #endif // rational_h
00251
00252