1 // 大数的四则运算
2 #include <iostream>
3 #include <string>
4 #include <algorithm>
5 using namespace std;
6
7 class BIGINTEGEROPERATIONS
8 {
9 private:
10 static int COMPARE(string number1, string number2)
11 {
12 int j;
13
14 int length1 = number1.size();
15 int length2 = number2.size();
16
17 if(number1.size() == 0) number1 = "0";
18 if(number2.size() == 0) number2 = "0";
19
20 j = 0;
21 for(int i = 0; i < length1; ++i)
22 {
23 if(number1[i] == ‘0‘) ++j;
24 else break;
25 }
26 number1 = number1.substr(j);
27
28 j = 0;
29 for(int i = 0; i < length2; ++i)
30 {
31 if(number2[i] == ‘0‘) ++j;
32 else break;
33 }
34 number2 = number2.substr(j);
35
36 length1 = number1.size();
37 length2 = number2.size();
38
39 if(length1 > length2)
40 {
41 return 1;
42 }
43 else if(length1 == length2)
44 {
45 if(number1.compare(number2) > 0)
46 {
47 return 1;
48 }
49 else if(number1.compare(number2) == 0)
50 {
51 return 0;
52 }
53 else
54 {
55 return -1;
56 }
57 }
58 else
59 {
60 return -1;
61 }
62
63 return 0;
64 }
65
66 public:
67 static string PLUS(string number1,string number2)
68 {
69 int i;
70 int length1 = number1.size();
71 int length2 = number2.size();
72
73 string result="";
74
75 reverse(number1.begin(), number1.end());
76 reverse(number2.begin(), number2.end());
77
78 for(i = 0; i < length1 && i < length2; i++)
79 {
80 char c = (char)(number1[i] + number2[i] - 48);
81 result = result + c;
82 }
83
84 while(i < length1)
85 {
86 result = result + number1[i];
87 ++i;
88 }
89
90 while(i < length2)
91 {
92 result = result + number2[i];
93 ++i;
94 }
95
96 int carry = 0;
97 for(i = 0; i < (int)result.size(); ++i)
98 {
99 int value = result[i] - 48 + carry;
100 result[i] = (char)(value % 10 + 48);
101 carry = value / 10;
102 }
103
104 if(carry !=0 )
105 {
106 result = result + (char)(carry + 48);
107 }
108
109 for(i = result.size() - 1; i >= 0; i--)
110 {
111 if(result[i] != ‘0‘) break;
112 }
113
114 result = result.substr(0, i + 1);
115
116 reverse(result.begin(), result.end());
117 if(result.length() == 0) result = "0";
118 return result;
119 }
120
121
122 static string MINUS(string number1,string number2)
123 {
124 int i;
125 string result = "";
126
127 int length1 = number1.size();
128 int length2 = number2.size();
129
130 if(COMPARE(number2,number1) > 0)
131 {
132 return "-" + MINUS(number2, number1);
133 }
134
135 reverse(number1.begin(),number1.end());
136 reverse(number2.begin(),number2.end());
137
138 for(i = 0; i < length1 && i < length2; i++)
139 {
140 char c = number1[i] - number2[i] + 48;
141 result = result + c;
142 }
143
144 if(i < length1)
145 {
146 for(; i < length1; i++)
147 {
148 result = result + number1[i];
149 }
150 }
151
152 int carry = 0;
153 for(i = 0; i < (int)result.length(); i++)
154 {
155 int value = result[i] - 48 + carry;
156 if(value < 0)
157 {
158 value = value + 10;
159 carry = -1;
160 }
161 else carry = 0;
162 result[i]=(char)(value + 48);
163 }
164
165 for(i = result.size() - 1; i >= 0; i--)
166 {
167 if(result[i] != ‘0‘)break;
168 }
169
170 result = result.substr(0, i+1);
171
172 reverse(result.begin(), result.end());
173 if(result.length()==0) result = "0";
174 return result;
175 }
176
177
178 static string MULTIPLY(string number1, string number2)
179 {
180 int i, j;
181 int *iresult;
182 int length1 = number1.size();
183 int length2 = number2.size();
184 string result = "";
185
186 reverse(number1.begin(), number1.end());
187 reverse(number2.begin(), number2.end());
188
189 iresult = (int*)malloc(sizeof(int) * (length1 + length2 + 1));
190 memset(iresult, 0, sizeof(int) * (length1 + length2 + 1));
191
192 for(i = 0; i < length1; i++)
193 {
194 for(j = 0; j < length2; j++)
195 {
196 iresult[i+j] += ((number1[i] - 48) * (number2[j] - 48));
197 }
198 }
199
200 int carry = 0;
201 for(i = 0; i < length1 + length2; i++)
202 {
203 int value = iresult[i] + carry;
204 iresult[i] = value % 10;
205 carry = value / 10;
206 }
207
208 for(i = length1 + length2 - 1; i >= 0; i--)
209 {
210 if(iresult[i] != 0)break;
211 }
212
213 for(; i >= 0; i--)
214 {
215 result = result + (char)(iresult[i]+48);
216 }
217
218 free(iresult);
219
220 if(result == "") result = "0";
221 return result;
222 }
223
224
225 // 缺省地,商数向下取整, floatpoint用于指定保留小数点的位数
226 static string DIVIDE(string number1, string number2, int floatpoint = 0)
227 {
228 int i, j, pos;
229 string result = "";
230 string tempstr = "";
231 int length1 = number1.size();
232 int length2 = number2.size();
233
234 if((COMPARE(number2, number1) > 0) && (floatpoint == 0))
235 {
236 return "0";
237 }
238
239 tempstr = number1.substr(0, length2);
240 pos = length2 - 1;
241
242 while(pos < length1)
243 {
244 int quotient = 0;
245 while(COMPARE(tempstr, number2) >= 0)
246 {
247 quotient++;
248 tempstr = MINUS(tempstr, number2);
249 }
250
251 result = result + (char)(quotient + 48);
252 pos++;
253 if(pos < length1)
254 {
255 tempstr += number1[pos];
256 }
257 }
258
259 if(floatpoint > 0)
260 {
261 result += ‘.‘;
262 string stmp = "1";
263 int itmp = 0;
264 for(int k = 0; k < floatpoint; ++k)
265 {
266 stmp += ‘0‘;
267 if(COMPARE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2) < 0)
268 {
269 result += ‘0‘;
270 ++itmp;
271 }
272 }
273
274 string temp = DIVIDE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2);
275 if(temp[0] != ‘0‘) result += temp;
276 }
277
278 j = result.size();
279 for(i = 0; i < j; i++)
280 {
281 if(result[i] != ‘0‘) break;
282 }
283
284 result = result.substr(i, j);
285
286 return result;
287 }
288
289 static string MOD(string number1, string number2)
290 {
291 if(COMPARE(number2, number1) > 0)
292 {
293 return number1;
294 }
295 else if(COMPARE(number2, number1) == 0)
296 {
297 return "0";
298 }
299 else
300 {
301 return MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2));
302 }
303 }
304 };
305
306 int main(int argc, char* argv[])
307 {
308 string str1 = "9999999999999999999999999999999999999999";
309 string str2 = "9998999899989998999899989998999899989998";
310
311 cout << BIGINTEGEROPERATIONS::PLUS(str1, str2) << endl;
312 cout << "===============" << endl;
313 cout << BIGINTEGEROPERATIONS::MINUS(str1, str2) << endl;
314 cout << "===============" << endl;
315 cout << BIGINTEGEROPERATIONS::MULTIPLY(str1, str2) << endl;
316 cout << "===============" << endl;
317 cout << BIGINTEGEROPERATIONS::DIVIDE(str1, str2, 4) << endl;
318 cout << "===============" << endl;
319 cout << BIGINTEGEROPERATIONS::MOD(str1, str2) << endl;
320
321 return 0;
322 }