00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <QString>
00010 #include <QChar>
00011
00012 #include "stdio.h"
00013
00014 #include "fixed.h"
00015 #include "funcaux.h"
00016
00017
00019
00023 Fixed::Fixed(int x, int p) {
00024 _depura("Fixed::Fixed", 0);
00025 value = x;
00026 precision = p;
00027 _depura("END Fixed::Fixed", 0);
00028 }
00029
00030
00032
00035 Fixed::Fixed(QString a) {
00036 _depura("Fixed::Fixed", 0);
00037 fromFixed(a.toAscii());
00038 _depura("END Fixed::Fixed", 0);
00039 }
00040
00041
00043
00046 Fixed::Fixed(const char *a) {
00047 _depura("Fixed::Fixed", 0);
00048 fromFixed(a);
00049 _depura("END Fixed::Fixed", 0);
00050 }
00051
00052
00054
00056 Fixed::Fixed() {
00057 _depura("Fixed::Fixed", 0);
00058 value = 0;
00059 precision = 1;
00060 _depura("END Fixed::Fixed", 0);
00061
00062 }
00063
00064
00065 Fixed operator + (Fixed x, Fixed y) {
00066 x.equalize_precision(y);
00067 x.value = x.value + y.value;
00068 return x;
00069 }
00070
00071
00072
00073 Fixed operator + (Fixed x, int y) {
00074 return x + Fixed(y, 0);
00075 }
00076
00077
00078
00079 Fixed Fixed::operator = (Fixed x) {
00080 value = x.value;
00081 precision = x.precision;
00082 return *this;
00083 }
00084
00085
00086 Fixed Fixed::operator = (int x) {
00087 value = x;
00088 precision = 0;
00089 return *this;
00090 }
00091
00092
00093 Fixed operator / (Fixed x, Fixed y) {
00094 x.value = x.value / y.value;
00095 x.precision += y.precision;
00096 return x;
00097 }
00098
00099
00100 Fixed operator / (Fixed x, int y) {
00101 return x / Fixed(y, 0);
00102 }
00103
00104
00105 Fixed operator / (int x, Fixed y) {
00106 return Fixed(x, 0) / y;
00107 }
00108
00109
00111
00115 QString Fixed::toQString(QChar separadorDecimal) {
00116 _depura("Fixed::toQString", 0);
00117 setprecision(2);
00118 int options = COMMAS;
00119 Fixed_numerator x = value;
00120 bool negative;
00121 if (x < 0) {
00122 x = -x;
00124 if (x < 0)
00125 x = x - 1;
00126 negative = true;
00127 } else
00128 negative = false;
00129 Fixed_numerator n = 0;
00130 Fixed_numerator units = 0;
00131 unsigned char buffer[MAX_FIXED_LENGTH + MAX_FIXED_PRECISION];
00132 for (unsigned int i = 0; i <= sizeof(buffer); i++)
00133 buffer[i] = 0;
00134 do {
00135 if (n == precision) {
00136 if (n > 0 || options & DECIMAL)
00139
00140
00141 buffer[sizeof(buffer) - ++n] = separadorDecimal.toAscii();
00142
00143 units = n;
00144 }
00145 Fixed_numerator y;
00146 y = (Fixed_numerator) x / 10;
00147 buffer[sizeof(buffer) - ++n] = integer(x - y * 10) + '0';
00148 x = y;
00149 } while (n <= precision || x != 0);
00150 if (negative)
00151 buffer[sizeof(buffer) - ++n] = '-';
00152 if (options & ALIGN) {
00153 while (n - units < MAX_FIXED_LENGTH - 2)
00154 buffer[sizeof(buffer) - ++n] = ' ';
00155 }
00156 QString a((const char *) buffer + sizeof(buffer) - n);
00157 _depura("END Fixed::toQString", 0);
00158 return a;
00159 }
00160
00161
00162 bool operator == (Fixed x, Fixed y) {
00163 x.equalize_precision(y);
00164 return x.value == y.value;
00165 }
00166
00167
00168 bool operator == (Fixed x, int y) {
00169 return x == Fixed(y, 0);
00170 }
00171
00172
00173
00174 bool operator == (int x, Fixed y) {
00175 return Fixed(x, 0) == y;
00176 }
00177
00178
00180
00183 void Fixed::equalize_precision(Fixed &x) {
00184 _depura("Fixed::equalize_precision", 0);
00185 while (precision < x.precision) {
00186 value *= 10;
00187 precision ++;
00188 }
00189 while (x.precision < precision) {
00190 x.value *= 10 ;
00191 x.precision ++;
00192 }
00193 _depura("END Fixed::equalize_precision", 0);
00194 }
00195
00196
00198
00201 void Fixed::setprecision(int prec) {
00202 _depura("Fixed::setprecision", 0);
00203 while (precision < prec) {
00204 value *= 10;
00205 precision ++;
00206 }
00207 while (prec < precision) {
00208 Fixed_numerator aux;
00209 aux = value;
00210 value = (Fixed_numerator) (value / 10);
00211 if ((aux % 10) >=5) {
00212 value++;
00213 }
00214 precision--;
00215 }
00216 _depura("END Fixed::setprecision", 0);
00217 }
00218
00219
00221
00224 void Fixed::fromFixed(const char *s) {
00225 _depura("Fixed::fromFixed", 0);
00226 value = 0;
00227 precision = 0;
00228 int c;
00229 while ((c = *s++) == ' ' || c == '\t')
00230 ;
00231 bool negative;
00232 if (c == '-') {
00233 negative = true;
00234 c = *s++;
00235 } else {
00236 negative = false;
00237 }
00238 bool decimal = false;
00239 while (precision < MAX_FIXED_PRECISION) {
00240 if ('0' <= c && c <= '9') {
00241 value = value * 10 + (c - '0');
00242 if (decimal)
00243 precision++;
00244 } else if (c == '.' || c ==',') {
00245 if (decimal)
00246 break;
00247 decimal = true;
00248 } else if (c != ',')
00249 break;
00250 c = *s++;
00251 }
00252 if (negative)
00253 value = - value;
00254 if (value == 0)
00255 precision = 1;
00256 _depura("END Fixed::fromFixed", 0);
00257 }
00258
00259
00260 bool operator < (Fixed x, Fixed y) {
00261 x.equalize_precision(y);
00262 return x.value < y.value;
00263 }
00264
00265
00266 bool operator < (Fixed x, int y) {
00267 return x < Fixed(y, 0);
00268 }
00269
00270
00271 bool operator < (int x, Fixed y) {
00272 return Fixed(x, 0) < y;
00273 }
00274
00275
00276 Fixed operator - (Fixed x) {
00277 x.value = -x.value;
00278 return x;
00279 }
00280
00281
00282 Fixed operator * (Fixed x, int y) {
00283 return x * Fixed(y, 0);
00284 }
00285
00286
00287 Fixed operator * (Fixed x, Fixed y) {
00288 x.value = x.value * y.value;
00289 x.precision = x.precision + y.precision;
00290 return x;
00291 }
00292
00293
00294 Fixed Fixed::operator [] (int p) const {
00295 _depura("Fixed::operator[]");
00296 Fixed x(0, p);
00297 _depura("END Fixed::operator[]");
00298 return x;
00299 }
00300
00301
00302 Fixed operator - (Fixed x, Fixed y) {
00303 x.equalize_precision(y);
00304 x.value = x.value - y.value;
00305 return x;
00306 }
00307
00308
00309 Fixed operator - (Fixed x, int y) {
00310 return x - Fixed(y, 0);
00311 }
00312
00313
00314 Fixed operator - (int x, Fixed y) {
00315 return Fixed(x, 0) - y;
00316 }
00317