-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteiroLovelace.cpp
More file actions
496 lines (415 loc) · 12.2 KB
/
Copy pathInteiroLovelace.cpp
File metadata and controls
496 lines (415 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "InteiroLovelace.hpp"
const InteiroLovelace ZERO;
InteiroLovelace::InteiroLovelace(){
inicializar();
}
InteiroLovelace::InteiroLovelace(const InteiroLovelace &copiarInteiroLovelace){
setTamanho(copiarInteiroLovelace.getTamanho());
setQuantidadeAlgarismos(copiarInteiroLovelace.getQuantidadeAlgarismos());
setZero(copiarInteiroLovelace.eZero());
setSinal(copiarInteiroLovelace.getSinal());
if (!copiarInteiroLovelace.eZero())
copiarAlgarismos(copiarInteiroLovelace, *this);
}
InteiroLovelace::InteiroLovelace(const Lovelace &copiarLovelace){
setTamanho(copiarLovelace.getTamanho());
setQuantidadeAlgarismos(copiarLovelace.getQuantidadeAlgarismos());
setZero(copiarLovelace.eZero());
setSinal(true);
if (!copiarLovelace.eZero())
copiarAlgarismos(copiarLovelace, *this);
}
InteiroLovelace::InteiroLovelace(const char *algarismos,int tamanho,int quantidadeAlgarismos,bool zero,bool sinal)
: Lovelace(algarismos,tamanho,quantidadeAlgarismos,zero)
{
setSinal(sinal);
}
void InteiroLovelace::inicializar(){
Lovelace::inicializar();
setSinal(true);
}
void InteiroLovelace::zerar(){
Lovelace::zerar();
setSinal(true);
}
void InteiroLovelace::imprimir() const{
imprimir(0);
}
void InteiroLovelace::imprimir(char separador) const{
if (eNegativo())
cout << '-';
Lovelace::imprimir(separador);
}
void InteiroLovelace::imprimirInfo(int opcao) const{
cout << "sinal : " << getSinal() << endl;
Lovelace::imprimirInfo(opcao);
}
bool InteiroLovelace::getSinal() const{
return sinal;
}
void InteiroLovelace::setSinal(bool novoSinal){
sinal = novoSinal;
}
void InteiroLovelace::toLovelace(Lovelace &saida) const{
Lovelace aux(this->algarismos,this->getTamanho(),this->getQuantidadeAlgarismos(),this->eZero());
saida.atribuir(aux);
}
InteiroLovelace InteiroLovelace::somar(const InteiroLovelace &B) const{
Lovelace auxA,auxB;
toLovelace(auxA);
B.toLovelace(auxB);
if (this->getSinal() == B.getSinal()){
InteiroLovelace resultado(auxA.somar(auxB));
resultado.setSinal(this->getSinal());
return resultado;
}
else {
InteiroLovelace resultado(auxA.subtrair(auxB));
bool sinalMaior = auxA.eMaiorQue(B)?this->getSinal():B.getSinal();
resultado.setSinal(sinalMaior);
return resultado;
}
}
InteiroLovelace InteiroLovelace::subtrair(const InteiroLovelace &B) const{
Lovelace auxA,auxB;
toLovelace(auxA);
B.toLovelace(auxB);
if(this->getSinal() != B.getSinal()){
InteiroLovelace resultado(auxA.somar(auxB));
resultado.setSinal(this->getSinal());
return resultado;
}
else if(this->ePositivo() && B.ePositivo()){
InteiroLovelace resultado(auxA.subtrair(auxB));
resultado.setSinal(auxB.eMenorOuIgualA(auxA));
return resultado;
}
else if(this->eNegativo() && B.eNegativo()){
InteiroLovelace resultado(auxA.subtrair(auxB));
resultado.setSinal(auxA.eMenorOuIgualA(auxB));
return resultado;
}
}
InteiroLovelace InteiroLovelace::multiplicar(const InteiroLovelace &B) const{
Lovelace auxA,auxB;
toLovelace(auxA);
B.toLovelace(auxB);
InteiroLovelace resultado(auxA.multiplicar(auxB));
resultado.setSinal(this->getSinal()==B.getSinal());
return resultado;
}
void InteiroLovelace::dividir(const InteiroLovelace &B,InteiroLovelace &quociente,InteiroLovelace &resto) const{
Lovelace auxA,auxB;
toLovelace(auxA);
B.toLovelace(auxB);
bool sinaisIguais=(this->getSinal()==B.getSinal());
{
InteiroLovelace result(auxA.dividir_burro(auxB));
result.setSinal(sinaisIguais);
quociente.atribuir(result);
}
{
InteiroLovelace rest(auxA.dividir_burro(auxB,false));
rest.setSinal(sinaisIguais);//Essa lógica tem que ser revista
resto.atribuir(rest);
}
}
InteiroLovelace InteiroLovelace::dividir(const InteiroLovelace &B, bool quocienteOuResto) const{
Lovelace auxA,auxB;
toLovelace(auxA);
B.toLovelace(auxB);
InteiroLovelace resultado(auxA.dividir_burro(auxB,quocienteOuResto));
resultado.setSinal(this->getSinal()==B.getSinal());
return resultado;
}
InteiroLovelace InteiroLovelace::exponenciar(const InteiroLovelace &X) const{
if(!this->eZero() && X.eMaiorQue(ZERO)){
Lovelace auxA,auxX;
toLovelace(auxA);
X.toLovelace(auxX);
InteiroLovelace resultado(auxA.exponenciar(auxX));
if(!this->getSinal() && X.eImpar())
resultado.setSinal(false);
return resultado;
}
else
errorMessage("Operação inválida de exponenciação!");
}
InteiroLovelace InteiroLovelace::incrementar(){
InteiroLovelace aux;
aux.setDigito(0,1);//aux=1; Equivalente após sobrecarga //Remover depois de fazer a base pro java!
return ((*this) = somar(aux));
}
InteiroLovelace InteiroLovelace::decrementar(){
InteiroLovelace aux;
aux.setDigito(0,1);
return ((*this) = subtrair(aux));
}
InteiroLovelace& InteiroLovelace::atribuir(long long int numero){
unsigned long long int n = numero;
Lovelace::atribuir(n);
if (numero >= 0)
setSinal(true);
else
setSinal(false);
return (*this);
}
InteiroLovelace& InteiroLovelace::atribuir(const int &numero){
long long int aux = numero;
return atribuir(aux);
}
InteiroLovelace& InteiroLovelace::atribuir(const InteiroLovelace &B){
if (&B != this){
if (naoEZero())
delete this->algarismos;
if (B.eZero())
this->algarismos = NULL;
else
copiarAlgarismos(B,*this);
this->setQuantidadeAlgarismos(B.getQuantidadeAlgarismos());
this->setTamanho(B.getTamanho());
this->setZero(B.eZero());
this->setSinal(B.getSinal());
}
return (*this);
}
InteiroLovelace& InteiroLovelace::atribuir(string numeroEmString){
long long int inicio, tamanho;
string::iterator it=numeroEmString.begin();
for (tamanho = 0, inicio = 0; it!=numeroEmString.end() && ((numeroEmString[tamanho] >= '0' && numeroEmString[tamanho] <= '9') || numeroEmString[tamanho] == '-');++tamanho, ++it){
if (!tamanho) {
if (numeroEmString[tamanho] == '0')
inicio++;
else if (numeroEmString[tamanho] == '-'){
setSinal(false);
inicio++;
}
}
}
if (tamanho-inicio == 0){
this->zerar();
}
else {
long long int c;
tamanho--;
for(c=tamanho;c>=inicio;c--)
this->setDigito(tamanho-c,numeroEmString[c]-'0');
}
return (*this);
}
InteiroLovelace InteiroLovelace::fatorial() const{
InteiroLovelace resultado;
if (!getSinal())
resultado = Lovelace::fatorial();
return resultado;
}
InteiroLovelace InteiroLovelace::inverterSinal() const{
InteiroLovelace resultado(*this);
if (this->naoEZero())
resultado.setSinal(!getSinal());
return resultado;
}
bool InteiroLovelace::eIgualA(const InteiroLovelace &B) const{
if (this == &B)
return true;
if (this->getSinal() != B.getSinal())
return false;
if (this->getQuantidadeAlgarismos() != B.getQuantidadeAlgarismos())
return false;
else {
if (this->eZero() && B.eZero())
return true;
for (long long int c = 0;c < this->getTamanho();c++)
if (this->algarismos[c] != B.algarismos[c])
return false;
}
return true;
}
bool InteiroLovelace::eDiferenteDe(const InteiroLovelace &B) const{
return !(this->eIgualA(B));
}
bool InteiroLovelace::eMaiorQue(const InteiroLovelace &B) const{
if (this == &B)
return false;
if (this->getSinal() != B.getSinal())
return ePositivo();
else if (this->getQuantidadeAlgarismos() == B.getQuantidadeAlgarismos()){
if (this->eZero() && B.eZero())
return false;
long long int c;
for (c = this->getQuantidadeAlgarismos();c && this->getDigito(c) == B.getDigito(c);c--);
if (this->getDigito(c) > B.getDigito(c))
return ePositivo();
else
return eNegativo();
}
else if (this->getQuantidadeAlgarismos() > B.getQuantidadeAlgarismos())
return this->ePositivo();
else
return this->eNegativo();
return true;
}
bool InteiroLovelace::eMenorQue(const InteiroLovelace &B) const{
if (this == &B)
return false;
if (this->getSinal() != B.getSinal())
return eNegativo();
else if (this->getQuantidadeAlgarismos() == B.getQuantidadeAlgarismos()){
if (this->eZero() && B.eZero())
return false;
long long int c;
for (c = this->getQuantidadeAlgarismos();c && this->getDigito(c) == B.getDigito(c);c--);
if (this->getDigito(c) < B.getDigito(c))
return ePositivo();
else
return eNegativo();
}
else if (this->getQuantidadeAlgarismos() < B.getQuantidadeAlgarismos())
return this->ePositivo();
else
return this->eNegativo();
return true;
}
bool InteiroLovelace::eMaiorOuIgualA(const InteiroLovelace &B) const{
return (this->eIgualA(B) || this->eMaiorQue(B));
}
bool InteiroLovelace::eMenorOuIgualA(const InteiroLovelace &B) const{
return (this->eIgualA(B) || this->eMenorQue(B));
}
bool InteiroLovelace::ePositivo() const{
return getSinal();
}
bool InteiroLovelace::eNegativo() const{
return !getSinal();
}
InteiroLovelace& InteiroLovelace::operator=(InteiroLovelace &B){
if (&B != this){
if (naoEZero())
delete this->algarismos;
if (B.eZero())
this->algarismos = NULL;
else
copiarAlgarismos(B,*this);
this->setQuantidadeAlgarismos(B.getQuantidadeAlgarismos());
this->setTamanho(B.getTamanho());
this->setZero(B.eZero());
this->setSinal(B.getSinal());
}
return (*this);
}
InteiroLovelace& InteiroLovelace::operator=(const InteiroLovelace &B){
this->atribuir(B);
return (*this);
}
InteiroLovelace& InteiroLovelace::operator=(const long long int &numero){
return atribuir(numero);
}
InteiroLovelace& InteiroLovelace::operator=(const int &numero){
return atribuir(numero);
}
InteiroLovelace& InteiroLovelace::operator+=(const InteiroLovelace &B){
return ((*this) = somar(B));
}
InteiroLovelace& InteiroLovelace::operator-=(const InteiroLovelace &B){
return ((*this) = subtrair(B));
}
InteiroLovelace& InteiroLovelace::operator*=(const InteiroLovelace &B){
return ((*this) = multiplicar(B));
}
InteiroLovelace& InteiroLovelace::operator/=(const InteiroLovelace &B){
return ((*this) = dividir(B));
}
InteiroLovelace& InteiroLovelace::operator%=(const InteiroLovelace &B){
}
InteiroLovelace& InteiroLovelace::operator^=(const InteiroLovelace &B){
}
/* Operações Aritméticas */
InteiroLovelace InteiroLovelace::operator+(const InteiroLovelace &B) const{
return somar(B);
}
InteiroLovelace InteiroLovelace::operator-(const InteiroLovelace &B) const{
return subtrair(B);
}
InteiroLovelace InteiroLovelace::operator*(const InteiroLovelace &B) const{
return multiplicar(B);
}
InteiroLovelace InteiroLovelace::operator/(const InteiroLovelace &B) const{
InteiroLovelace resultado;
//return dividir(B);
return resultado;
}
InteiroLovelace InteiroLovelace::operator%(const InteiroLovelace &B) const{
return multiplicar(B);
}
InteiroLovelace InteiroLovelace::operator^(const InteiroLovelace &B) const{
return exponenciar(B);
}
/* Operações Inc/Dec */
InteiroLovelace& InteiroLovelace::operator++(){
this->incrementar();
return (*this);
}
InteiroLovelace& InteiroLovelace::operator--(){
this->decrementar();
return (*this);
}
InteiroLovelace InteiroLovelace::operator++(int semuso){
InteiroLovelace retorno;
retorno = (*this);
this->incrementar();
return retorno;
}
InteiroLovelace InteiroLovelace::operator--(int semuso){
InteiroLovelace retorno;
retorno = (*this);
this->decrementar();
return retorno;
}
/* Operações de Comparação */
bool operator==(const InteiroLovelace &A, const InteiroLovelace &B){
return (A.eIgualA(B));
}
bool operator!=(const InteiroLovelace &A, const InteiroLovelace &B){
return (A.eDiferenteDe(B));
}
bool operator>(const InteiroLovelace &A, const InteiroLovelace &B){
return A.eMaiorQue(B);
}
bool operator<(const InteiroLovelace &A, const InteiroLovelace &B){
return A.eMenorQue(B);
}
bool operator>=(const InteiroLovelace &A, const InteiroLovelace &B){
return A.eMaiorOuIgualA(B);
}
bool operator<=(const InteiroLovelace &A, const InteiroLovelace &B){
return A.eMenorOuIgualA(B);
}
/* Operações de Ext/Ins de Fluxo */
std::ostream &operator<<(std::ostream &out,const InteiroLovelace &A){
long long int c;
char a, b;
if (A.eNegativo())
cout << '-';
if (A.eZero()) {
out << Lovelace::TabelaDeConversao[0];
}
else {
A.getBitwise(A.getTamanho()-1,a,b);
if(!(A.getQuantidadeAlgarismos()%2))
out << Lovelace::TabelaDeConversao[(int)b] << A.TabelaDeConversao[(int)a];
else
out << Lovelace::TabelaDeConversao[(int)a];
for(c=A.getTamanho()-2;c>-1;c--) {
A.getBitwise(c,a,b);
out << Lovelace::TabelaDeConversao[(int)b] << Lovelace::TabelaDeConversao[(int)a];
}
}
return out;
}
std::istream &operator>>(std::istream &in,InteiroLovelace &A){
string entrada;
while(!entrada[0])
getline(in, entrada);
A.atribuir(entrada);
return in;
}