Ir a la documentación de este archivo.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "rational.h"
00013 #include <cstdlib>
00014 #include <cctype>
00015
00016 OPEN_namespace(ADH)
00017 USING_namespace(ADH);
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 bool check_ok( const rational& r ) {
00035 if ( &r != 0 ) {
00036
00037 }
00038 else {
00039
00040 return false;
00041 }
00042
00043 if ( r.m_den > 0 ) {
00044
00045 }
00046 else {
00047
00048 return false;
00049 }
00050 if (r.m_num == 0) {
00051 if ( r.m_den == 1 ) {
00052
00053 return true;
00054 }
00055 else {
00056 return false;
00057 }
00058 }
00059 if ( mcd(r.m_num, r.m_den) == 1 ) {
00060
00061 }
00062 else {
00063
00064 return false;
00065 }
00066 return true;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 bool check_ok_no_Rep( const rational& r ) {
00079 if ( &r != 0 ) {
00080
00081 }
00082 else {
00083
00084 return false;
00085 }
00086
00087 if ( r.den() > 0 ) {
00088
00089 }
00090 else {
00091
00092 return false;
00093 }
00094 if (r.num() == 0) {
00095 if ( r.den() == 1 ) {
00096
00097 return true;
00098 }
00099 else {
00100 return false;
00101 }
00102 }
00103 if ( mcd(r.num(), r.den()) == 1 ) {
00104
00105 }
00106 else {
00107
00108 return false;
00109 }
00110 return true;
00111
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 long mcd(long x, long y) {
00132 long g = (x < 0 ? -x : x);
00133 long r = (y < 0 ? -y : y);
00134 long temp;
00135
00136 do {
00137 temp = r;
00138 r = g % r;
00139 g = temp;
00140 } while (0 != r);
00141
00142 return g;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 void rational::Simplify() {
00157 if (m_num == 0) {
00158 m_den = 1;
00159 }
00160 long divisor = mcd(m_num, m_den);
00161 if (divisor > 1) {
00162 m_num /= divisor;
00163 m_den /= divisor;
00164 }
00165 if (m_den < 0) {
00166 m_num = -m_num;
00167 m_den = -m_den;
00168 }
00169 }
00170
00171
00172 rational& rational::operator += (const rational& otro) {
00173 m_num = m_num * otro.m_den + m_den * otro.m_num;
00174 m_den *= otro.m_den;
00175 Simplify();
00176
00177 return *this;
00178 }
00179
00180
00181
00182 rational& rational::operator -= (const rational& otro) {
00183 long oldm_den = m_den;
00184 long oldm_num = m_num;
00185 long d = otro.m_den;
00186 long n = otro.m_num;
00187
00188 m_den *= d;
00189 m_num = oldm_num * d - oldm_den * n;
00190 Simplify();
00191
00192 return *this;
00193 }
00194
00195
00196 rational & operator *= ( rational & a, const rational & b ) {
00197 a.set( a.num() * b.num(), a.den() * b.den() );
00198 return a;
00199 }
00200
00201
00202
00203 rational& rational::fromString (const char* nStr) {
00204 char ch;
00205
00206 bool es_positivo = true;
00207
00208
00209 do {
00210 ch = *nStr; nStr++;
00211 if (ch == '-') {
00212 es_positivo = !es_positivo;
00213 }
00214 } while (!isdigit(ch));
00215
00216
00217 long num = 0;
00218 while (isdigit(ch)) {
00219 num = 10 * num + (ch-'0');
00220 ch = *nStr; nStr++;
00221 }
00222
00223
00224 while (isspace(ch)) {
00225 ch = *nStr; nStr++;
00226 }
00227
00228 long den;
00229 if (ch ==']') {
00230 den = 1;
00231 }
00232 else {
00233 do {
00234 ch = *nStr; nStr++;
00235 if (ch == '-') {
00236 es_positivo = !es_positivo;
00237 }
00238 } while (!isdigit(ch));
00239
00240
00241 den = 0;
00242 while (isdigit(ch)) {
00243 den = 10 * den + (ch-'0');
00244 ch = *nStr; nStr++;
00245 }
00246
00247 }
00248
00249
00250
00251 if (! es_positivo) {
00252 num = -num;
00253 }
00254 set( num, den );
00255 return *this;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 std::ostream& operator<< (std::ostream &COUT, const rational& r) {
00267 if ( r.m_den == 1 ) {
00268 return COUT << "[" << r.m_num << "]" ;
00269 } else {
00270 return COUT << "[" << r.m_num << "/" << r.m_den << "]" ;
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 std::istream& operator >> (std::istream &CIN, rational& r) {
00284 char ch;
00285
00286 bool es_positivo = true;
00287
00288
00289 do {
00290 CIN >> ch;
00291 if (ch == '-') {
00292 es_positivo = !es_positivo;
00293 }
00294 } while (!isdigit(ch));
00295
00296
00297 r.m_num = 0;
00298 while (isdigit(ch)) {
00299 r.m_num = 10 * r.m_num + (ch-'0');
00300 CIN >> ch;
00301 }
00302
00303
00304 while (isspace(ch)) {
00305 CIN >> ch;
00306 }
00307
00308 if (ch ==']') {
00309 r.m_den = 1;
00310 }
00311 else {
00312 do {
00313 CIN >> ch;
00314 if (ch == '-') {
00315 es_positivo = !es_positivo;
00316 }
00317 } while (!isdigit(ch));
00318
00319
00320 r.m_den = 0;
00321 while (isdigit(ch)) {
00322 r.m_den = 10 * r.m_den + (ch-'0');
00323 CIN >> ch;
00324 }
00325
00326
00327
00328
00329 while (ch != ']') {
00330 CIN >> ch;
00331 }
00332 }
00333
00334
00335
00336 if (! es_positivo) {
00337 r.m_num = -r.m_num;
00338 }
00339
00340 r.Simplify();
00341 return CIN;
00342
00343
00344
00345
00346
00347
00348
00349 }
00350
00351
00352
00353 rational operator + (const rational &x, const rational &y) {
00354 long res_num, res_den;
00355 res_den = x.m_den * y.m_den;
00356 res_num = x.m_num * y.m_den + x.m_den * y.m_num;
00357
00358 return rational(res_num, res_den);
00359 }
00360
00361
00362
00363 rational operator - (const rational &x, const rational &y) {
00364 long res_num, res_den;
00365
00366 res_den = x.m_den * y.m_den;
00367 res_num = x.m_num * y.m_den - x.m_den * y.m_num;
00368
00369 return rational(res_num, res_den);
00370 }
00371
00372
00373
00374 rational operator * (const rational &x, const rational &y) {
00375 long res_num, res_den;
00376
00377 res_num = x.m_num * y.m_num;
00378 res_den = x.m_den * y.m_den;
00379
00380 return rational(res_num, res_den);
00381 }
00382
00383
00384
00385
00386 rational operator / (const rational &x, const rational &y) {
00387 long res_num, res_den;
00388 if (0 != y.m_num) {
00389 res_num = x.m_num * y.m_den;
00390 res_den = x.m_den * y.m_num;
00391 }
00392 return rational(res_num, res_den);
00393 }
00394
00395 CLOSE_namespace(ADH)
00396
00397