-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigNumber.c
235 lines (220 loc) · 5.66 KB
/
BigNumber.c
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
/*
* @Name: Big/Huge Number Library
* @Author: Max Base
* @Date: 2022-11-01
* @Repository: https://github.com/BaseMax/BigNumberC
*/
#include "BigNumber.h"
/**
* @brief Create a Big Number object
*
* @param size
* @return BigNumber*
*/
BigNumber* createBigNumber(int size) {
BigNumber *bn = malloc(sizeof(BigNumber));
bn->digits = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
bn->digits[i] = 0;
}
bn->size = size;
return bn;
}
/**
* @brief Set the Big Number object
*
* @param bn
* @param digits
* @param size
*/
void setBigNumber(BigNumber *bn, int *digits, int size) {
for (int i = 0; i < size; i++) {
bn->digits[i] = digits[size - i - 1];
}
bn->size = size;
}
/**
* @brief Create a Big Number object and set it digits
*
* @param digits
* @param size
* @return BigNumber*
*/
BigNumber* createAndSetBigNumber(int *digits, int size) {
BigNumber *bn = createBigNumber(size);
setBigNumber(bn, digits, size);
return bn;
}
/**
* @brief Destroy the Big Number object
*
* @param bn
*/
void destroyBigNumber(BigNumber *bn) {
free(bn->digits);
free(bn);
}
/**
* @brief Print the Big Number object
*
* @param bn
*/
void printBigNumber(BigNumber *bn) {
if (bn == NULL) printf("NULL");
else if(bn->size == 0) printf("0");
else {
int isZero = 1;
for (int i = 0; i < bn->size; i++) {
if (bn->digits[i] != 0) {
isZero = 0;
break;
}
}
if (isZero) printf("0");
else for (int i = bn->size - 1; i >= 0; i--) printf("%d", bn->digits[i]);
}
printf("\n");
}
/**
* @brief Add two Big Numbers
*
* @param bn1
* @param bn2
* @return BigNumber*
*/
BigNumber* addBigNumbers(BigNumber *bn1, BigNumber *bn2) {
int size = bn1->size > bn2->size ? bn1->size : bn2->size;
BigNumber *bn3 = createBigNumber(size + 1);
for (int i = 0; i < size; i++) {
int digit1 = i < bn1->size ? bn1->digits[i] : 0;
int digit2 = i < bn2->size ? bn2->digits[i] : 0;
bn3->digits[i] = digit1 + digit2;
}
for (int i = 0; i < size; i++) {
if (bn3->digits[i] >= 10) {
bn3->digits[i] -= 10;
bn3->digits[i + 1]++;
}
}
if (bn3->digits[size] == 0) bn3->size--;
return bn3;
}
/**
* @brief Multiply two Big Numbers
*
* @param bn1
* @param bn2
* @return BigNumber*
*/
BigNumber* multiplyBigNumbers(BigNumber *bn1, BigNumber *bn2) {
BigNumber *bn3 = createBigNumber(bn1->size + bn2->size);
for (int i = 0; i < bn1->size; i++) {
for (int j = 0; j < bn2->size; j++) {
bn3->digits[i + j] += bn1->digits[i] * bn2->digits[j];
}
}
for (int i = 0; i < bn3->size; i++) {
if (bn3->digits[i] >= 10) {
bn3->digits[i + 1] += bn3->digits[i] / 10;
bn3->digits[i] %= 10;
}
}
while (bn3->digits[bn3->size - 1] == 0) bn3->size--;
return bn3;
}
/**
* @brief Deep copy a Big Number
*
* @param bn
* @return BigNumber*
*/
BigNumber* copyBigNumber(BigNumber *bn) {
BigNumber *result = createBigNumber(bn->size);
for (int i = 0; i < bn->size; i++) {
result->digits[i] = bn->digits[i];
}
return result;
}
/**
* @brief Minus two Big Numbers
*
* @param bn1
* @param bn2
* @return BigNumber*
*/
BigNumber* minusBigNumbers(BigNumber *bn1, BigNumber *bn2) {
BigNumber *result = createBigNumber(bn1->size);
for (int i = 0; i < bn1->size; i++) {
result->digits[i] = bn1->digits[i];
}
for (int i = 0; i < bn2->size; i++) {
result->digits[i] -= bn2->digits[i];
}
for (int i = 0; i < bn1->size; i++) {
if (result->digits[i] < 0) {
result->digits[i] += 10;
result->digits[i + 1]--;
}
}
while (result->digits[result->size - 1] == 0) result->size--;
return result;
}
/**
* @brief Divide two Big Numbers
*
* @param bn1
* @param bn2
* @return BigNumber*
*/
BigNumber* divideBigNumbers(BigNumber *bn1, BigNumber *bn2) {
BigNumber *result = createBigNumber(bn1->size);
BigNumber *temp = createBigNumber(bn1->size);
for (int i = bn1->size - 1; i >= 0; i--) {
temp->digits[i] = bn1->digits[i];
while (compareBigNumbers(temp, bn2) >= 0) {
temp = minusBigNumbers(temp, bn2);
result->digits[i]++;
}
}
while (result->digits[result->size - 1] == 0) result->size--;
destroyBigNumber(temp);
return result;
}
/**
* @brief Remainder two Big Numbers
*
* @param bn1
* @param bn2
* @return BigNumber*
*/
BigNumber* remainderBigNumbers(BigNumber *bn1, BigNumber *bn2) {
BigNumber *result = createBigNumber(bn1->size);
BigNumber *temp = createBigNumber(bn1->size);
for (int i = bn1->size - 1; i >= 0; i--) {
temp->digits[i] = bn1->digits[i];
while (compareBigNumbers(temp, bn2) >= 0) {
temp = minusBigNumbers(temp, bn2);
result->digits[i]++;
}
}
while (result->digits[result->size - 1] == 0) result->size--;
destroyBigNumber(temp);
return result;
}
/**
* @brief Compare two Big Numbers
*
* @param bn1
* @param bn2
* @return int
*/
int compareBigNumbers(BigNumber *bn1, BigNumber *bn2) {
for (int i = bn1->size - 1; i >= 0; i--) {
if (bn1->digits[i] > bn2->digits[i]) {
return 1;
} else if (bn1->digits[i] < bn2->digits[i]) {
return -1;
}
}
return 0;
}