1 | // BigInt.cc |
---|
2 | |
---|
3 | #ifndef BIG_INT_CC |
---|
4 | #define BIG_INT_CC |
---|
5 | |
---|
6 | #include "BigInt.h" |
---|
7 | |
---|
8 | // |
---|
9 | // Konstruktoren fuer die Klasse BigInt |
---|
10 | // |
---|
11 | |
---|
12 | // Default-Konstruktor |
---|
13 | |
---|
14 | BigInt::BigInt() |
---|
15 | { |
---|
16 | mpz_init(value); |
---|
17 | } |
---|
18 | |
---|
19 | BigInt::BigInt(long int a) |
---|
20 | { |
---|
21 | mpz_init(value); |
---|
22 | mpz_set_si(value,a); |
---|
23 | } |
---|
24 | |
---|
25 | BigInt::BigInt(unsigned long int a) |
---|
26 | { |
---|
27 | mpz_init(value); |
---|
28 | mpz_set_ui(value,a); |
---|
29 | } |
---|
30 | |
---|
31 | BigInt::BigInt(int a) |
---|
32 | { |
---|
33 | mpz_init(value); |
---|
34 | mpz_set_si(value,(long int)a); |
---|
35 | } |
---|
36 | |
---|
37 | BigInt::BigInt(unsigned int a) |
---|
38 | { |
---|
39 | mpz_init(value); |
---|
40 | mpz_set_ui(value,(unsigned long)a); |
---|
41 | } |
---|
42 | |
---|
43 | BigInt::BigInt(short int a) |
---|
44 | { |
---|
45 | mpz_init(value); |
---|
46 | mpz_set_si(value,(long int)a); |
---|
47 | } |
---|
48 | |
---|
49 | BigInt::BigInt(unsigned short int a) |
---|
50 | { |
---|
51 | mpz_init(value); |
---|
52 | mpz_set_ui(value,(unsigned long int)a); |
---|
53 | } |
---|
54 | |
---|
55 | BigInt::BigInt(char a) |
---|
56 | { |
---|
57 | mpz_init(value); |
---|
58 | mpz_set_si(value,(long int)a); |
---|
59 | } |
---|
60 | |
---|
61 | BigInt::BigInt(unsigned char a) |
---|
62 | { |
---|
63 | mpz_init(value); |
---|
64 | mpz_set_ui(value,(unsigned long int)a); |
---|
65 | } |
---|
66 | |
---|
67 | // Copy-Konstruktor |
---|
68 | |
---|
69 | BigInt::BigInt(const BigInt& a) |
---|
70 | { |
---|
71 | mpz_init(value); |
---|
72 | mpz_set(value,a.value); |
---|
73 | } |
---|
74 | |
---|
75 | // Destruktor |
---|
76 | BigInt::~BigInt() |
---|
77 | { |
---|
78 | mpz_clear(value); |
---|
79 | } |
---|
80 | |
---|
81 | // |
---|
82 | // Zuweisungsoperatoren |
---|
83 | // |
---|
84 | |
---|
85 | BigInt& BigInt::operator=(long int a) |
---|
86 | { |
---|
87 | mpz_set_si(value,a); |
---|
88 | return *this; |
---|
89 | } |
---|
90 | |
---|
91 | BigInt& BigInt::operator=(unsigned long int a) |
---|
92 | { |
---|
93 | mpz_set_ui(value,a); |
---|
94 | return *this; |
---|
95 | } |
---|
96 | |
---|
97 | BigInt& BigInt::operator=(int a) |
---|
98 | { |
---|
99 | mpz_set_si(value,(long int) a); |
---|
100 | return *this; |
---|
101 | } |
---|
102 | |
---|
103 | BigInt& BigInt::operator=(unsigned int a) |
---|
104 | { |
---|
105 | mpz_set_ui(value,(unsigned long int) a); |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | BigInt& BigInt::operator=(short int a) |
---|
110 | { |
---|
111 | mpz_set_si(value,(long int) a); |
---|
112 | return *this; |
---|
113 | } |
---|
114 | |
---|
115 | BigInt& BigInt::operator=(unsigned short int a) |
---|
116 | { |
---|
117 | mpz_set_ui(value,(unsigned long int) a); |
---|
118 | return *this; |
---|
119 | } |
---|
120 | |
---|
121 | BigInt& BigInt::operator=(char a) |
---|
122 | { |
---|
123 | mpz_set_si(value,(long int) a); |
---|
124 | return *this; |
---|
125 | } |
---|
126 | |
---|
127 | BigInt& BigInt::operator=(unsigned char a) |
---|
128 | { |
---|
129 | mpz_set_ui(value,(unsigned long int) a); |
---|
130 | return *this; |
---|
131 | } |
---|
132 | |
---|
133 | BigInt& BigInt::operator=(const BigInt& a) |
---|
134 | { |
---|
135 | mpz_set(value,a.value); |
---|
136 | return *this; |
---|
137 | } |
---|
138 | |
---|
139 | // |
---|
140 | // Type-Conversion |
---|
141 | // |
---|
142 | |
---|
143 | BigInt::operator bool() |
---|
144 | { |
---|
145 | if (mpz_sgn(value)) return true; |
---|
146 | return false; |
---|
147 | } |
---|
148 | |
---|
149 | BigInt::operator long int() |
---|
150 | { |
---|
151 | long int ret_val; |
---|
152 | ret_val=mpz_get_si(value); |
---|
153 | return ret_val; |
---|
154 | } |
---|
155 | |
---|
156 | BigInt::operator unsigned long int() |
---|
157 | { |
---|
158 | long int ret_val; |
---|
159 | ret_val=mpz_get_ui(value); |
---|
160 | return ret_val; |
---|
161 | } |
---|
162 | |
---|
163 | BigInt::operator int() |
---|
164 | { |
---|
165 | int ret_val=operator long int(); |
---|
166 | return ret_val; |
---|
167 | } |
---|
168 | |
---|
169 | BigInt::operator unsigned int() |
---|
170 | { |
---|
171 | unsigned int ret_val=operator unsigned long int(); |
---|
172 | return ret_val; |
---|
173 | } |
---|
174 | |
---|
175 | BigInt::operator short int() |
---|
176 | { |
---|
177 | short int ret_val=operator long int(); |
---|
178 | return ret_val; |
---|
179 | } |
---|
180 | |
---|
181 | BigInt::operator unsigned short int() |
---|
182 | { |
---|
183 | unsigned short int ret_val=operator unsigned long int(); |
---|
184 | return ret_val; |
---|
185 | } |
---|
186 | |
---|
187 | BigInt::operator char() |
---|
188 | { |
---|
189 | char ret_val=operator long int(); |
---|
190 | return ret_val; |
---|
191 | } |
---|
192 | |
---|
193 | BigInt::operator unsigned char() |
---|
194 | { |
---|
195 | unsigned char ret_val=operator unsigned long int(); |
---|
196 | return ret_val; |
---|
197 | } |
---|
198 | |
---|
199 | // |
---|
200 | // unary arithmetic operators |
---|
201 | // |
---|
202 | |
---|
203 | // unary Minus |
---|
204 | BigInt BigInt::operator-() |
---|
205 | { |
---|
206 | BigInt erg; |
---|
207 | mpz_neg(erg.value,value); |
---|
208 | return erg; |
---|
209 | } |
---|
210 | |
---|
211 | // += Operator |
---|
212 | BigInt& BigInt::operator+=(const BigInt &a) |
---|
213 | { |
---|
214 | BigInt aux; |
---|
215 | mpz_set(aux.value,value); |
---|
216 | mpz_add(value,aux.value,a.value); |
---|
217 | return *this; |
---|
218 | } |
---|
219 | |
---|
220 | // -= Operator |
---|
221 | BigInt& BigInt::operator-=(const BigInt &a) |
---|
222 | { |
---|
223 | BigInt aux; |
---|
224 | mpz_set(aux.value,value); |
---|
225 | mpz_sub(value,aux.value,a.value); |
---|
226 | return *this; |
---|
227 | } |
---|
228 | |
---|
229 | // *= Operator |
---|
230 | BigInt& BigInt::operator*=(const BigInt &a) |
---|
231 | { |
---|
232 | BigInt aux; |
---|
233 | mpz_set(aux.value,value); |
---|
234 | mpz_mul(value,aux.value,a.value); |
---|
235 | return *this; |
---|
236 | } |
---|
237 | |
---|
238 | // /= Operator |
---|
239 | BigInt& BigInt::operator/=(const BigInt &a) |
---|
240 | { |
---|
241 | BigInt aux; |
---|
242 | mpz_set(aux.value,value); |
---|
243 | mpz_fdiv_q(value,aux.value,a.value); |
---|
244 | return *this; |
---|
245 | } |
---|
246 | |
---|
247 | // prefix ++ |
---|
248 | BigInt& BigInt::operator++() |
---|
249 | { |
---|
250 | BigInt aux; |
---|
251 | mpz_set(aux.value,value); |
---|
252 | mpz_add(value,aux.value,BigInt(1).value); |
---|
253 | return *this; |
---|
254 | } |
---|
255 | |
---|
256 | // postfix ++ |
---|
257 | BigInt BigInt::operator++(int) |
---|
258 | { |
---|
259 | BigInt erg; |
---|
260 | mpz_add(erg.value,value,BigInt(1).value); |
---|
261 | return erg; |
---|
262 | } |
---|
263 | |
---|
264 | // prefix -- |
---|
265 | BigInt& BigInt::operator--() |
---|
266 | { |
---|
267 | BigInt aux; |
---|
268 | mpz_set(aux.value,value); |
---|
269 | mpz_sub(value,aux.value,BigInt(1).value); |
---|
270 | return *this; |
---|
271 | } |
---|
272 | |
---|
273 | // postfix -- |
---|
274 | BigInt BigInt::operator--(int) |
---|
275 | { |
---|
276 | BigInt erg; |
---|
277 | mpz_add(erg.value,value,BigInt(1).value); |
---|
278 | return erg; |
---|
279 | } |
---|
280 | |
---|
281 | BigInt operator-(const BigInt& r) |
---|
282 | { |
---|
283 | BigInt erg; |
---|
284 | mpz_neg(erg.value,r.value); |
---|
285 | return erg; |
---|
286 | } |
---|
287 | |
---|
288 | // |
---|
289 | // Vergleichsoperatorn |
---|
290 | // |
---|
291 | |
---|
292 | bool operator<(const BigInt& a,const BigInt& b) |
---|
293 | { |
---|
294 | if (mpz_cmp(a.value,b.value)<0) return true; |
---|
295 | return false; |
---|
296 | } |
---|
297 | |
---|
298 | bool operator<=(const BigInt& a,const BigInt& b) |
---|
299 | { |
---|
300 | if (mpz_cmp(a.value,b.value)>0) return false; |
---|
301 | return true; |
---|
302 | } |
---|
303 | |
---|
304 | bool operator>(const BigInt& a,const BigInt& b) |
---|
305 | { |
---|
306 | if (mpz_cmp(a.value,b.value)>0) return true; |
---|
307 | return false; |
---|
308 | } |
---|
309 | |
---|
310 | bool operator>=(const BigInt& a,const BigInt& b) |
---|
311 | { |
---|
312 | if (mpz_cmp(a.value,b.value)<0) return false; |
---|
313 | return true; |
---|
314 | } |
---|
315 | |
---|
316 | bool operator==(const BigInt& a,const BigInt& b) |
---|
317 | { |
---|
318 | if (!mpz_cmp(a.value,b.value)) return true; |
---|
319 | return false; |
---|
320 | } |
---|
321 | |
---|
322 | bool operator!=(const BigInt& a,const BigInt& b) |
---|
323 | { |
---|
324 | if (!mpz_cmp(a.value,b.value)) return false; |
---|
325 | return true; |
---|
326 | } |
---|
327 | |
---|
328 | bool operator<(const long int& a,const BigInt& b) |
---|
329 | { |
---|
330 | if (mpz_cmp(BigInt(a).value,b.value)<0) return true; |
---|
331 | return false; |
---|
332 | } |
---|
333 | |
---|
334 | bool operator<=(const long int& a,const BigInt& b) |
---|
335 | { |
---|
336 | if (mpz_cmp(BigInt(a).value,b.value)>0) return false; |
---|
337 | return true; |
---|
338 | } |
---|
339 | |
---|
340 | bool operator>(const long int& a,const BigInt& b) |
---|
341 | { |
---|
342 | if (mpz_cmp(BigInt(a).value,b.value)>0) return true; |
---|
343 | return false; |
---|
344 | } |
---|
345 | |
---|
346 | bool operator>=(const long int& a,const BigInt& b) |
---|
347 | { |
---|
348 | if (mpz_cmp(BigInt(a).value,b.value)<0) return false; |
---|
349 | return true; |
---|
350 | } |
---|
351 | |
---|
352 | bool operator==(const long int& a,const BigInt& b) |
---|
353 | { |
---|
354 | if (!mpz_cmp(BigInt(a).value,b.value)) return true; |
---|
355 | return false; |
---|
356 | } |
---|
357 | |
---|
358 | bool operator!=(const long int& a,const BigInt& b) |
---|
359 | { |
---|
360 | if (!mpz_cmp(BigInt(a).value,b.value)) return false; |
---|
361 | return true; |
---|
362 | } |
---|
363 | |
---|
364 | bool operator<(const BigInt& a,const long int& b) |
---|
365 | { |
---|
366 | if (mpz_cmp(a.value,BigInt(b).value)<0) return true; |
---|
367 | return false; |
---|
368 | } |
---|
369 | |
---|
370 | bool operator<=(const BigInt& a,const long int& b) |
---|
371 | { |
---|
372 | if (mpz_cmp(a.value,BigInt(b).value)>0) return false; |
---|
373 | return true; |
---|
374 | } |
---|
375 | |
---|
376 | bool operator>(const BigInt& a,const long int& b) |
---|
377 | { |
---|
378 | if (mpz_cmp(a.value,BigInt(b).value)>0) return true; |
---|
379 | return false; |
---|
380 | } |
---|
381 | |
---|
382 | bool operator>=(const BigInt& a,const long int& b) |
---|
383 | { |
---|
384 | if (mpz_cmp(a.value,BigInt(b).value)<0) return false; |
---|
385 | return true; |
---|
386 | } |
---|
387 | |
---|
388 | bool operator==(const BigInt& a,const long int& b) |
---|
389 | { |
---|
390 | if (!mpz_cmp(a.value,BigInt(b).value)) return true; |
---|
391 | return false; |
---|
392 | } |
---|
393 | |
---|
394 | bool operator!=(const BigInt& a,const long int& b) |
---|
395 | { |
---|
396 | if (!mpz_cmp(a.value,BigInt(b).value)) return false; |
---|
397 | return true; |
---|
398 | } |
---|
399 | |
---|
400 | // |
---|
401 | // die Grundoperationen |
---|
402 | // |
---|
403 | |
---|
404 | BigInt operator+(const BigInt& a,const BigInt &b) |
---|
405 | { |
---|
406 | BigInt erg(a); |
---|
407 | return erg+=b; |
---|
408 | } |
---|
409 | |
---|
410 | BigInt operator-(const BigInt& a,const BigInt &b) |
---|
411 | { |
---|
412 | BigInt erg(a); |
---|
413 | return erg-=b; |
---|
414 | } |
---|
415 | |
---|
416 | BigInt operator*(const BigInt& a,const BigInt &b) |
---|
417 | { |
---|
418 | BigInt erg(a); |
---|
419 | return erg*=b; |
---|
420 | } |
---|
421 | |
---|
422 | BigInt operator/(const BigInt& a,const BigInt &b) |
---|
423 | { |
---|
424 | BigInt erg(a); |
---|
425 | return erg/=b; |
---|
426 | } |
---|
427 | |
---|
428 | BigInt operator+(const long int& a,const BigInt &b) |
---|
429 | { |
---|
430 | BigInt erg(a); |
---|
431 | return erg+=b; |
---|
432 | } |
---|
433 | |
---|
434 | BigInt operator-(const long int& a,const BigInt &b) |
---|
435 | { |
---|
436 | BigInt erg(a); |
---|
437 | return erg-=b; |
---|
438 | } |
---|
439 | |
---|
440 | BigInt operator*(const long int& a,const BigInt &b) |
---|
441 | { |
---|
442 | BigInt erg(a); |
---|
443 | return erg*=b; |
---|
444 | } |
---|
445 | |
---|
446 | BigInt operator/(const long int& a,const BigInt &b) |
---|
447 | { |
---|
448 | BigInt erg(a); |
---|
449 | return erg/=b; |
---|
450 | } |
---|
451 | |
---|
452 | BigInt operator+(const BigInt& a,const long int &b) |
---|
453 | { |
---|
454 | BigInt erg(a); |
---|
455 | return erg+=BigInt(b); |
---|
456 | } |
---|
457 | |
---|
458 | BigInt operator-(const BigInt& a,const long int &b) |
---|
459 | { |
---|
460 | BigInt erg(a); |
---|
461 | return erg-=BigInt(b); |
---|
462 | } |
---|
463 | |
---|
464 | BigInt operator*(const BigInt& a,const long int &b) |
---|
465 | { |
---|
466 | BigInt erg(a); |
---|
467 | return erg*=BigInt(b); |
---|
468 | } |
---|
469 | |
---|
470 | BigInt operator/(const BigInt& a,const long int &b) |
---|
471 | { |
---|
472 | BigInt erg(a); |
---|
473 | return erg/=BigInt(b); |
---|
474 | } |
---|
475 | |
---|
476 | // liefert das Vorzeichen |
---|
477 | int sgn(const BigInt& a) |
---|
478 | { |
---|
479 | return mpz_sgn(a.value); |
---|
480 | } |
---|
481 | |
---|
482 | // liefert den Absolutbetrag |
---|
483 | BigInt abs(const BigInt& a) |
---|
484 | { |
---|
485 | BigInt erg; |
---|
486 | if (mpz_sgn(a.value)<0) |
---|
487 | mpz_neg(erg.value,a.value); |
---|
488 | else |
---|
489 | mpz_set(erg.value,a.value); |
---|
490 | return erg; |
---|
491 | } |
---|
492 | |
---|
493 | #endif // BIG_INT_CC |
---|