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
00021
00022 #define 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 rational(const rational& o)
00047 { 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 rational& operator *= (const rational&);
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 ostream& operator << (ostream &, const rational& );
00082 friend istream& operator >> (istream &, rational& );
00083 rational& fromString (const char* num);
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
00130 inline rational& rational::Swap ( rational& o ) {
00131 #if 1
00132 rational tmp = o;
00133 o = *this;
00134 *this = tmp;
00135 #else
00136
00137 char tmp[ sizeof( *this ) ];
00138 memcpy( tmp, o, sizeof( *this ) );
00139 memcpy( o, *this, sizeof( *this ) );
00140 memcpy( *this, tmp, sizeof( *this ) );
00141 #endif
00142 return *this;
00143 }
00144
00145
00146 inline rational& rational::operator = (long entero) {
00147 m_num = entero;
00148 m_den = 1;
00149 return *this;
00150 }
00151
00152
00153 inline rational& rational::operator *= (const rational& num) {
00154 m_num *= num.m_num;
00155 m_den *= num.m_den;
00156 Simplify();
00157 return *this;
00158 }
00159
00160
00161
00162
00163
00164
00165 inline rational& rational::operator /= (const rational& num) {
00166 m_num *= num.m_den;
00167 m_den *= num.m_num;
00168 Simplify();
00169 return *this;
00170 }
00171
00172
00173
00174
00175
00176 inline rational rational::operator - () const {
00177 rational tmp = (*this);
00178 tmp.m_num = - tmp.m_num;
00179 return tmp;
00180 }
00181
00182
00183 inline bool operator == (const rational &x, const rational &y) {
00184 return (x.m_num == y.m_num) && (x.m_den == y.m_den);
00185
00186
00187
00188
00189
00190
00191 }
00192
00193
00194 inline bool operator < (const rational &x, const rational &y) {
00195 return (x.m_num * y.m_den) < (x.m_den * y.m_num);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 }
00212
00213
00214 inline bool operator > (const rational &x, const rational &y) {
00215 return (y < x);
00216 }
00217
00218
00219 inline bool operator != (const rational& x, const rational& y) {
00220 return !(x == y);
00221 }
00222
00223
00224 inline bool operator <= (const rational& x, const rational& y) {
00225 return !(y < x);
00226 }
00227
00228
00229 inline bool operator >= (const rational& x, const rational& y) {
00230 return !(x < y);
00231 }
00232
00233
00234 inline double real(const rational& num) {
00235 return double (num.m_num) / double (num.m_den);
00236 }
00237
00238
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 bool check_ok_externo( const rational& r );
00251
00252 CLOSE_namespace(ADH)
00253
00254 #endif // rational_h
00255
00256