Ir a la documentación de este archivo.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef rational_h
00020 #define rational_h ///< Evita la inclusión múltiple
00021
00022 #define INCLUDE_iostream // ADH_port.h ==> #include <iostream>
00023 #include "ADH_port.h"
00024
00025 OPEN_namespace(ADH)
00026 USING_namespace(ADH);
00027
00028
00029
00030
00031
00032
00033 class rational {
00034 private:
00035 long m_num;
00036 long m_den;
00037
00038 void Simplify();
00039
00040 public:
00041
00042 rational() : m_num(0), m_den(1) { }
00043 rational(long num) : m_num(num), m_den(1) { }
00044 rational(long num, long den)
00045 : m_num(num), m_den(den) { Simplify(); }
00046
00047 rational(const rational& o) { m_num = o.m_num; m_den = o.m_den; }
00048 ~rational() { }
00049
00050 void set(long num=0, long den=1);
00051
00052 long num() const { return m_num; }
00053 long den() const { return m_den; }
00054
00055
00056
00057
00058 rational& operator = (const rational&);
00059 rational& operator = (long);
00060 rational& swap ( rational& );
00061
00062 rational& operator += (const rational&);
00063 rational& operator -= (const rational&);
00064
00065 rational& operator /= (const rational&);
00066
00067 rational operator - () const;
00068
00069 friend rational operator + (const rational&, const rational&);
00070 friend rational operator - (const rational&, const rational&);
00071 friend rational operator * (const rational&, const rational&);
00072 friend rational operator / (const rational&, const rational&);
00073
00074 friend bool operator == (const rational&, const rational&);
00075 friend bool operator < (const rational&, const rational&);
00076 friend bool operator != (const rational&, const rational&);
00077 friend bool operator <= (const rational&, const rational&);
00078 friend bool operator >= (const rational&, const rational&);
00079 friend bool operator > (const rational&, const rational&);
00080
00081 friend std::ostream& operator << (std::ostream &, const rational& );
00082 friend std::istream& operator >> (std::istream &, rational& );
00083 rational& fromString (const char* nStr);
00084
00085 friend double real (const rational& );
00086 friend long integer(const rational& );
00087
00088 friend bool check_ok( const rational& r );
00089
00090
00091
00092 };
00093
00094 long mcd(long x, long y);
00095
00096
00097 inline long gcd(long x, long y) { return mcd(x,y); }
00098
00099
00100 inline void rational::set(long n, long d) {
00101 m_num = n;
00102 m_den = d;
00103 Simplify();
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113 inline rational& rational::operator = (const rational& o) {
00114 m_num = o.m_num,
00115 m_den = o.m_den;
00116
00117
00118 return *this;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 inline rational& rational::swap ( rational& o ) {
00130 #if 1
00131 rational tmp = o;
00132 o = *this;
00133 *this = tmp;
00134 #else
00135
00136 char tmp[ sizeof( *this ) ];
00137 memcpy( tmp, & o, sizeof( *this ) );
00138 memcpy( & o, this, sizeof( *this ) );
00139 memcpy( this, tmp, sizeof( *this ) );
00140 #endif
00141 return *this;
00142 }
00143
00144
00145 inline rational& rational::operator = (long entero) {
00146 m_num = entero;
00147 m_den = 1;
00148 return *this;
00149 }
00150
00151
00152 #if 0
00153
00154 inline rational& rational::operator *= (const rational& num) {
00155 m_num *= num.m_num;
00156 m_den *= num.m_den;
00157 Simplify();
00158 return *this;
00159 }
00160 #endif
00161
00162 rational & operator *= ( rational & a, const rational & b );
00163
00164
00165
00166
00167
00168 inline rational& rational::operator /= (const rational& num) {
00169 m_num *= num.m_den;
00170 m_den *= num.m_num;
00171 Simplify();
00172 return *this;
00173 }
00174
00175
00176
00177
00178 inline rational rational::operator - () const {
00179 rational tmp = (*this);
00180 tmp.m_num = - tmp.m_num;
00181 return tmp;
00182 }
00183
00184
00185 inline bool operator == (const rational &x, const rational &y) {
00186 return (x.m_num == y.m_num) && (x.m_den == y.m_den);
00187
00188
00189
00190
00191
00192
00193 }
00194
00195
00196 inline bool operator < (const rational &x, const rational &y) {
00197 return (x.m_num * y.m_den) < (x.m_den * y.m_num);
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 }
00215
00216
00217 inline bool operator > (const rational &x, const rational &y) {
00218 return (y < x);
00219 }
00220
00221
00222 inline bool operator != (const rational& x, const rational& y) {
00223 return !(x == y);
00224 }
00225
00226
00227 inline bool operator <= (const rational& x, const rational& y) {
00228 return !(y < x);
00229 }
00230
00231
00232 inline bool operator >= (const rational& x, const rational& y) {
00233 return !(x < y);
00234 }
00235
00236
00237 inline double real(const rational& num) {
00238 return double (num.m_num) / double (num.m_den);
00239 }
00240
00241
00242 inline long integer(const rational& num) {
00243 return long (num.m_num / num.m_den);
00244 }
00245
00246 #if 0
00247
00248 inline rational::operator long() {
00249 return long (m_num / m_den);
00250 }
00251 #endif
00252
00253 bool check_ok_externo( const rational& r );
00254
00255 CLOSE_namespace(ADH)
00256
00257 #endif // rational_h
00258
00259