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(int a) |
---|
20 | { |
---|
21 | mpz_init(value); |
---|
22 | mpz_set_si(value,(long)a); |
---|
23 | } |
---|
24 | |
---|
25 | // Copy-Konstruktor |
---|
26 | |
---|
27 | BigInt::BigInt(const BigInt& a) |
---|
28 | { |
---|
29 | mpz_init(value); |
---|
30 | mpz_set(value,a.value); |
---|
31 | } |
---|
32 | |
---|
33 | // Destruktor |
---|
34 | BigInt::~BigInt() |
---|
35 | { |
---|
36 | mpz_clear(value); |
---|
37 | } |
---|
38 | |
---|
39 | // |
---|
40 | // Zuweisungsoperatoren |
---|
41 | // |
---|
42 | |
---|
43 | BigInt& BigInt::operator=(int a) |
---|
44 | { |
---|
45 | mpz_set_si(value,(long)a); |
---|
46 | return *this; |
---|
47 | } |
---|
48 | |
---|
49 | BigInt& BigInt::operator=(const BigInt& a) |
---|
50 | { |
---|
51 | mpz_set(value,a.value); |
---|
52 | return *this; |
---|
53 | } |
---|
54 | |
---|
55 | // |
---|
56 | // Type-Conversion |
---|
57 | // |
---|
58 | |
---|
59 | BigInt::operator bool() |
---|
60 | { |
---|
61 | if (mpz_sgn(value)) return true; |
---|
62 | return false; |
---|
63 | } |
---|
64 | |
---|
65 | BigInt::operator int() |
---|
66 | { |
---|
67 | long int ret_val; |
---|
68 | ret_val=mpz_get_si(value); |
---|
69 | return (int)ret_val; |
---|
70 | } |
---|
71 | |
---|
72 | BigInt::operator short() |
---|
73 | { |
---|
74 | long int ret_val; |
---|
75 | ret_val=mpz_get_si(value); |
---|
76 | return (short)ret_val; |
---|
77 | } |
---|
78 | |
---|
79 | // |
---|
80 | // unary arithmetic operators |
---|
81 | // |
---|
82 | |
---|
83 | // unary Minus |
---|
84 | BigInt BigInt::operator-() |
---|
85 | { |
---|
86 | BigInt erg; |
---|
87 | mpz_neg(erg.value,value); |
---|
88 | return erg; |
---|
89 | } |
---|
90 | |
---|
91 | // += Operator |
---|
92 | BigInt& BigInt::operator+=(const BigInt &a) |
---|
93 | { |
---|
94 | BigInt aux; |
---|
95 | mpz_set(aux.value,value); |
---|
96 | mpz_add(value,aux.value,a.value); |
---|
97 | return *this; |
---|
98 | } |
---|
99 | |
---|
100 | // -= Operator |
---|
101 | BigInt& BigInt::operator-=(const BigInt &a) |
---|
102 | { |
---|
103 | BigInt aux; |
---|
104 | mpz_set(aux.value,value); |
---|
105 | mpz_sub(value,aux.value,a.value); |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | // *= Operator |
---|
110 | BigInt& BigInt::operator*=(const BigInt &a) |
---|
111 | { |
---|
112 | BigInt aux; |
---|
113 | mpz_set(aux.value,value); |
---|
114 | mpz_mul(value,aux.value,a.value); |
---|
115 | return *this; |
---|
116 | } |
---|
117 | |
---|
118 | // /= Operator |
---|
119 | BigInt& BigInt::operator/=(const BigInt &a) |
---|
120 | { |
---|
121 | BigInt aux; |
---|
122 | mpz_set(aux.value,value); |
---|
123 | mpz_fdiv_q(value,aux.value,a.value); |
---|
124 | return *this; |
---|
125 | } |
---|
126 | |
---|
127 | // prefix ++ |
---|
128 | BigInt& BigInt::operator++() |
---|
129 | { |
---|
130 | BigInt aux; |
---|
131 | mpz_set(aux.value,value); |
---|
132 | mpz_add(value,aux.value,BigInt(1).value); |
---|
133 | return *this; |
---|
134 | } |
---|
135 | |
---|
136 | // postfix ++ |
---|
137 | BigInt BigInt::operator++(int) |
---|
138 | { |
---|
139 | BigInt erg; |
---|
140 | mpz_add(erg.value,value,BigInt(1).value); |
---|
141 | return erg; |
---|
142 | } |
---|
143 | |
---|
144 | // prefix -- |
---|
145 | BigInt& BigInt::operator--() |
---|
146 | { |
---|
147 | BigInt aux; |
---|
148 | mpz_set(aux.value,value); |
---|
149 | mpz_sub(value,aux.value,BigInt(1).value); |
---|
150 | return *this; |
---|
151 | } |
---|
152 | |
---|
153 | // postfix -- |
---|
154 | BigInt BigInt::operator--(int) |
---|
155 | { |
---|
156 | BigInt erg; |
---|
157 | mpz_add(erg.value,value,BigInt(1).value); |
---|
158 | return erg; |
---|
159 | } |
---|
160 | |
---|
161 | BigInt operator-(const BigInt& r) |
---|
162 | { |
---|
163 | BigInt erg; |
---|
164 | mpz_neg(erg.value,r.value); |
---|
165 | return erg; |
---|
166 | } |
---|
167 | |
---|
168 | // |
---|
169 | // Vergleichsoperatorn |
---|
170 | // |
---|
171 | |
---|
172 | bool operator<(const BigInt& a,const BigInt& b) |
---|
173 | { |
---|
174 | if (mpz_cmp(a.value,b.value)<0) return true; |
---|
175 | return false; |
---|
176 | } |
---|
177 | |
---|
178 | bool operator<=(const BigInt& a,const BigInt& b) |
---|
179 | { |
---|
180 | if (mpz_cmp(a.value,b.value)>0) return false; |
---|
181 | return true; |
---|
182 | } |
---|
183 | |
---|
184 | bool operator>(const BigInt& a,const BigInt& b) |
---|
185 | { |
---|
186 | if (mpz_cmp(a.value,b.value)>0) return true; |
---|
187 | return false; |
---|
188 | } |
---|
189 | |
---|
190 | bool operator>=(const BigInt& a,const BigInt& b) |
---|
191 | { |
---|
192 | if (mpz_cmp(a.value,b.value)<0) return false; |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | bool operator==(const BigInt& a,const BigInt& b) |
---|
197 | { |
---|
198 | if (!mpz_cmp(a.value,b.value)) return true; |
---|
199 | return false; |
---|
200 | } |
---|
201 | |
---|
202 | bool operator!=(const BigInt& a,const BigInt& b) |
---|
203 | { |
---|
204 | if (!mpz_cmp(a.value,b.value)) return false; |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | bool operator<(const int& a,const BigInt& b) |
---|
209 | { |
---|
210 | if (mpz_cmp(BigInt(a).value,b.value)<0) return true; |
---|
211 | return false; |
---|
212 | } |
---|
213 | |
---|
214 | bool operator<=(const int& a,const BigInt& b) |
---|
215 | { |
---|
216 | if (mpz_cmp(BigInt(a).value,b.value)>0) return false; |
---|
217 | return true; |
---|
218 | } |
---|
219 | |
---|
220 | bool operator>(const int& a,const BigInt& b) |
---|
221 | { |
---|
222 | if (mpz_cmp(BigInt(a).value,b.value)>0) return true; |
---|
223 | return false; |
---|
224 | } |
---|
225 | |
---|
226 | bool operator>=(const int& a,const BigInt& b) |
---|
227 | { |
---|
228 | if (mpz_cmp(BigInt(a).value,b.value)<0) return false; |
---|
229 | return true; |
---|
230 | } |
---|
231 | |
---|
232 | bool operator==(const int& a,const BigInt& b) |
---|
233 | { |
---|
234 | if (!mpz_cmp(BigInt(a).value,b.value)) return true; |
---|
235 | return false; |
---|
236 | } |
---|
237 | |
---|
238 | bool operator!=(const int& a,const BigInt& b) |
---|
239 | { |
---|
240 | if (!mpz_cmp(BigInt(a).value,b.value)) return false; |
---|
241 | return true; |
---|
242 | } |
---|
243 | |
---|
244 | bool operator<(const BigInt& a,const int& b) |
---|
245 | { |
---|
246 | if (mpz_cmp(a.value,BigInt(b).value)<0) return true; |
---|
247 | return false; |
---|
248 | } |
---|
249 | |
---|
250 | bool operator<=(const BigInt& a,const int& b) |
---|
251 | { |
---|
252 | if (mpz_cmp(a.value,BigInt(b).value)>0) return false; |
---|
253 | return true; |
---|
254 | } |
---|
255 | |
---|
256 | bool operator>(const BigInt& a,const int& b) |
---|
257 | { |
---|
258 | if (mpz_cmp(a.value,BigInt(b).value)>0) return true; |
---|
259 | return false; |
---|
260 | } |
---|
261 | |
---|
262 | bool operator>=(const BigInt& a,const int& b) |
---|
263 | { |
---|
264 | if (mpz_cmp(a.value,BigInt(b).value)<0) return false; |
---|
265 | return true; |
---|
266 | } |
---|
267 | |
---|
268 | bool operator==(const BigInt& a,const int& b) |
---|
269 | { |
---|
270 | if (!mpz_cmp(a.value,BigInt(b).value)) return true; |
---|
271 | return false; |
---|
272 | } |
---|
273 | |
---|
274 | bool operator!=(const BigInt& a,const int& b) |
---|
275 | { |
---|
276 | if (!mpz_cmp(a.value,BigInt(b).value)) return false; |
---|
277 | return true; |
---|
278 | } |
---|
279 | |
---|
280 | // |
---|
281 | // die Grundoperationen |
---|
282 | // |
---|
283 | |
---|
284 | BigInt operator+(const BigInt& a,const BigInt &b) |
---|
285 | { |
---|
286 | BigInt erg(a); |
---|
287 | return erg+=b; |
---|
288 | } |
---|
289 | |
---|
290 | BigInt operator-(const BigInt& a,const BigInt &b) |
---|
291 | { |
---|
292 | BigInt erg(a); |
---|
293 | return erg-=b; |
---|
294 | } |
---|
295 | |
---|
296 | BigInt operator*(const BigInt& a,const BigInt &b) |
---|
297 | { |
---|
298 | BigInt erg(a); |
---|
299 | return erg*=b; |
---|
300 | } |
---|
301 | |
---|
302 | BigInt operator/(const BigInt& a,const BigInt &b) |
---|
303 | { |
---|
304 | BigInt erg(a); |
---|
305 | return erg/=b; |
---|
306 | } |
---|
307 | |
---|
308 | BigInt operator+(const int& a,const BigInt &b) |
---|
309 | { |
---|
310 | BigInt erg(a); |
---|
311 | return erg+=b; |
---|
312 | } |
---|
313 | |
---|
314 | BigInt operator-(const int& a,const BigInt &b) |
---|
315 | { |
---|
316 | BigInt erg(a); |
---|
317 | return erg-=b; |
---|
318 | } |
---|
319 | |
---|
320 | BigInt operator*(const int& a,const BigInt &b) |
---|
321 | { |
---|
322 | BigInt erg(a); |
---|
323 | return erg*=b; |
---|
324 | } |
---|
325 | |
---|
326 | BigInt operator/(const int& a,const BigInt &b) |
---|
327 | { |
---|
328 | BigInt erg(a); |
---|
329 | return erg/=b; |
---|
330 | } |
---|
331 | |
---|
332 | BigInt operator+(const BigInt& a,const int &b) |
---|
333 | { |
---|
334 | BigInt erg(a); |
---|
335 | return erg+=BigInt(b); |
---|
336 | } |
---|
337 | |
---|
338 | BigInt operator-(const BigInt& a,const int &b) |
---|
339 | { |
---|
340 | BigInt erg(a); |
---|
341 | return erg-=BigInt(b); |
---|
342 | } |
---|
343 | |
---|
344 | BigInt operator*(const BigInt& a,const int &b) |
---|
345 | { |
---|
346 | BigInt erg(a); |
---|
347 | return erg*=BigInt(b); |
---|
348 | } |
---|
349 | |
---|
350 | BigInt operator/(const BigInt& a,const int &b) |
---|
351 | { |
---|
352 | BigInt erg(a); |
---|
353 | return erg/=BigInt(b); |
---|
354 | } |
---|
355 | |
---|
356 | // liefert das Vorzeichen |
---|
357 | int sgn(const BigInt& a) |
---|
358 | { |
---|
359 | return mpz_sgn(a.value); |
---|
360 | } |
---|
361 | |
---|
362 | // liefert den Absolutbetrag |
---|
363 | BigInt abs(const BigInt& a) |
---|
364 | { |
---|
365 | BigInt erg; |
---|
366 | if (mpz_sgn(a.value)<0) |
---|
367 | mpz_neg(erg.value,a.value); |
---|
368 | else |
---|
369 | mpz_set(erg.value,a.value); |
---|
370 | return erg; |
---|
371 | } |
---|
372 | |
---|
373 | #endif // BIG_INT_CC |
---|