C++ BigInteger模板

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <string>
  4 #include <iostream>
  5 #include <iomanip>
  6 #include <algorithm>
  7 using namespace std;
  8 
  9 #define MAXN 9999
 10 #define MAXSIZE 10
 11 #define DLEN 4
 12 const long long MOD = 258280327;
 13 class BigInteger
 14 {
 15 private:
 16     int a[3000];    //可以控制大数的位数
 17     int len;       //大数长度
 18 public:
 19     BigInteger(){ len = 1; memset(a, 0, sizeof(a)); }   //构造函数
 20     BigInteger(const int);       //将一个int类型的变量转化为大数
 21     BigInteger(const char*);     //将一个字符串类型的变量转化为大数
 22     BigInteger(const BigInteger &);  //拷贝构造函数
 23     BigInteger &operator=(const BigInteger &);   //重载赋值运算符,大数之间进行赋值运算
 24 
 25     friend istream& operator>>(istream&, BigInteger&);   //重载输入运算符
 26     friend ostream& operator<<(ostream&, BigInteger&);   //重载输出运算符
 27 
 28     BigInteger operator+(const BigInteger &) const;   //重载加法运算符,两个大数之间的相加运算
 29     BigInteger operator-(const BigInteger &) const;   //重载减法运算符,两个大数之间的相减运算
 30     BigInteger operator*(const BigInteger &) const;   //重载乘法运算符,两个大数之间的相乘运算
 31     BigInteger operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算
 32 
 33     BigInteger operator^(const int  &) const;    //大数的n次方运算
 34     long long    operator%(const long long  &) const;    //大数对一个int类型的变量进行取模运算
 35     bool   operator>(const BigInteger & T)const;   //大数和另一个大数的大小比较
 36     bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较
 37 
 38     void print();       //输出大数
 39 };
 40 BigInteger::BigInteger(const int b)     //将一个int类型的变量转化为大数
 41 {
 42     int c, d = b;
 43     len = 0;
 44     memset(a, 0, sizeof(a));
 45     while (d > MAXN)
 46     {
 47         c = d - (d / (MAXN + 1)) * (MAXN + 1);
 48         d = d / (MAXN + 1);
 49         a[len++] = c;
 50     }
 51     a[len++] = d;
 52 }
 53 BigInteger::BigInteger(const char*s)     //将一个字符串类型的变量转化为大数
 54 {
 55     int t, k, index, l, i;
 56     memset(a, 0, sizeof(a));
 57     l = strlen(s);
 58     len = l / DLEN;
 59     if (l%DLEN)
 60         len++;
 61     index = 0;
 62     for (i = l - 1; i >= 0; i -= DLEN)
 63     {
 64         t = 0;
 65         k = i - DLEN + 1;
 66         if (k<0)
 67             k = 0;
 68         for (int j = k; j <= i; j++)
 69             t = t * 10 + s[j] - '0';
 70         a[index++] = t;
 71     }
 72 }
 73 BigInteger::BigInteger(const BigInteger & T) : len(T.len)  //拷贝构造函数
 74 {
 75     int i;
 76     memset(a, 0, sizeof(a));
 77     for (i = 0; i < len; i++)
 78         a[i] = T.a[i];
 79 }
 80 BigInteger & BigInteger::operator=(const BigInteger & n)   //重载赋值运算符,大数之间进行赋值运算
 81 {
 82     int i;
 83     len = n.len;
 84     memset(a, 0, sizeof(a));
 85     for (i = 0; i < len; i++)
 86         a[i] = n.a[i];
 87     return *this;
 88 }
 89 istream& operator>>(istream & in, BigInteger & b)   //重载输入运算符
 90 {
 91     char ch[MAXSIZE * 4];
 92     int i = -1;
 93     in >> ch;
 94     int l = strlen(ch);
 95     int count = 0, sum = 0;
 96     for (i = l - 1; i >= 0;)
 97     {
 98         sum = 0;
 99         int t = 1;
100         for (int j = 0; j<4 && i >= 0; j++, i--, t *= 10)
101         {
102             sum += (ch[i] - '0')*t;
103         }
104         b.a[count] = sum;
105         count++;
106     }
107     b.len = count++;
108     return in;
109 
110 }
111 ostream& operator<<(ostream& out, BigInteger& b)   //重载输出运算符
112 {
113     int i;
114     cout << b.a[b.len - 1];
115     for (i = b.len - 2; i >= 0; i--)
116     {
117         cout.width(DLEN);
118         cout.fill('0');
119         cout << b.a[i];
120     }
121     return out;
122 }
123 
124 BigInteger BigInteger::operator+(const BigInteger & T) const   //两个大数之间的相加运算
125 {
126     BigInteger t(*this);
127     int i, big;      //位数
128     big = T.len > len ? T.len : len;
129     for (i = 0; i < big; i++)
130     {
131         t.a[i] += T.a[i];
132         if (t.a[i] > MAXN)
133         {
134             t.a[i + 1]++;
135             t.a[i] -= MAXN + 1;
136         }
137     }
138     if (t.a[big] != 0)
139         t.len = big + 1;
140     else
141         t.len = big;
142     return t;
143 }
144 BigInteger BigInteger::operator-(const BigInteger & T) const   //两个大数之间的相减运算
145 {
146     int i, j, big;
147     bool flag;
148     BigInteger t1, t2;
149     if (*this>T)
150     {
151         t1 = *this;
152         t2 = T;
153         flag = 0;
154     }
155     else
156     {
157         t1 = T;
158         t2 = *this;
159         flag = 1;
160     }
161     big = t1.len;
162     for (i = 0; i < big; i++)
163     {
164         if (t1.a[i] < t2.a[i])
165         {
166             j = i + 1;
167             while (t1.a[j] == 0)
168                 j++;
169             t1.a[j--]--;
170             while (j > i)
171                 t1.a[j--] += MAXN;
172             t1.a[i] += MAXN + 1 - t2.a[i];
173         }
174         else
175             t1.a[i] -= t2.a[i];
176     }
177     t1.len = big;
178     while (t1.a[t1.len - 1] == 0 && t1.len > 1)
179     {
180         t1.len--;
181         big--;
182     }
183     if (flag)
184         t1.a[big - 1] = 0 - t1.a[big - 1];
185     return t1;
186 }
187 
188 BigInteger BigInteger::operator*(const BigInteger & T) const   //两个大数之间的相乘运算
189 {
190     BigInteger ret;
191     int i, j, up;
192     int temp, temp1;
193     for (i = 0; i < len; i++)
194     {
195         up = 0;
196         for (j = 0; j < T.len; j++)
197         {
198             temp = a[i] * T.a[j] + ret.a[i + j] + up;
199             if (temp > MAXN)
200             {
201                 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
202                 up = temp / (MAXN + 1);
203                 ret.a[i + j] = temp1;
204             }
205             else
206             {
207                 up = 0;
208                 ret.a[i + j] = temp;
209             }
210         }
211         if (up != 0)
212             ret.a[i + j] = up;
213     }
214     ret.len = i + j;
215     while (ret.a[ret.len - 1] == 0 && ret.len > 1)
216         ret.len--;
217     return ret;
218 }
219 BigInteger BigInteger::operator/(const int & b) const   //大数对一个整数进行相除运算
220 {
221     BigInteger ret;
222     int i, down = 0;
223     for (i = len - 1; i >= 0; i--)
224     {
225         ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
226         down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
227     }
228     ret.len = len;
229     while (ret.a[ret.len - 1] == 0 && ret.len > 1)
230         ret.len--;
231     return ret;
232 }
233 long long BigInteger::operator %(const long long & b) const    //大数对一个int类型的变量进行取模运算
234 {
235     int i;
236     long long d = 0;
237     for (i = len - 1; i >= 0; i--)
238     {
239         d = ((d * (MAXN + 1)) % b + (long long)(a[i])) % b;
240     }
241     return d;
242 }
243 BigInteger BigInteger::operator^(const int & n) const    //大数的n次方运算
244 {
245     BigInteger t, ret(1);
246     int i;
247     if (n<0)
248         exit(-1);
249     if (n == 0)
250         return 1;
251     if (n == 1)
252         return *this;
253     int m = n;
254     while (m>1)
255     {
256         t = *this;
257         for (i = 1; i << 1 <= m; i <<= 1)
258         {
259             t = t*t;
260         }
261         m -= i;
262         ret = ret*t;
263         if (m == 1)
264             ret = ret*(*this);
265     }
266     return ret;
267 }
268 bool BigInteger::operator>(const BigInteger & T) const   //大数和另一个大数的大小比较
269 {
270     int ln;
271     if (len > T.len)
272         return true;
273     else if (len == T.len)
274     {
275         ln = len - 1;
276         while (a[ln] == T.a[ln] && ln >= 0)
277             ln--;
278         if (ln >= 0 && a[ln] > T.a[ln])
279             return true;
280         else
281             return false;
282     }
283     else
284         return false;
285 }
286 bool BigInteger::operator >(const int & t) const    //大数和一个int类型的变量的大小比较
287 {
288     BigInteger b(t);
289     return *this>b;
290 }
291 
292 void BigInteger::print()    //输出大数
293 {
294     int i;
295     cout << a[len - 1];
296     for (i = len - 2; i >= 0; i--)
297     {
298         cout.width(DLEN);
299         cout.fill('0');
300         cout << a[i];
301     }
302     cout << endl;
303 }

 其实这个也没多大用,大数肯定直接用Java了

posted on 2015-08-04 16:28  张济  阅读(633)  评论(0编辑  收藏  举报

导航