-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigInteger.cpp
231 lines (215 loc) · 6.84 KB
/
bigInteger.cpp
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
// Hexadecimal Big Integer program with everything hard-coded, by 110064533
#include <bits/stdc++.h>
#define MAX_DIGIT 300 // max digit allowed is 300
void Hex2Int(int *result, char *str) {
const int n = strlen(str);
memset(result, 0, sizeof(int)*MAX_DIGIT);
for (int i = n - 1; i >= 0; i--) {
if (str[i] >= '0' && str[i] <= '9') {
result[n - i - 1] = str[i] - '0';
}
else if (str[i] >= 'a' && str[i] <= 'z') {
result[n - i - 1] = str[i] - 'a' + 10;
}
else if (str[i] >= 'A' && str[i] <= 'Z') {
result[n - i - 1] = str[i] - 'A' + 10;
}
}
}
// interface functions
void Print(int *result) {
// Print function, print out the bid number in hex
int i = MAX_DIGIT - 1;
while (i > 0 && result[i] == 0) {
i--; // skip leading zero, if any
}
while (i >= 0) {
// print out 10~15
if (result[i] == 10) {
std::cout << 'a';
}
else if (result[i] == 11) {
std::cout << 'b';
}
else if (result[i] == 12) {
std::cout << 'c';
}
else if (result[i] == 13) {
std::cout << 'd';
}
else if (result[i] == 14) {
std::cout << 'e';
}
else if (result[i] == 15) {
std::cout << 'f';
}
else {
// print out 0~9
std::cout << result[i];
}
i--;
}
std::cout << "\n";
}
void Assign(int *result, int *source) {
// Assign function, assign source to result
memset(result, 0, sizeof(int)*MAX_DIGIT);
for (int i = MAX_DIGIT - 1; i >= 0; i--) {
result[i] = source[i];
}
}
// arithmetic operators
int Compare(int *a, int *b) {
// Compare function, if a > b return 1,
// if a < b return -1, if a == b return 0
int i = MAX_DIGIT - 1;
while (i > 0 && a[i] == b[i]) {
i--;
}
if (a[i] - b[i] == 0) {
return 0;
}
return (a[i] - b[i] > 0) ? 1 : -1;
}
void Add(int *result, int *a, int *b) {
// Addition, result = a + b
memset(result, 0, sizeof(int)*MAX_DIGIT);
for (int i = 0, carry = 0; i < MAX_DIGIT; i++) {
result[i] = a[i] + b[i] + carry;
carry = result[i] / 16;
result[i] %= 16;
}
}
bool Sub(int *result, int *a, int *b) {
// Substraction, if a > b result= a - b return sign = false
// if a < b result = b - a return sign = true
memset(result, 0, sizeof(int)*MAX_DIGIT);
bool sign = false;
// case 1. a > B
if (Compare(a, b) == 1) {
for (int i = 0, borrow = 0; i < MAX_DIGIT; i++) {
result[i] = a[i] - b[i] - borrow;
// result < 0 means not enough, need to borrow
if (result[i] < 0) {
borrow = 1;
result[i] += 16; // lend result 16
}
// result >= 0 means enough or equal
else if (result[i] >= 0) {
borrow = 0;
}
}
// a - b > 0 is positive, sign remains false
return sign;
}
// case 2. a < b
else if (Compare(a, b) == -1) {
for (int i = 0, borrow = 0; i < MAX_DIGIT; i++) {
result[i] = b[i] - a[i] - borrow;
// result < 0 means not enough, need to borrow
if (result[i] < 0) {
borrow = 1;
result[i] += 16; // lend result 16
}
// result >= 0 means enough or equal
else if (result[i] >= 0) {
borrow = 0;
}
}
// a - b < 0 is negative, sign changed into true
sign = true;
return sign;
}
// case 3. a == b
return 0;
}
void Mul(int *result, int *a, int *b) {
// Multiplication, result = a * b
memset(result, 0, sizeof(int)*MAX_DIGIT);
for (int i = 0; i < MAX_DIGIT; i++) {
if (a[i] == 0) {
continue;
}
for (int j = 0; i + j < MAX_DIGIT; j++) {
result[i + j] += a[i] * b[j];
}
}
// if result[i] >= 16 means overflow, need to add a carry
for (int i = 0, carry = 0; i < MAX_DIGIT; i++) {
result[i] += carry;
carry = result[i] / 16;
result[i] %= 16;
}
}
void Div(int *divide, int *remain, int *a, int *b) {
memset(divide, 0, sizeof(int)*MAX_DIGIT);
memset(remain, 0, sizeof(int)*MAX_DIGIT);
while (Sub(remain, a, b) == false) {
// remain = a - b
Assign(a, remain); // a = remain
divide[0]++;
for (int i = 0; i < MAX_DIGIT; i++) {
// check for carry
if (divide[i] == 16) {
// if any element == 16
divide[i] = 0; // set it to 0
divide[i + 1] += 1; // add the carry to next digit
}
}
}
}
int main() {
char *str1 = (char*)calloc(MAX_DIGIT, sizeof(char)); // input a
char *str2 = (char*)calloc(MAX_DIGIT, sizeof(char)); // input b
// store str1 and str2 in array a and b
int *a = (int*)calloc(MAX_DIGIT, sizeof(int));
int *b = (int*)calloc(MAX_DIGIT, sizeof(int));
// store the result of addition, substraction,
// multiplication, quotient, and remainder
int *add = (int*)calloc(MAX_DIGIT, sizeof(int));
int *sub = (int*)calloc(MAX_DIGIT, sizeof(int));
int *mul = (int*)calloc(MAX_DIGIT, sizeof(int));
int *quo = (int*)calloc(MAX_DIGIT, sizeof(int));
int *rem = (int*)calloc(MAX_DIGIT, sizeof(int));
// sample input
/* input constrains:
* (1) a >= b
* (2) len(a) <= len(b) + 5
* otherwise the result would either be incorrect
* or exceeded constant execution time requirement. */
strcpy(str1, "f1245AB3341Ff3461818881767676819EE");
strcpy(str2, "ffa24387539639853800bbeCbcb494990");
std::cout << "string a = " << str1 << "\n";
std::cout << "string b = " << str2 << "\n";
// convert hex input char array into decimal int array
Hex2Int(a, str1);
Hex2Int(b, str2);
// check if we read the input string correctly
std::cout << "integer a = "; Print(a);
std::cout << "integer b = "; Print(b);
// testing addition
Add(add, a, b);
std::cout << "a + b = "; Print(add);
// testing substraction
Sub(sub, a, b);
std::cout << "a - b = "; Print(sub);
// testing multiplication
Mul(mul, a, b);
std::cout << "a * b = "; Print(mul);
// testing integer divsion
Div(quo, rem, a, b);
std::cout << "a / b = "; Print(quo);
// testing modulus
// Div(quo, rem, a, b);
std::cout << "a % b = "; Print(a);
free(str1);
free(str2);
free(a);
free(b);
free(add);
free(sub);
free(mul);
free(quo);
free(rem);
return 0;
}