00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef decimal_h
00017 #define decimal_h
00018
00019 #include <iostream>
00020 #include "BigNum.h"
00021 #include <cstdlib>
00022 #include <cctype>
00023
00024 class decimal{
00025 private:
00026 BigNum m_num;
00027 decimal( const BigNum& res ) { m_num = res; }
00028
00029
00030 public:
00031
00032 decimal() : m_num(0) { }
00033 decimal(long num) : m_num(num) { }
00034 decimal(const decimal& o)
00035 { m_num = o.m_num; }
00036 ~decimal() { }
00037
00038
00039
00040
00041
00042 decimal& operator = (const decimal&);
00043 decimal& operator = (long);
00044 decimal& swap ( decimal& );
00045
00046 decimal& operator += (const decimal&);
00047 decimal& operator -= (const decimal&);
00048 decimal& operator *= (const decimal&);
00049 decimal& operator /= (const decimal&);
00050
00051 decimal operator - () const;
00052
00053 friend decimal operator + (const decimal&, const decimal&);
00054 friend decimal operator - (const decimal&, const decimal&);
00055 friend decimal operator * (const decimal&, const decimal&);
00056 friend decimal operator / (const decimal&, const decimal&);
00057 friend decimal operator % (const decimal&, const decimal&);
00058
00059 friend bool operator == (const decimal&, const decimal&);
00060 friend bool operator < (const decimal&, const decimal&);
00061 friend bool operator != (const decimal&, const decimal&);
00062 friend bool operator <= (const decimal&, const decimal&);
00063 friend bool operator >= (const decimal&, const decimal&);
00064 friend bool operator > (const decimal&, const decimal&);
00065
00066 friend ostream& operator << (ostream &, const decimal& );
00067 friend istream& operator >> (istream &, decimal& );
00068
00069 };
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 inline decimal& decimal::operator = (const decimal& o) {
00080 m_num = o.m_num;
00081
00082
00083 return *this;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 inline decimal& decimal::swap ( decimal& o ) {
00095 #if 1
00096 decimal tmp = o;
00097 o = *this;
00098 *this = tmp;
00099 #else
00100
00101 char tmp[ sizeof( *this ) ];
00102 memcpy( tmp, o, sizeof( *this ) );
00103 memcpy( o, *this, sizeof( *this ) );
00104 memcpy( *this, tmp, sizeof( *this ) );
00105 #endif
00106 return *this;
00107 }
00108
00109
00110 inline decimal& decimal::operator = (long entero) {
00111 m_num = entero;
00112 return *this;
00113 }
00114
00115
00116 inline decimal& decimal::operator *= (const decimal& num) {
00117 BigNum m_res;
00118
00119 m_res = m_num * num.m_num;
00120 m_num = m_res;
00121
00122 return *this;
00123 }
00124
00125
00126
00127
00128
00129 inline decimal& decimal::operator /= (const decimal& num) {
00130 BigNum m_res;
00131
00132 m_res = m_num / num.m_num;
00133 m_num = m_res;
00134
00135 return *this;
00136 }
00137
00138
00139
00140
00141 inline decimal decimal::operator - () const {
00142 decimal tmp = (*this);
00143 tmp.m_num = - tmp.m_num;
00144 return tmp;
00145 }
00146
00147
00148 inline bool operator == (const decimal &x, const decimal &y) {
00149 return (x.m_num == y.m_num);
00150
00151 }
00152
00153
00154
00155 inline bool operator < (const decimal &x, const decimal &y) {
00156 return x.m_num < y.m_num;
00157
00158 }
00159
00160
00161 inline bool operator > (const decimal &x, const decimal &y) {
00162 return (y < x);
00163 }
00164
00165
00166 inline bool operator != (const decimal& x, const decimal& y) {
00167 return !(x == y);
00168 }
00169
00170
00171 inline bool operator <= (const decimal& x, const decimal& y) {
00172 return !(y < x);
00173 }
00174
00175
00176 inline bool operator >= (const decimal& x, const decimal& y) {
00177 return !(x < y);
00178 }
00179
00180
00181
00182
00183
00184
00185 decimal& decimal::operator += (const decimal& otro) {
00186 BigNum m_res;
00187
00188 m_res = m_num + otro.m_num;
00189 m_num = m_res;
00190
00191 return *this;
00192 }
00193
00194
00195
00196 decimal& decimal::operator -= (const decimal& otro) {
00197 BigNum m_res;
00198
00199 m_res = m_num - otro.m_num;
00200 m_num = m_res;
00201
00202 return *this;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 ostream& operator<< (ostream &COUT, const decimal& r) {
00215
00216 return COUT << "[" << r.m_num << "]" ;
00217
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 istream& operator >> (istream &CIN, decimal& r) {
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 return CIN;
00290
00291 }
00292
00293
00294
00295 decimal operator + (const decimal &x, const decimal &y) {
00296
00297 return decimal(x.m_num + y.m_num);
00298 }
00299
00300
00301
00302
00303 decimal operator - (const decimal &x, const decimal &y) {
00304
00305 return decimal(x.m_num - y.m_num);
00306 }
00307
00308
00309
00310 decimal operator * (const decimal &x, const decimal &y) {
00311
00312 return decimal(x.m_num * y.m_num);
00313 }
00314
00315
00316
00317
00318 decimal operator / (const decimal &x, const decimal &y) {
00319
00320 return decimal(x.m_num / y.m_num);
00321 }
00322
00323 decimal operator % (const decimal &x, const decimal &y) {
00324
00325 return decimal(x.m_num % y.m_num);
00326 }
00327
00328 #endif // rational_h
00329
00330