source: git/IntegerProgramming/BigInt.cc @ b08831

spielwiese
Last change on this file since b08831 was 6ba162, checked in by Hans Schönemann <hannes@…>, 24 years ago
This commit was generated by cvs2svn to compensate for changes in r4282, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@4283 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.8 KB
Line 
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
14BigInt::BigInt()
15{
16  mpz_init(value);
17}
18
19BigInt::BigInt(long int a)
20{
21  mpz_init(value);
22  mpz_set_si(value,a);
23}
24
25BigInt::BigInt(unsigned long int a)
26{
27  mpz_init(value);
28  mpz_set_ui(value,a);
29}
30
31BigInt::BigInt(int a)
32{
33  mpz_init(value);
34  mpz_set_si(value,(long int)a);
35}
36
37BigInt::BigInt(unsigned int a)
38{
39  mpz_init(value);
40  mpz_set_ui(value,(unsigned long)a);
41}
42
43BigInt::BigInt(short int a)
44{
45  mpz_init(value);
46  mpz_set_si(value,(long int)a);
47}
48
49BigInt::BigInt(unsigned short int a)
50{
51  mpz_init(value);
52  mpz_set_ui(value,(unsigned long int)a);
53}
54
55BigInt::BigInt(char a)
56{
57  mpz_init(value);
58  mpz_set_si(value,(long int)a);
59}
60
61BigInt::BigInt(unsigned char a)
62{
63  mpz_init(value);
64  mpz_set_ui(value,(unsigned long int)a);
65}
66
67// Copy-Konstruktor
68
69BigInt::BigInt(const BigInt& a)
70{
71  mpz_init(value);
72  mpz_set(value,a.value);
73}
74
75// Destruktor
76BigInt::~BigInt()
77{
78  mpz_clear(value);
79}
80
81//
82// Zuweisungsoperatoren
83//
84
85BigInt& BigInt::operator=(long int a)
86{
87  mpz_set_si(value,a);
88  return *this;
89}
90
91BigInt& BigInt::operator=(unsigned long int a)
92{
93  mpz_set_ui(value,a);
94  return *this;
95}
96
97BigInt& BigInt::operator=(int a)
98{
99  mpz_set_si(value,(long int) a);
100  return *this;
101}
102
103BigInt& BigInt::operator=(unsigned int a)
104{
105  mpz_set_ui(value,(unsigned long int) a);
106  return *this;
107}
108
109BigInt& BigInt::operator=(short int a)
110{
111  mpz_set_si(value,(long int) a);
112  return *this;
113}
114
115BigInt& BigInt::operator=(unsigned short int a)
116{
117  mpz_set_ui(value,(unsigned long int) a);
118  return *this;
119}
120
121BigInt& BigInt::operator=(char a)
122{
123  mpz_set_si(value,(long int) a);
124  return *this;
125}
126
127BigInt& BigInt::operator=(unsigned char a)
128{
129  mpz_set_ui(value,(unsigned long int) a);
130  return *this;
131}
132
133BigInt& BigInt::operator=(const BigInt& a)
134{
135  mpz_set(value,a.value);
136  return *this;
137}
138
139//
140// Type-Conversion
141//
142
143BigInt::operator bool()
144{
145  if (mpz_sgn(value)) return true;
146  return false;
147}
148
149BigInt::operator long int()
150{
151  long int ret_val;
152  ret_val=mpz_get_si(value);
153  return ret_val;
154}
155
156BigInt::operator unsigned long int()
157{
158  long int ret_val;
159  ret_val=mpz_get_ui(value);
160  return ret_val;
161}
162
163BigInt::operator int()
164{
165  int ret_val=operator long int();
166  return ret_val;
167}
168
169BigInt::operator unsigned int()
170{
171  unsigned int ret_val=operator unsigned long int();
172  return ret_val;
173}
174
175BigInt::operator short int()
176{
177  short int ret_val=operator long int();
178  return ret_val;
179}
180
181BigInt::operator unsigned short int()
182{
183  unsigned short int ret_val=operator unsigned long int();
184  return ret_val;
185}
186
187BigInt::operator char()
188{
189  char ret_val=operator long int();
190  return ret_val;
191}
192
193BigInt::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
204BigInt BigInt::operator-()
205{
206  BigInt erg;
207  mpz_neg(erg.value,value);
208  return erg;
209}
210
211// += Operator
212BigInt& 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
221BigInt& 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
230BigInt& 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
239BigInt& 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 ++
248BigInt& 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 ++
257BigInt BigInt::operator++(int)
258{
259  BigInt erg;
260  mpz_add(erg.value,value,BigInt(1).value);
261  return erg;
262}
263
264// prefix --
265BigInt& 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 --
274BigInt BigInt::operator--(int)
275{
276  BigInt erg;
277  mpz_add(erg.value,value,BigInt(1).value);
278  return erg;
279}
280
281BigInt 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
292bool operator<(const BigInt& a,const BigInt& b)
293{
294  if (mpz_cmp(a.value,b.value)<0) return true;
295  return false;
296}
297
298bool operator<=(const BigInt& a,const BigInt& b)
299{
300  if (mpz_cmp(a.value,b.value)>0) return false;
301  return true;
302}
303
304bool operator>(const BigInt& a,const BigInt& b)
305{
306  if (mpz_cmp(a.value,b.value)>0) return true;
307  return false;
308}
309
310bool operator>=(const BigInt& a,const BigInt& b)
311{
312  if (mpz_cmp(a.value,b.value)<0) return false;
313  return true;
314}
315
316bool operator==(const BigInt& a,const BigInt& b)
317{
318  if (!mpz_cmp(a.value,b.value)) return true;
319  return false;
320}
321
322bool operator!=(const BigInt& a,const BigInt& b)
323{
324  if (!mpz_cmp(a.value,b.value)) return false;
325  return true;
326}
327
328bool 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
334bool 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
340bool 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
346bool 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
352bool 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
358bool 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
364bool 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
370bool 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
376bool 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
382bool 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
388bool 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
394bool 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
404BigInt operator+(const BigInt& a,const BigInt &b)
405{
406  BigInt erg(a);
407  return erg+=b;
408}
409
410BigInt operator-(const BigInt& a,const BigInt &b)
411{
412  BigInt erg(a);
413  return erg-=b;
414}
415
416BigInt operator*(const BigInt& a,const BigInt &b)
417{
418  BigInt erg(a);
419  return erg*=b;
420}
421
422BigInt operator/(const BigInt& a,const BigInt &b)
423{
424  BigInt erg(a);
425  return erg/=b;
426}
427
428BigInt operator+(const long int& a,const BigInt &b)
429{
430  BigInt erg(a);
431  return erg+=b;
432}
433
434BigInt operator-(const long int& a,const BigInt &b)
435{
436  BigInt erg(a);
437  return erg-=b;
438}
439
440BigInt operator*(const long int& a,const BigInt &b)
441{
442  BigInt erg(a);
443  return erg*=b;
444}
445
446BigInt operator/(const long int& a,const BigInt &b)
447{
448  BigInt erg(a);
449  return erg/=b;
450}
451
452BigInt operator+(const BigInt& a,const long int &b)
453{
454  BigInt erg(a);
455  return erg+=BigInt(b);
456}
457
458BigInt operator-(const BigInt& a,const long int &b)
459{
460  BigInt erg(a);
461  return erg-=BigInt(b);
462}
463
464BigInt operator*(const BigInt& a,const long int &b)
465{
466  BigInt erg(a);
467  return erg*=BigInt(b);
468}
469
470BigInt operator/(const BigInt& a,const long int &b)
471{
472  BigInt erg(a);
473  return erg/=BigInt(b);
474}
475
476// liefert das Vorzeichen
477int sgn(const BigInt& a)
478{
479  return mpz_sgn(a.value);
480}
481
482// liefert den Absolutbetrag
483BigInt 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
Note: See TracBrowser for help on using the repository browser.