-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyInt.cpp
More file actions
552 lines (411 loc) · 11 KB
/
Copy pathmyInt.cpp
File metadata and controls
552 lines (411 loc) · 11 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Hector Rizo
// Assignment 4
//
//
#include <iostream>
#include <cstring>
#include "myInt.h"
using namespace std;
// ------------CONSTRUCTORS ------------
MyInt :: MyInt(){
capacity = 1;
size = 1;
number = new char [capacity];
number[0] = 48; // 48 is the ascii value for 0
}
MyInt :: ~MyInt(){
if(number != NULL){ // checking if pointer is used
delete [] number;
number = NULL;
}
}
MyInt :: MyInt (const int& input){
int num = 0;
if (input <0)
num = 0;
else
num = input; // making copy of input
capacity = numDigits(input); // setting capacity equal to num of digits
size = capacity;
number = new char [capacity];
int copy = input; // this value stores the copy from the integer
for(int i =size-1; i>=0; i--){
number[i] = copy%10 + 48;
copy = copy/10; // shrinking interger by one digit
}
}
MyInt:: MyInt ( const char* numbers){
bool reset = 0; // this variable is false until a invalid char is met
int index = 0;
int count = 0;
bool stop = 0;
for(int i = 0; i <strlen(numbers); i++){
if(numbers[i] != 48){stop = 1;}
if(numbers[i] == 48 && stop == 0){
index = i+1;
count++;
}
else continue;
}
capacity = strlen(numbers) - count; // taking null charector into account
size = capacity;
number = new char[capacity];
for(int i = 0; i < size; i++){
if(numbers[i] >= 48 && numbers[i] <= 57)
number[i] = numbers[index + i];
else{ reset = 1; break;}
}
if(reset == 1){
number[0] = 48;
for(int i = 1; i < size; i++)
number[i] = '\0';
size =1;
}
shrink();
}
MyInt :: MyInt (const MyInt& copy){
size = copy.size;
capacity = copy.capacity;
number = new char[capacity];
for(int i = 0; i< size; i++)
number[i] = copy.number[i];
}
// -------- HELPER FUNCTIONS --------
int MyInt :: numDigits(const int& input) const{
int number = 10; // making a copy of input
int count = 1; // counting digits always starts at 1
while(number <= input)
{
count++;
number *= 10;
}
return count;
}
void MyInt :: shrink(){
char * temp = NULL;
capacity = size;
temp = new char [capacity+1];
for(int i = 0; i<capacity; i++)
temp[i] = number[i];
temp[capacity] = '\0';
delete [] number;
number = temp;
temp = NULL;
}
void MyInt :: grow(){
char * temp = NULL;
capacity += 5;
temp = new char [capacity];
for(int i = 0; i<capacity; i++)
temp[i] = number[i];
delete [] number;
number = temp;
temp = NULL;
}
void MyInt :: sort (int * result, int digs) const{
int carry = 0; int copy = 0; bool shift = 0; int index=0;
for(int i = digs-1; i >= 1; i--){
if(result[i] > 9){
copy = result[i];
result[i] = copy%10;
carry = (copy/10);
result[i-1] += carry;
}
else
continue;
}
carry = 0; copy = 0;
for(int i = 0; i < digs; i++){
result[i] += carry;
if(result[i] > 10 ){
copy = result[i];
result[i] = copy/10;
carry = (copy%10)*10;
}
else if(result[i] == 10){
copy = result[i];
result[i] = copy/10;
shift = 1;
index = i;
break;
}
else
continue;
}
if (shift == 1){
for(int i = digs -1; i > index; i--){
result[i] = result [i -1];
}
result[index+1] = 0;
}
}
// --------PUBLIC FUNCTIONS ----------
MyInt& MyInt :: operator= (const MyInt& copy){
delete[] number; // deallocating old set of nubmers
size = copy.size;
capacity = copy.capacity;
number = new char[capacity+1];
for(int i = 0; i< size; i++)
number[i] = copy.number[i];
number[capacity] = '\0';
shrink();
return *this;
}
MyInt MyInt :: operator+ (const MyInt& x) const{
int capacity_copy = this->capacity + x.capacity; // getting new size of copy array
int loop_size = 0;// this variable will hold the # iterations in copy loop_size
int arr_size = 0;
int number_conversion = 0; // this number will hold values tempmorarily for array math
bool which_arr = 0; // this bool indicates which array is smaller in size
int hold = 0;
char * number_copy = new char [capacity_copy]; // this is the resulting number
for(int i = 0; i<capacity_copy; i++){ // initializing array with 0s
number_copy[i] = 48;
}
if(this->capacity < x.capacity){
loop_size = x.capacity;
arr_size = this->capacity;
which_arr = 1;
} // these if and else set # of iterations in loop
else{
loop_size = this->capacity;
arr_size = x.capacity;
which_arr = 0;
}
for(int i = capacity_copy -1; i > capacity_copy - arr_size -1; i--){ // initializing new array with smaller array
if(which_arr == 0){
number_conversion = x.number[i-loop_size] ;
}
else{
number_conversion = this->number[i-loop_size];
}
number_copy[i] = number_conversion;
}
for(int i = capacity_copy -1; i > capacity_copy - loop_size-1 ; i--){ // adding second array to copy array
if(which_arr == 0){
number_conversion = this->number[i-arr_size] - 48;
}
else{
number_conversion = x.number[i-arr_size] - 48;
}
number_copy[i] += number_conversion;
if (number_copy[i] > 57){
hold = number_copy[i] - 10;
number_copy[i-1] += 1; // carrying over value to index
number_copy[i] = hold;
}
}
int space_count = 0;
for(int i = 0; i < capacity_copy; i++){ // this for loop counts number of empty spaces
if(number_copy[i] == 48){
space_count = i+1;
}
else
break;
}
capacity_copy = capacity_copy - space_count+1; // adjusting capacity and removing un needed space
char * function_number = new char [capacity_copy]; // new array adjusted for size
for(int i = capacity_copy-2; i >= 0; i--){
function_number[i] = number_copy[i + space_count];
}
function_number[capacity_copy-1] = '\0';
MyInt obj(function_number);
delete[] number_copy; delete [] function_number; // deallocating memory
number_copy = NULL; function_number= NULL;
return obj;
}
MyInt MyInt :: operator* (const MyInt& x) const{
int which_arr = 0; // this variable tells which number has more digits -- 0 means they are the same
int top_digits = x.size;
int bottom_digits = x.size;
int total_digits = x.size + this->size;
int * top = 0; int * bottom = 0;
if(this->size > x.size){
which_arr = 1; // first number has more digits
top_digits = this->size;
bottom_digits = x.size;
top = new int [top_digits]; // converting to ints
for(int i = 0; i < top_digits; i++){
top[i] = this->number[i] - 48;
}
bottom = new int [bottom_digits];
for (int i = 0; i < bottom_digits; i++){
bottom[i] = x.number[i] -48;
}
}
else if(this->size < x.size){
which_arr =2;
top_digits = x.size;
bottom_digits = this->size;
top = new int [top_digits]; // converting to ints
for(int i = 0; i < top_digits; i++){
top[i] = x.number[i] - 48;
}
bottom = new int [bottom_digits];
for (int i = 0; i < bottom_digits; i++){
bottom[i] = this->number[i] -48;
}
}
else{
top = new int [top_digits]; // converting to ints
for(int i = 0; i < top_digits; i++){
top[i] = this->number[i] - 48;
}
bottom = new int [bottom_digits];
for (int i = 0; i < bottom_digits; i++){
bottom[i] = x.number[i] -48;
}
}
int ** answer = new int *[bottom_digits]; // dynamic 2d array to hold all rows
for(int i = 0; i < bottom_digits; i++){
answer[i] = new int[total_digits];
}
for(int row = bottom_digits -1; row >= 0; row--){ // initializing all elements
for(int digit = total_digits-1; digit >= 0; digit--){
answer[row][digit] = 0;
}
}
int * final_answer = new int [total_digits]; // this array holds the sums of all the numbers
for (int i = 0; i < total_digits; i++){
final_answer[i] = 0;
}
int counter = 0; int row_counter = 0;
for(int row = bottom_digits -1; row >= 0; row--){
for(int digit = top_digits-1; digit >= 0; digit--){ // adding row by row within 2d array
counter++;
answer[row][(total_digits-1) - counter+ 1 - row_counter] = top[digit] * bottom[row];
}
counter = 0; row_counter++;
sort(answer[row], total_digits); // accounting for carries
}
for(int i = 0; i < bottom_digits; i++){ // storing all the sums in int array
for(int j = 0; j < total_digits; j++){
final_answer[j] += answer[i][j];
}
}
sort(final_answer, total_digits); // accounting for carrys
char * conversion_arr = new char [total_digits+1];
for(int j = 0; j < total_digits; j++){
conversion_arr[j] = 0;
conversion_arr[j] +=final_answer[j] + 48; // converting to char array
}
conversion_arr[total_digits] = '\0'; // adding null char
MyInt WIN(conversion_arr);
for(int i = 0; i < bottom_digits; i++){ // deallocatingall pointes
delete [] answer[i];
}
delete [] answer;
delete [] top;
delete [] bottom;
delete [] final_answer;
delete []conversion_arr ;
return WIN;
}
MyInt& MyInt :: operator++(){
*this = *this + 1;
return *this;
}
MyInt MyInt :: operator++ (int this_was_terribble){
MyInt temp = *this;
*this = *this +1;
return temp;
}
//---------FRIEND FUNCTIONS ------------
ostream& operator<< ( ostream& out,const MyInt& obj){
for (int i = 0; i<obj.size; i++)
out << obj.number[i];
return out;
}
istream& operator >> (istream& in, MyInt& obj){
int cap = 1;
int index = 0; // this keeps track of array index
char * set = new char[cap];
char *copy = 0; // this is used to grow array
char value; // this holds each digit
int check = 0;
char trash;
while(check != 10){
if(index == cap-1){ // growing array based on needed space
cap += 1;
copy = new char[cap];
for(int i = 0; i<index; i++)
copy[i] = set[i];
copy[index] = '\0';
delete[] set;
set = copy;
copy =0;
}
check = in.peek();
if(index == 0){
if (check == 32 || check == 10){
in.get(trash);
}
else if(check < 48 || check > 58) break;
else {
in.get(set[index]);
++index;
}
}
else {
if(check == 32 || check == 10 ){ in.ignore();break;}
else if(check < 48 || check > 57){ break;} // if an element is not a nuber it is isnt touched
else{
in.get(set[index]);
++index;
}
}
}
MyInt that(set); // initializing with char array
obj = that; // copying temporary object data to cin value
delete[]set;
set = 0;
return in;
}
bool operator< (const MyInt& x, const MyInt& y){
bool result = 0; // starting at false
if(x.size < y.size) // if it size of char array is smaller
result = 1;
else if(x.size == y.size){ // checking every index of array
for(int i = 0; i<x.size;i++){
if(x.number[i] < y.number[i]){
result = 1; break;}
}
}
return result;
}
bool operator== (const MyInt&x, const MyInt& y){
bool result = 0;
if (x.size == y.size){
for(int i = 0; i < x.size; i++){
if(x.number[i] == y.number[i])
result = 1;
else
result = 0;
}
}
return result;
}
bool operator> (const MyInt& x, const MyInt& y){
bool result = 1;
if (x<y || x==y)
result = 0;
return result;
}
bool operator!= (const MyInt& x, const MyInt& y){
bool result = 1;
if(x==y)
result = 0;
return result;
}
bool operator<= (const MyInt&x, const MyInt& y){
bool result = 0;
if(x<y || x==y)
result = 1;
return result;
}
bool operator >= (const MyInt&x, const MyInt& y){
bool result = 0;
if(x>y || x== y)
result = 1;
return result;
}