forked from chenzhuowansui/big_num_cal
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbig_data_int.cpp
More file actions
202 lines (166 loc) · 5.15 KB
/
Copy pathbig_data_int.cpp
File metadata and controls
202 lines (166 loc) · 5.15 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
#include <iostream>
#include <exception>
#include "big_data_int.h"
big_data_int operator *(const big_data_int &a, const big_data_int &b) {
const int len_a = a.length();
const int len_b = b.length();
const int len = len_a + len_b;
const char *num_a = a.data();
const char *num_b = b.data();
//Define an int array to store the mediate result, whose length is len_a + len_b
int *mid = new int[len];
for (int i = 0; i < len; i++) {
mid[i] = 0;
}
for (int i = 0; i < len_a; i++)
for(int j = 0; j < len_b; j++) {
mid[len - 1 -i -j] += (num_a[len_a - 1 -i] - '0') * (num_b[len_b - 1 -j] - '0');
}
//Deal with over bit
for(int i = len -1; i > 0; i--) {
if (mid[i] > 9) {
int tmp = mid[i];
mid[i] = tmp % 10;
mid[i-1] += tmp / 10;
}
}
string s("");
bool first_non_zero_found = false;
for (int i = 0; i < len; i++) {
//Skip '0' at the head
if (mid[i] == 0 && !first_non_zero_found) {
continue;
}
first_non_zero_found = true;
s += mid[i] + '0';
}
//Add the terminal symbol for char*
s = s.length()? s + string("\0") : string("0\0");
delete[] mid;
return big_data_int(s);
}
big_data_int operator /(const big_data_int &a, const big_data_int &b) {
big_data_int result("0");
big_data_int first(a), second(b);
if (b.is_zero()) {
cout << "[ERROR]:!!!Divided By Zero!!!" << endl;
return big_data_int("");
}
while (first.gt(second)) {
string rst("");
for (int i = 0; i < (first.length() - second.length() - 1); i++) {
rst += string("0");
}
if (rst.length()) {
rst = string("1") + rst;
big_data_int step(rst);
while (first.gt(step * second)) {
first = first - step * second;
result = result + step;
}
} else {
break;
}
}
while (first.gt(second)) {
first = first - second;
result = result + big_data_int("1");
}
return result;
}
big_data_int operator %(const big_data_int &a, const big_data_int &b) {
return a - a/b*b;
}
big_data_int operator -(const big_data_int &a, const big_data_int &b) {
//Make sure first arg greater than the second arg, if not, reverse the args
if (!a.gt(b)) {
return big_data_int(big_data_int(string("-")), (b - a));
}
const int len_a = a.length();
const int len_b = b.length();
const int len_max = (len_a > len_b)? len_a : len_b;
const int len_min = (len_a > len_b)? len_b : len_a;
const char *num_a = a.data();
const char *num_b = b.data();
int i = 0;
//Define an int array to store the mediate result, whose length is max(len_a, len_b) + 1
int *mid = new int[len_max];
for (i = 0; i < len_max; i++) {
mid[i] = 0;
}
for (i = 0; i < len_min; i++) {
mid[len_max - 1 -i] = (num_a[len_a - 1 -i] - '0') - (num_b[len_b - 1 -i] - '0');
}
while (i < len_a) {
mid[len_max - 1 -i] = (num_a[len_a - 1 -i] - '0');
i++;
}
//Deal with over bit
for(i = len_max -1; i > 0; i--) {
if (mid[i] < 0) {
mid[i] += 10;
mid[i-1] -= 1;
}
}
string s("");
bool first_non_zero_found = false;
for (i = 0; i < len_max; i++) {
//Skip '0' at the head
if (mid[i] == 0 && !first_non_zero_found) {
continue;
}
first_non_zero_found = true;
s += mid[i] + '0';
}
//Add the terminal symbol for char*
s = s.length()? s + string("\0") : string("0\0");
delete[] mid;
return big_data_int(s);
}
big_data_int operator +(const big_data_int &a, const big_data_int &b) {
const int len_a = a.length();
const int len_b = b.length();
const int len_max = (len_a > len_b)? len_a : len_b;
const int len_min = (len_a > len_b)? len_b : len_a;
const char *num_a = a.data();
const char *num_b = b.data();
int i = 0;
//Define an int array to store the mediate result, whose length is max(len_a, len_b) + 1
int *mid = new int[len_max + 1];
for (i = 0; i <= len_max; i++) {
mid[i] = 0;
}
for (i = 0; i < len_min; i++) {
mid[len_max - i] = (num_a[len_a - 1 -i] - '0') + (num_b[len_b - 1 -i] - '0');
}
while (i < len_a) {
mid[len_max - i] += (num_a[len_a - 1 -i] - '0');
i++;
}
while (i < len_b) {
mid[len_max - i] += (num_b[len_b - 1 -i] - '0');
i++;
}
//Deal with over bit
for(i = len_max; i > 0; i--) {
if (mid[i] > 9) {
int tmp = mid[i];
mid[i] = tmp % 10;
mid[i-1] += tmp / 10;
}
}
string s("");
bool first_non_zero_found = false;
for (i = 0; i <= len_max; i++) {
//Skip '0' at the head
if (mid[i] == 0 && !first_non_zero_found) {
continue;
}
first_non_zero_found = true;
s += mid[i] + '0';
}
//Add the terminal symbol for char*
s = s.length()? s + string("\0") : string("0\0");
delete[] mid;
return big_data_int(s);
}