ACM大数模板(支持正负整数)

之前就保留过简陋的几个用外部数组变量实现的简单大数模板,也没有怎么用过,今天就想着整合封装一下,封装成C++的类,以后需要调用的时候也方便得多。

 

实现了基本的加减乘除和取模运算的操作符重载,大数除以大数难度太大就没实现,另外还实现了比较运算符,方便实际使用贴近内置类型的体验。

话不多说,贴代码。

 

/*BigInt.h*/

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <ctype.h>
  4 
  5 #define MAXBIT 1007
  6 #define BITTYPE int
  7 
  8 class BigInt {
  9 private:
 10     BITTYPE bit[MAXBIT];
 11     bool negative;//负数标志
 12 
 13 public:
 14     BigInt();                //默认构造函数,值为0
 15     BigInt(const int);        //构造函数
 16     BigInt(const char *);    //构造函数
 17     BigInt(const BigInt &);    //复制构造函数
 18     
 19     /*重载赋值运算符*/
 20     BigInt& operator=(const BigInt&);
 21     BigInt& operator=(const int        );
 22 
 23     /*重载算数运算符*/
 24     BigInt operator+(const BigInt&    )const;
 25     BigInt operator+(const int        )const;
 26     BigInt operator-(const BigInt&    )const;
 27     BigInt operator-(const int        )const;
 28     BigInt operator*(const BigInt&    )const;
 29     BigInt operator*(const int        )const;
 30     BigInt operator/(const int        )const;
 31     int    operator%(const int        )const;
 32     
 33     /*重载比较运算符*/
 34     bool operator>(const BigInt&    )const;
 35     bool operator>(const int        )const;
 36     bool operator>=(const BigInt&    )const;
 37     bool operator>=(const int        )const;
 38     bool operator<(const BigInt&    )const;
 39     bool operator<(const int        )const;
 40     bool operator<=(const BigInt&    )const;
 41     bool operator<=(const int        )const;
 42     bool operator==(const BigInt&    )const;
 43     bool operator==(const int        )const;
 44     bool operator!=(const BigInt&    )const;
 45     bool operator!=(const int        )const;
 46     
 47     void print()        const;//输出数值
 48     bool isZero()        const;//是否为0
 49     bool isPositive()    const;//是否为正数
 50     bool isNegative()    const;//是否为负数
 51     bool nonNegative()    const;//是否为非负数
 52 
 53 private:
 54     BigInt opposite()const;//取相反数
 55     BigInt absoluteAdd(const BigInt&)const;//加上绝对值
 56     BigInt absoluteMinus(const BigInt&)const;//减去绝对值小于自身的数的绝对值
 57     bool   absoluteEqual(const BigInt&)const;//绝对值等于
 58     bool   absoluteGreater(const BigInt&)const;//绝对值大于
 59     bool   absoluteEqualGreater(const BigInt&)const;//绝对值大于等于
 60 };
 61 
 62 BigInt::BigInt()
 63 {
 64     memset(bit,0,sizeof(bit));
 65     negative = false;
 66 }
 67 
 68 BigInt::BigInt(const int n)
 69 {
 70     memset(bit,0,sizeof(bit));
 71     int nn = n;
 72     if (nn>=0) negative = false;
 73     else {
 74         negative = true;
 75         nn = -nn;
 76     }
 77     int pos = 0;
 78     while (nn) {
 79         bit[pos++] = nn % 10;
 80         nn /= 10;
 81     }
 82 }
 83 
 84 BigInt::BigInt(const char *s)
 85 {
 86     int len = strlen(s);
 87     bool valid = true;//符合数字格式
 88     if (len >= 2) {
 89         if (s[0]!='+' && s[0]!='-' && !isdigit(s[0])) valid = false;
 90         for (int i=1; i<len; ++i) {
 91             if (!isdigit(s[i])) valid = false;
 92         }
 93     }
 94     else if (len == 1) {
 95         if (!isdigit(s[0])) valid = false;
 96     }
 97     if (len==0 || !valid) {
 98         memset(bit,0,sizeof(bit));
 99         negative = false;
100         return;
101     }
102     int beg = 0, end = len-1;
103     if (s[0] == '+') {
104         negative = false;
105         ++beg;
106     }
107     else if (s[0] == '-') {
108         bool zeroFlag = true;
109         for (int i=1; i<len; ++i) {
110             if (s[i]!='0') {
111                 zeroFlag = false;
112                 break;
113             }
114         }
115         if (zeroFlag) negative = false;
116         else negative = true;
117         ++beg;
118     }
119     else negative = false;
120     memset(bit,0,sizeof(bit));
121     for (int i=beg; i<=end; ++i) {
122         bit[len-1-i] = s[i] - '0';
123     }
124 }
125 
126 BigInt::BigInt(const BigInt& n)
127 {
128     memcpy(bit,n.bit,sizeof(bit));
129     negative = n.negative;
130 }
131 
132 BigInt& BigInt::operator=(const BigInt& n)
133 {
134     memcpy(bit,n.bit,sizeof(bit));
135     negative = n.negative;
136     return *this;
137 }
138 
139 BigInt& BigInt::operator=(const int n)
140 {
141     return *this = BigInt(n);
142 }
143 
144 BigInt BigInt::operator+(const BigInt& n)const
145 {
146     if ((!negative && !n.negative) || (negative && n.negative)) {
147         return this->absoluteAdd(n);
148     }
149     else {
150         if (absoluteEqual(n)) return BigInt();
151         else if (absoluteEqualGreater(n)) return this->absoluteMinus(n);
152         else return n.absoluteMinus(*this);
153     }
154 }
155 
156 BigInt BigInt::operator+(const int n)const
157 {
158     return *this + BigInt(n);
159 }
160 
161 BigInt BigInt::operator-(const BigInt& n)const
162 {
163     return *this + n.opposite();
164 }
165 
166 BigInt BigInt::operator-(const int n)const
167 {
168     return *this - BigInt(n);
169 }
170 
171 BigInt BigInt::operator*(const BigInt& n)const
172 {
173     if (isZero() || n.isZero()) return BigInt();
174     BigInt bi = BigInt();
175     if ((!negative && !n.negative) || (negative && n.negative)) {
176         bi.negative = false;
177     }
178     else bi.negative = true;
179     for (int i=0; i<MAXBIT; ++i) for (int j=0; j<MAXBIT-i; ++j) {
180         bi.bit[i+j] += bit[i] * n.bit[j];
181     }
182     for (int i=0; i<MAXBIT-1; ++i) {//进位
183         bi.bit[i+1] += bi.bit[i] / 10;
184         bi.bit[i] %= 10;
185     }
186     return bi;
187 }
188 
189 BigInt BigInt::operator*(const int n)const
190 {
191     return *this * BigInt(n);
192 }
193 
194 BigInt BigInt::operator/(const int n)const
195 {//除以0直接返回0
196     if (isZero() || n==0) return BigInt();
197     BigInt bi = BigInt();
198     if ((!negative && n>0) || (negative && n<0)) {
199         bi.negative = false;
200     }
201     else bi.negative = true;
202     int div = 0;//累计除数
203     for (int i=MAXBIT-1; i>=0; --i) {
204         div = div * 10 + bit[i];
205         bi.bit[i] = div / n;
206         div %= n;
207     }
208     return bi;
209 }
210 
211 int BigInt::operator%(const int n)const
212 {
213     int mod = 0;//累计余数
214     for (int i=MAXBIT-1; i>=0; --i) {
215         //mod = ((mod*(MAXBIT+1/*??*/)) + bit[i]) % n;
216         mod = ((mod*10) + bit[i]) % n;
217     }
218     return mod;
219 }
220 
221 bool BigInt::operator>(const BigInt& n)const
222 {
223     if (!negative && n.negative) return true;
224     else if (negative && !n.negative) return false;
225     else if (!negative && !n.negative) return absoluteGreater(n);
226     else return n.absoluteGreater(*this);
227 }
228 
229 bool BigInt::operator>(const int n)const
230 {
231     return *this > BigInt(n);
232 }
233 
234 bool BigInt::operator>=(const BigInt& n)const
235 {
236     if (!negative && n.negative) return true;
237     else if (negative && !n.negative) return false;
238     else if (!negative && !n.negative) return absoluteEqualGreater(n);
239     else return n.absoluteEqualGreater(*this);
240 }
241 
242 bool BigInt::operator>=(const int n)const
243 {
244     return *this >= BigInt(n);
245 }
246 
247 bool BigInt::operator<(const BigInt& n)const
248 {
249     return n > *this;
250 }
251 
252 bool BigInt::operator<(const int n)const
253 {
254     return *this < BigInt(n);
255 }
256 
257 bool BigInt::operator<=(const BigInt& n)const
258 {
259     return n >= *this;
260 }
261 
262 bool BigInt::operator<=(const int n)const
263 {
264     return *this <= BigInt(n);
265 }
266 
267 bool BigInt::operator==(const BigInt& n)const
268 {
269     if (negative != n.negative) return false;
270     for (int i=0; i<MAXBIT; ++i) {
271         if (bit[i] != n.bit[i]) return false;
272     }
273     return true;
274 }
275 
276 bool BigInt::operator==(const int n)const
277 {
278     return *this == BigInt(n);
279 }
280 
281 bool BigInt::operator!=(const BigInt& n)const
282 {
283     if (negative != n.negative) return true;
284     for (int i=0; i<MAXBIT; ++i) {
285         if (bit[i] != n.bit[i]) return true;
286     }
287     return false;
288 }
289 
290 bool BigInt::operator!=(const int n)const
291 {
292     return *this != BigInt(n);
293 }
294 
295 void BigInt::print()const
296 {
297     if (negative) printf("-");
298     int pos = MAXBIT - 1;
299     for (; pos>0; --pos) {
300         if (bit[pos]) break;
301     }
302     for (int i=pos; i>=0; --i) printf("%d",bit[i]);
303 }
304 
305 bool BigInt::isZero()const
306 {
307     bool zeroFlag = true;
308     for (int i=0; i<MAXBIT; ++i) {
309         if (bit[i] != 0) {
310             zeroFlag = false;
311             break;
312         }
313     }
314     return zeroFlag;
315 }
316 
317 bool BigInt::isPositive()const
318 {
319     return !negative && !isZero();
320 }
321 
322 bool BigInt::isNegative()const
323 {
324     return negative;
325 }
326 
327 bool BigInt::nonNegative()const
328 {
329     return !negative;
330 }
331 
332 BigInt BigInt::opposite()const
333 {
334     BigInt n(*this);
335     if (!n.isZero()) n.negative = !n.negative;
336     return n;
337 }
338 
339 BigInt BigInt::absoluteAdd(const BigInt& n)const
340 {
341     BigInt bi(*this);
342     int next = 0;//进位
343     for (int i=0; i<MAXBIT; ++i) {
344         bi.bit[i] = (bit[i] + n.bit[i] + next) % 10;
345         next   = (bit[i] + n.bit[i] + next) / 10;
346     }
347     return bi;
348 }
349 
350 BigInt BigInt::absoluteMinus(const BigInt& n)const
351 {
352     BigInt bi(*this);
353     for (int i=MAXBIT-1; i>=0; --i) {
354         if (bi.bit[i]>=n.bit[i]) bi.bit[i] -= n.bit[i];
355         else {//借位
356             int borrow = i + 1;//借位位
357             while (bi.bit[borrow]==0) ++borrow;
358             --bi.bit[borrow];
359             for (int j=i+1; j<borrow; ++j) bi.bit[j] = 9;
360             bi.bit[i] = bi.bit[i] + 10 - n.bit[i];
361         }
362     }
363     return bi;
364 }
365 
366 bool BigInt::absoluteEqual(const BigInt& n)const
367 {
368     for (int i=0; i<MAXBIT; ++i) {
369         if (bit[i] != n.bit[i]) return false;
370     }
371     return true;
372 }
373 
374 bool BigInt::absoluteGreater(const BigInt& n)const
375 {
376     for (int i=MAXBIT-1; i>=0; --i) {
377         if (bit[i]>n.bit[i]) return true;
378         else if (bit[i]<n.bit[i]) return false;
379     }
380     return false;
381 }
382 
383 bool BigInt::absoluteEqualGreater(const BigInt& n)const
384 {
385     for (int i=MAXBIT-1; i>=0; --i) {
386         if (bit[i]>n.bit[i]) return true;
387         else if (bit[i]<n.bit[i]) return false;
388     }
389     return true;
390 }

 

代码调用也挺方便,如下例子:

 1 #include "BigInt.h"
 2 
 3 int main()
 4 {
 5     BigInt m = 1354145646;
 6     BigInt n("-32143542");
 7     if (m >= n) puts("m >= n");
 8     n = m + n;
 9     n = n * 123;
10     n.print();
11     return 0;
12 }

 

 

 

模板写成后特地去poj刷了大数相加和大数相乘两道水题,一次ac的满足感可是杠杠的~

 

欢迎大家参考,测试,批评指正bug和不足之处,感谢~

 

 

 

 

---------------------------------------------------------------------------------------------

2014/10/15更新

添加了重载负号,+=,-=,*=,/=,%=,还有string()函数返回值的字符串功能

/*BigInt.h*/

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <ctype.h>
  4 
  5 #define MAXBIT 1007
  6 #define BITTYPE int
  7 
  8 class BigInt {
  9 private:
 10     BITTYPE bit[MAXBIT];
 11     bool negative;//负数标志
 12 
 13 public:
 14     BigInt();                //默认构造函数,值为0
 15     BigInt(const int);        //构造函数
 16     BigInt(const char *);    //构造函数
 17     BigInt(const BigInt &);    //复制构造函数
 18     
 19     /*重载赋值运算符*/
 20     BigInt& operator=(const BigInt&);
 21     BigInt& operator=(const int        );
 22 
 23     /*重载算数运算符*/
 24     BigInt operator+(const BigInt&    )const;
 25     BigInt operator+(const int        )const;
 26     BigInt operator+=(const BigInt&    );
 27     BigInt operator+=(const int        );
 28     BigInt operator-(const BigInt&    )const;
 29     BigInt operator-(const int        )const;
 30     BigInt operator-=(const BigInt&    );
 31     BigInt operator-=(const int        );
 32     BigInt operator-(                )const;
 33     BigInt operator*(const BigInt&    )const;
 34     BigInt operator*(const int        )const;
 35     BigInt operator*=(const BigInt&    );
 36     BigInt operator*=(const int        );
 37     BigInt operator/(const int        )const;
 38     BigInt operator/=(const int        );
 39     int    operator%(const int        )const;
 40     BigInt operator%=(const int        );
 41     
 42     /*重载比较运算符*/
 43     bool operator>(const BigInt&    )const;
 44     bool operator>(const int        )const;
 45     bool operator>=(const BigInt&    )const;
 46     bool operator>=(const int        )const;
 47     bool operator<(const BigInt&    )const;
 48     bool operator<(const int        )const;
 49     bool operator<=(const BigInt&    )const;
 50     bool operator<=(const int        )const;
 51     bool operator==(const BigInt&    )const;
 52     bool operator==(const int        )const;
 53     bool operator!=(const BigInt&    )const;
 54     bool operator!=(const int        )const;
 55     
 56     void print()        const;//输出数值
 57     char *string()        const;//返回数值字符串
 58     bool isZero()        const;//是否为0
 59     bool isPositive()    const;//是否为正数
 60     bool isNegative()    const;//是否为负数
 61     bool nonNegative()    const;//是否为非负数
 62 
 63 private:
 64     BigInt opposite()const;//取相反数
 65     BigInt absoluteAdd(const BigInt&)const;//加上绝对值
 66     BigInt absoluteMinus(const BigInt&)const;//减去绝对值小于自身的数的绝对值
 67     bool   absoluteEqual(const BigInt&)const;//绝对值等于
 68     bool   absoluteGreater(const BigInt&)const;//绝对值大于
 69     bool   absoluteEqualGreater(const BigInt&)const;//绝对值大于等于
 70 };
 71 
 72 BigInt::BigInt()
 73 {
 74     memset(bit,0,sizeof(bit));
 75     negative = false;
 76 }
 77 
 78 BigInt::BigInt(const int n)
 79 {
 80     memset(bit,0,sizeof(bit));
 81     int nn = n;
 82     if (nn>=0) negative = false;
 83     else {
 84         negative = true;
 85         nn = -nn;
 86     }
 87     int pos = 0;
 88     while (nn) {
 89         bit[pos++] = nn % 10;
 90         nn /= 10;
 91     }
 92 }
 93 
 94 BigInt::BigInt(const char *s)
 95 {
 96     int len = strlen(s);
 97     bool valid = true;//符合数字格式
 98     if (len >= 2) {
 99         if (s[0]!='+' && s[0]!='-' && !isdigit(s[0])) valid = false;
100         for (int i=1; i<len; ++i) {
101             if (!isdigit(s[i])) valid = false;
102         }
103     }
104     else if (len == 1) {
105         if (!isdigit(s[0])) valid = false;
106     }
107     if (len==0 || !valid) {
108         memset(bit,0,sizeof(bit));
109         negative = false;
110         return;
111     }
112     int beg = 0, end = len-1;
113     if (s[0] == '+') {
114         negative = false;
115         ++beg;
116     }
117     else if (s[0] == '-') {
118         bool zeroFlag = true;
119         for (int i=1; i<len; ++i) {
120             if (s[i]!='0') {
121                 zeroFlag = false;
122                 break;
123             }
124         }
125         if (zeroFlag) negative = false;
126         else negative = true;
127         ++beg;
128     }
129     else negative = false;
130     memset(bit,0,sizeof(bit));
131     for (int i=beg; i<=end; ++i) {
132         bit[len-1-i] = s[i] - '0';
133     }
134 }
135 
136 BigInt::BigInt(const BigInt& n)
137 {
138     memcpy(bit,n.bit,sizeof(bit));
139     negative = n.negative;
140 }
141 
142 BigInt& BigInt::operator=(const BigInt& n)
143 {
144     memcpy(bit,n.bit,sizeof(bit));
145     negative = n.negative;
146     return *this;
147 }
148 
149 BigInt& BigInt::operator=(const int n)
150 {
151     return *this = BigInt(n);
152 }
153 
154 BigInt BigInt::operator+(const BigInt& n)const
155 {
156     if ((!negative && !n.negative) || (negative && n.negative)) {
157         return this->absoluteAdd(n);
158     }
159     else {
160         if (absoluteEqual(n)) return BigInt();
161         else if (absoluteEqualGreater(n)) return this->absoluteMinus(n);
162         else return n.absoluteMinus(*this);
163     }
164 }
165 
166 BigInt BigInt::operator+(const int n)const
167 {
168     return *this + BigInt(n);
169 }
170 
171 BigInt BigInt::operator+=(const BigInt& n)
172 {
173     return *this = *this + n;
174 }
175 
176 BigInt BigInt::operator+=(const int n)
177 {
178     return *this = *this + n;
179 }
180 
181 BigInt BigInt::operator-(const BigInt& n)const
182 {
183     return *this + n.opposite();
184 }
185 
186 BigInt BigInt::operator-(const int n)const
187 {
188     return *this - BigInt(n);
189 }
190 
191 BigInt BigInt::operator-=(const BigInt& n)
192 {
193     return *this = *this - n;
194 }
195 
196 BigInt BigInt::operator-=(const int n)
197 {
198     return *this = *this - n;
199 }
200 
201 BigInt BigInt::operator-()const
202 {
203     BigInt bi(*this);
204     if (!this->isZero()) bi.negative = !bi.negative;
205     return bi;
206 }
207 
208 BigInt BigInt::operator*(const BigInt& n)const
209 {
210     if (isZero() || n.isZero()) return BigInt();
211     BigInt bi = BigInt();
212     if ((!negative && !n.negative) || (negative && n.negative)) {
213         bi.negative = false;
214     }
215     else bi.negative = true;
216     for (int i=0; i<MAXBIT; ++i) for (int j=0; j<MAXBIT-i; ++j) {
217         bi.bit[i+j] += bit[i] * n.bit[j];
218     }
219     for (int i=0; i<MAXBIT-1; ++i) {//进位
220         bi.bit[i+1] += bi.bit[i] / 10;
221         bi.bit[i] %= 10;
222     }
223     return bi;
224 }
225 
226 BigInt BigInt::operator*(const int n)const
227 {
228     return *this * BigInt(n);
229 }
230 
231 BigInt BigInt::operator*=(const BigInt& n)
232 {
233     return *this = *this - n;
234 }
235 
236 BigInt BigInt::operator*=(const int n)
237 {
238     return *this = *this * n;
239 }
240 
241 BigInt BigInt::operator/(const int n)const
242 {//除以0直接返回0
243     if (isZero() || n==0) return BigInt();
244     BigInt bi = BigInt();
245     if ((!negative && n>0) || (negative && n<0)) {
246         bi.negative = false;
247     }
248     else bi.negative = true;
249     int div = 0;//累计除数
250     for (int i=MAXBIT-1; i>=0; --i) {
251         div = div * 10 + bit[i];
252         bi.bit[i] = div / n;
253         div %= n;
254     }
255     return bi;
256 }
257 
258 BigInt BigInt::operator/=(const int n)
259 {//除以0直接返回0
260     if (isZero() || n==0) return BigInt();
261     else return *this = *this / n;
262 }
263 
264 int BigInt::operator%(const int n)const
265 {
266     int mod = 0;//累计余数
267     for (int i=MAXBIT-1; i>=0; --i) {
268         //mod = ((mod*(MAXBIT+1/*??*/)) + bit[i]) % n;
269         mod = ((mod*10) + bit[i]) % n;
270     }
271     return mod;
272 }
273 
274 BigInt BigInt::operator%=(const int n)
275 {
276     int mod = 0;//累计余数
277     for (int i=MAXBIT-1; i>=0; --i) {
278         //mod = ((mod*(MAXBIT+1/*??*/)) + bit[i]) % n;
279         mod = ((mod*10) + bit[i]) % n;
280     }
281     return *this = BigInt(mod);
282 }
283 
284 bool BigInt::operator>(const BigInt& n)const
285 {
286     if (!negative && n.negative) return true;
287     else if (negative && !n.negative) return false;
288     else if (!negative && !n.negative) return absoluteGreater(n);
289     else return n.absoluteGreater(*this);
290 }
291 
292 bool BigInt::operator>(const int n)const
293 {
294     return *this > BigInt(n);
295 }
296 
297 bool BigInt::operator>=(const BigInt& n)const
298 {
299     if (!negative && n.negative) return true;
300     else if (negative && !n.negative) return false;
301     else if (!negative && !n.negative) return absoluteEqualGreater(n);
302     else return n.absoluteEqualGreater(*this);
303 }
304 
305 bool BigInt::operator>=(const int n)const
306 {
307     return *this >= BigInt(n);
308 }
309 
310 bool BigInt::operator<(const BigInt& n)const
311 {
312     return n > *this;
313 }
314 
315 bool BigInt::operator<(const int n)const
316 {
317     return *this < BigInt(n);
318 }
319 
320 bool BigInt::operator<=(const BigInt& n)const
321 {
322     return n >= *this;
323 }
324 
325 bool BigInt::operator<=(const int n)const
326 {
327     return *this <= BigInt(n);
328 }
329 
330 bool BigInt::operator==(const BigInt& n)const
331 {
332     if (negative != n.negative) return false;
333     for (int i=0; i<MAXBIT; ++i) {
334         if (bit[i] != n.bit[i]) return false;
335     }
336     return true;
337 }
338 
339 bool BigInt::operator==(const int n)const
340 {
341     return *this == BigInt(n);
342 }
343 
344 bool BigInt::operator!=(const BigInt& n)const
345 {
346     if (negative != n.negative) return true;
347     for (int i=0; i<MAXBIT; ++i) {
348         if (bit[i] != n.bit[i]) return true;
349     }
350     return false;
351 }
352 
353 bool BigInt::operator!=(const int n)const
354 {
355     return *this != BigInt(n);
356 }
357 
358 void BigInt::print()const
359 {
360     if (negative) printf("-");
361     int pos = MAXBIT - 1;
362     for (; pos>0; --pos) {
363         if (bit[pos]) break;
364     }
365     for (int i=pos; i>=0; --i) printf("%d",bit[i]);
366 }
367 
368 char *BigInt::string()const
369 {
370     char content[MAXBIT+2];
371     int posi = 0;
372     if (negative) content[posi++] = '-';
373     int pos = MAXBIT - 1;
374     for (; pos>0; --pos) {
375         if (bit[pos]) break;
376     }
377     //printf("pos = %d\n",pos);
378     for (int i=pos; i>=0; --i) {
379         content[posi++] = bit[i] + '0';
380         //printf("bit[%d] = %d\n",i,bit[i]);
381     }
382     content[posi] = '\0';
383     return content;
384 }
385 
386 bool BigInt::isZero()const
387 {
388     bool zeroFlag = true;
389     for (int i=0; i<MAXBIT; ++i) {
390         if (bit[i] != 0) {
391             zeroFlag = false;
392             break;
393         }
394     }
395     return zeroFlag;
396 }
397 
398 bool BigInt::isPositive()const
399 {
400     return !negative && !isZero();
401 }
402 
403 bool BigInt::isNegative()const
404 {
405     return negative;
406 }
407 
408 bool BigInt::nonNegative()const
409 {
410     return !negative;
411 }
412 
413 BigInt BigInt::opposite()const
414 {
415     BigInt n(*this);
416     if (!n.isZero()) n.negative = !n.negative;
417     return n;
418 }
419 
420 BigInt BigInt::absoluteAdd(const BigInt& n)const
421 {
422     BigInt bi(*this);
423     int next = 0;//进位
424     for (int i=0; i<MAXBIT; ++i) {
425         bi.bit[i] = (bit[i] + n.bit[i] + next) % 10;
426         next   = (bit[i] + n.bit[i] + next) / 10;
427     }
428     return bi;
429 }
430 
431 BigInt BigInt::absoluteMinus(const BigInt& n)const
432 {
433     BigInt bi(*this);
434     for (int i=MAXBIT-1; i>=0; --i) {
435         if (bi.bit[i]>=n.bit[i]) bi.bit[i] -= n.bit[i];
436         else {//借位
437             int borrow = i + 1;//借位位
438             while (bi.bit[borrow]==0) ++borrow;
439             --bi.bit[borrow];
440             for (int j=i+1; j<borrow; ++j) bi.bit[j] = 9;
441             bi.bit[i] = bi.bit[i] + 10 - n.bit[i];
442         }
443     }
444     return bi;
445 }
446 
447 bool BigInt::absoluteEqual(const BigInt& n)const
448 {
449     for (int i=0; i<MAXBIT; ++i) {
450         if (bit[i] != n.bit[i]) return false;
451     }
452     return true;
453 }
454 
455 bool BigInt::absoluteGreater(const BigInt& n)const
456 {
457     for (int i=MAXBIT-1; i>=0; --i) {
458         if (bit[i]>n.bit[i]) return true;
459         else if (bit[i]<n.bit[i]) return false;
460     }
461     return false;
462 }
463 
464 bool BigInt::absoluteEqualGreater(const BigInt& n)const
465 {
466     for (int i=MAXBIT-1; i>=0; --i) {
467         if (bit[i]>n.bit[i]) return true;
468         else if (bit[i]<n.bit[i]) return false;
469     }
470     return true;
471 }

 

转载于:https://www.cnblogs.com/huanglianjing/p/4002872.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值