大整数模拟

用C++实现了大整数的加,减,乘,低精度除法,高精度除法。。。。

还有递归实现的,while循环实现的,大整数快速幂~~

正确性,只能说AC过几道大数,加, 乘法, 快速幂,低精度除法题目~~

是否有其他BUG,未知~~~~

算法说明:

 高精度乘法用的是,模拟手算

高精度除法用的是相减法

所以,没有什么高深算法,大数据肯定会超时。。。

 

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <algorithm>
  5 #include <iostream>
  6 using namespace std;
  7 
  8 //能解决两个大数相加,相减的,比较两个大数
  9 class BigInteger
 10 {
 11  private:
 12     string num;
 13  public:  //API
 14     //五个构造函数
 15     BigInteger() { }
 16     BigInteger(const string &str): num(str) { }
 17     BigInteger(int time, char p) : num(time,p) { }
 18     BigInteger(const BigInteger &A) { num = A.num;} //复制构造函数
 19     BigInteger(int);
 20 
 21     BigInteger add(BigInteger &);
 22     BigInteger sub(BigInteger &);
 23     BigInteger mul(BigInteger &);
 24     void insert(int pos, const char *str) { num.insert(pos,str); }
 25     void erase(int pos, int len) { num.erase(pos,len); }
 26 
 27     BigInteger div1(const BigInteger &, BigInteger &mod); //高精度除法,返回商,余数为MOD
 28     BigInteger div2(const int &, int &mod); //低精度除法,返回商,余数为mod
 29     void dealzero( );
 30 
 31     BigInteger exp(BigInteger &);//高精度整数幂
 32     BigInteger quickpower(const int p); //二分整数快速幂
 33 
 34     int size() const  { return num.size(); } //返回大数的位数
 35     char &operator[ ] (const int id) { return num[id]; } //可以用作赋值的目标,左值
 36     const char operator [ ] (const int id) const { return num[id]; }  //返回大数的第id位,不能用作赋值的目标
 37     friend bool operator < (const BigInteger &, const BigInteger &);  //重载小于号,小于返回1,否则返回0
 38     friend bool operator > (const BigInteger &, const BigInteger &);  //重载大于号,大于返回1,否则返回0
 39     friend bool operator == (const BigInteger &, const BigInteger &); //重载等于号,等于返回1,否则返回0
 40 
 41     BigInteger operator = (const BigInteger &A) { num = A.num; return *this; } //大数赋值
 42 
 43     friend BigInteger operator + (const BigInteger &,const BigInteger &);
 44     friend BigInteger operator - (const BigInteger &,const BigInteger &);
 45     friend BigInteger operator * (const BigInteger &,const BigInteger &);
 46     friend BigInteger operator / (const BigInteger &,const BigInteger &);
 47     friend BigInteger operator ^ (const BigInteger &, int );
 48     friend ostream& operator << (ostream &out, const BigInteger &); //重载输出操作符
 49     void format(int pos); 
 50     friend istream& operator >> (istream &in, BigInteger &); //重载输入操作符合
 51 
 52 
 53 };
 54 
 55 BigInteger::BigInteger(int p)
 56 {
 57    int cnt = 0;
 58    char c[10];
 59    while( p )
 60    {
 61      c[0]  = p % 10 + '0';
 62      c[1] = 0;
 63      num.insert(cnt++,&c[0]);
 64      p /= 10;
 65    }
 66    reverse(num.begin(),num.end());
 67 }
 68 
 69 //比较两个正数的大小
 70 bool operator < (const BigInteger &A, const BigInteger &B)
 71 {
 72   int lenA = A.size();
 73   int lenB = B.size( );
 74   if( lenA > lenB ) return 0;
 75   else if ( lenA < lenB ) return 1;
 76   if( A.num < B.num ) return 1;
 77   else return 0;
 78 }
 79 
 80 
 81 bool operator >  (const BigInteger &A, const BigInteger &B)
 82 {
 83   int lenA = A.size();
 84   int lenB = B.size( );
 85   if( lenA > lenB ) return 1;
 86   else if ( lenA < lenB ) return 0;
 87   if( A.num > B.num ) return 1;
 88   else return 0;
 89 }
 90 
 91 bool operator ==  (const BigInteger &A, const BigInteger &B)
 92 {
 93   int lenA = A.size();
 94   int lenB = B.size();
 95   if( lenA != lenB ) return 0;
 96   if( A.num == B.num ) return 1;
 97   else return 0;
 98 }
 99 
100 //大数的幂运算,采用非递归实现
101 BigInteger operator ^ (const BigInteger &A, int p)
102 {
103    BigInteger C("1");
104    BigInteger B(A);
105    while( p )
106    {
107      if( p&1 ) C = C * B;
108      p >>= 1;
109      B = B * B; 
110    } 
111    return C;
112 
113 }
114 
115 //采用递归实现
116 BigInteger BigInteger::quickpower(const int p)
117 {
118   if( p == 0 )
119      return BigInteger("1");
120   else if( p == 1 )
121      return *this;
122   else
123   {
124      BigInteger temp = quickpower( p / 2 );
125      if( p&1 ) return temp * temp * (*this);
126      else   return temp * temp;
127   }
128 
129 }
130 
131 
132 //大数乘法
133 BigInteger operator * (const BigInteger &A, const BigInteger &B)
134 {
135   int lenA = A.size();
136   int lenB = B.size();
137   BigInteger C(lenA,'0'),D(lenB,'0'),E(lenA+lenB+1,'0');
138   for(int i = lenA - 1, j = 0; i >= 0; --i, ++j)
139        C[j] = A[i];    
140 
141   for(int i = lenB - 1, j = 0; i >= 0; --i, ++j)
142        D[j] = B[i];
143   int carry,mod,v;
144   for(int i = 0; i < lenA; ++i)
145   {
146      for(int j = 0; j < lenB; ++j)
147      {
148           v = (C[i] - '0') * (D[j] - '0') + E[i+j] - '0';
149           carry = v / 10;
150           mod = v - 10 * carry;
151           E[i + j ] = mod + 48;
152           E[i + j + 1] = int(E[i+j+1]) + carry; 
153      }
154 
155   }
156   for(int i = 0; i < lenA + lenB; ++i)
157   {
158      v = E[i] - '0';
159      carry = v / 10;
160      mod = v - 10 * carry;
161      E[i] =  mod + 48 ;
162      E[i+1] = int(E[i+1]) + carry; 
163      
164   }
165   int end = lenA + lenB;
166   while( E[end] == '0' && end >= 1) end--;
167   BigInteger F(end+1,'0');
168   for(int i = 0; end >= 0; ++i, --end) 
169      F[i] = E[end]; 
170   return F;
171 
172 }
173 
174 
175 //大数除法
176 BigInteger operator / (const BigInteger &A, const BigInteger &B)
177 {
178 
179 
180 }
181 
182 //大数加法
183 BigInteger operator + (const BigInteger &A, const BigInteger &B)
184 {
185    int lenA = A.size();
186    int lenB = B.size();
187    int maxn = lenA > lenB ? lenA + 1: lenB + 1;
188    BigInteger C(maxn,'0');
189    int cnt = 0;
190    --lenA; --lenB;
191    for( ; lenA >= 0 && lenB >= 0; --lenA, --lenB, ++cnt)
192    {
193      int a = A[lenA] - 48;
194      int b = B[lenB] - 48;
195      int c = C[cnt] - 48;
196      if( a + b + c >= 10 )
197      {
198         C[cnt] = (a + b + c ) % 10 + 48;
199         C[cnt + 1] = 1 + 48;
200      }
201      else
202      {
203         C[cnt] = a + b + c + 48;
204      }
205    }   
206    if( lenA >= 0 )
207    {
208        while( lenA >= 0 )
209        { 
210          int a = A[lenA] - 48;
211          int c = C[cnt] - 48;
212          if( a + c >= 10 )
213      {
214             C[cnt] = (a + c ) % 10 + 48;
215             C[cnt + 1] = 1 + 48;
216          } 
217          else
218            C[cnt] = a + c + 48;;
219          --lenA;
220          ++cnt;
221        }
222    } 
223    else if( lenB >= 0 )
224    {
225        while( lenB >= 0 )
226        { 
227          int b = B[lenB] - 48;
228          int c = C[cnt] - 48;
229          if( b + c >= 10 )
230      {
231             C[cnt] = (b + c ) % 10 + 48;
232             C[cnt + 1] = 1 + 48;
233          } 
234          else
235             C[cnt] = b + c + 48;
236          --lenB;
237          ++cnt;
238        }
239 
240    }
241    int end = C.size() - 1;
242    while( C[end] == '0' && end >= 0) --end;
243    BigInteger D(end + 1,'0');
244    for(int i = 0; end >= 0; i++)
245       D[i] = C[end--];
246    return D;
247 }
248 
249 //大数减法
250 BigInteger operator - (const BigInteger &A, const BigInteger &B)
251 {
252    int flag = 0; 
253    int lenA = A.size();
254    int lenB = B.size();
255    int maxn = lenA > lenB ? lenA + 1: lenB + 1;
256    BigInteger E,F;
257    //将大的数保存在E中,小的数保存在F中
258    if( A < B ) { E = B; F = A; flag = 1; lenA = B.size(); lenB = A.size(); }
259    else { E = A; F = B; } 
260    BigInteger C(maxn,'*');
261 
262    int cnt = 0;
263    --lenA; --lenB;
264    int carry = 0;
265    for( ; lenA >= 0 && lenB >= 0; --lenA, --lenB, ++cnt)
266    {
267      int a = E[lenA] - 48;
268      int b = F[lenB] - 48;
269      int c = C[cnt] - 48;
270      if( a - b + carry < 0 ) //因为E>F,最高位绝对不会借位,所以总能借到位
271      {
272           C[cnt] = (a + 10 - b + carry ) % 10 + 48;
273           carry = -1;
274      }
275      else
276      {
277         C[cnt] = a - b + carry +  48;
278         carry = 0;
279      }
280    }   
281    if( lenA >= 0 )
282    {
283        while( lenA >= 0 )
284        { 
285          int a = E[lenA] - 48;
286          if( a + carry < 0 )
287      {
288            C[cnt] = (a + carry + 10 ) % 10 + 48;
289            carry = -1;
290          } 
291          else
292          {
293             C[cnt] = a + carry + 48;
294             carry = 0;
295          }
296          --lenA;
297          ++cnt;
298        }
299    } 
300 
301    int end = C.size() - 1;
302    while( C[end] == '*' && end >= 0) --end;
303    while( C[end] == '0' && end >= 1) --end; //去除0,保留一个
304    BigInteger D(end + 1 + flag,'0');
305    if( flag ) D[0] = '-';
306    for(int i = flag; end >= 0; i++)
307       D[i] = C[end--];
308    return D;
309 }
310 
311 
312 ostream& operator << (ostream &out, const BigInteger &A)
313 {
314   for(int i = 0; i != A.size(); i++)
315         if( A[i] >= '0' && A[i] <= '9' )
316            out<<(int(A[i]) - 48);
317         else 
318        out<<A[i];
319   return out;
320 }
321 
322 
323 //小数位为pos
324 //0.99999999999999999999990 1
325 //HDU 1063
326 void BigInteger::format (int pos)
327 {
328   int len = this->size();
329   int tmp = len;
330   //首先去除小数点后的0
331   while(pos != -1 && --tmp && len - tmp <= pos && (*this)[tmp] == '0' && tmp >= 0);
332   if( pos > len && pos != -1) printf(".");
333   for(int x = pos - len - 1; x >= 0 ; x--)
334     printf("0");
335   for(int i = 0; i <= tmp; ++i)
336   {
337        if( (*this)[i] == 0 ) continue;
338        if(  len - pos == i  && pos != -1 )
339        {
340            printf(".");
341        }
342        printf("%d",(*this)[i] - 48);
343   }
344   puts("");
345        
346 }
347 
348 istream& operator >>(istream &in, BigInteger &A)
349 {
350   in >> A.num;
351   return in;
352 }
353 
354 BigInteger BigInteger::add(BigInteger &rthis)
355 {
356   return *this + rthis;
357 }
358 
359 BigInteger BigInteger::sub(BigInteger &rthis)
360 {
361   return *this - rthis;
362 }
363 
364 //逐位相除,然后逐位相减
365 //返回商,余数为MOD
366 
367 //去除前导0
368 void BigInteger::dealzero( )
369 {
370    int start = 0,pos = this->size();
371    while( (*this)[start] == '0' && start != pos - 1) ++start; //消除前导0后
372    //cout<<"交换前:"<<(*this)<<endl;
373    for(int x = start, t  = 0;t != x && x < pos; ++x,++t)
374    swap((*this)[x],(*this)[t]);
375    this->erase(pos - start,start);
376    //cout<<"交换后:"<<(*this)<<endl;
377    //cout<<"大小:"<<this->size()<<endl;
378   // int tt;
379   // scanf("%d",&tt);
380 }
381 
382 BigInteger BigInteger::div1(const BigInteger &rthis, BigInteger &mod)
383 {
384   int len = (*this).size();
385   int rlen = rthis.size();
386   if( (*this) < rthis )
387   {
388      mod = (*this);
389      return BigInteger("0");
390   } 
391   if( (*this) == rthis )
392   { 
393      mod = BigInteger("0");
394      return BigInteger("1");
395   }
396   BigInteger C;
397   BigInteger D(rthis);
398   BigInteger Result(len + 2,'*');
399   int start = 0, pos = 0, bit = 0, stop = 0;
400   char cc[2];
401   for(int i = 0; i < len && !stop; i++) //枚举每位
402   {
403      pos = C.size(); 
404      cc[0] = (*this)[i]; 
405      cc[1] = 0;
406      C.insert(pos++,&cc[0]);
407      C.dealzero();
408      if( C < rthis ) { Result[bit] = '0'; ++bit; continue; }
409      while( !(C < rthis) )
410      {
411         pos = C.size();
412         start = 0;
413         for(int j = pos - 1, k = rlen - 1; j >= start; --j, --k)
414         {
415             if( k >= 0 )
416             C[j] = C[j] - D[k] + '0';
417             if( C[j] - '0' < 0 )
418             {
419                C[j-1] = int(C[j-1]) - 1;
420                C[j] = int(C[j]) + 10 ;
421             }  
422             //cout<<"C[j] = "<<C[j]<<endl;
423         }
424         C.dealzero();
425         if( Result[bit] == '*' ) Result[bit] = '0';
426         Result[bit] = Result[bit] + 1; 
427         //cout<<"Result:"<<Result<<endl;
428         if( C == BigInteger("0") ) //如果余数为0
429         {
430            int tflag = 0; //判断被除数剩余位数是否为0
431            for(int l = i + 1; l != len; l++)
432           if( (*this)[l] != '0' ) { tflag = 1;  break; }
433            
434            if( tflag ) break; 
435            stop = 1;
436            for(int l = i + 1; l != len; l++)
437               Result[++bit] = (*this)[l];
438 
439            break;
440         }  
441         //int ttt;
442         //scanf("%d",&ttt);
443      }
444      ++bit;
445 
446   }
447   //处理商
448   int tmp = len;
449   start = -1;
450   while( Result[++start] == '0' );
451   while( Result[--len] == '*');
452   BigInteger DD;
453   pos = 0;
454   for(int x = start; x <= len; ++x)
455   {
456     cc[0] = Result[x];
457     cc[1] = 0;
458     DD.insert(pos++,cc);    
459   } 
460   //cout<<"商"<<DD<<endl;
461  // cout<<"余数:"<<C<<endl;
462   //余数
463   mod = C;
464 
465   return DD;
466 }
467 
468 BigInteger BigInteger::div2(const int &p, int &mod)
469 {
470    int len = this->size();
471    int result = 0, cnt =  0;
472    BigInteger res(len,'*');
473    for(int i = 0; i != len; ++i)
474    {
475        result = result * 10 + ((*this)[i] - '0');
476        res[cnt++] = result / p + '0'; 
477        result = result % p;
478    }
479    mod = result;
480    int start = -1;
481    len = cnt;
482    while( res[++start] == '0' ); //去除前导0
483    BigInteger fin(len - start,'0');
484    cnt = 0;
485    for(int i =  start; i < len; ++i)
486        fin[cnt++] = res[i];
487    return fin;
488 }
489 
490 int main( )
491 {
492    BigInteger A,B;
493    while( cin >> A >> B )
494    {
495       BigInteger C,MOD;
496       C = A.div1(B,MOD);
497       cout<<"商:"<<C<<endl;
498       cout<<"余数:"<<MOD<<endl;
499       if( C * B + MOD == A )
500       {
501          puts("yes");
502 
503       }
504       else
505          puts("no");
506        BigInteger D = A - B;
507        if( D + B == A )
508        {
509           puts("yes");
510        }
511        else puts("no");
512    }
513    return 0;
514 }
515 
516 
517 /*
518      if( line != 0 ) puts(""); 
519      printf("Case %d:\n",++line);
520      cout<<"大数加法"<<endl;
521      cout<<A<<" + "<<B<<" = "<<A+B<<endl;
522      cout<<"大数减法"<<endl;
523      cout<<A<<" - "<<B<<" = "<<A-B<<endl;
524      cout<<"大数关系比较"<<endl;
525      puts("<");
526      if( A < B ) puts("True");
527      else puts("False");
528      puts(">");
529      if( A > B ) puts("True");
530      else puts("False");
531      puts("==");
532      if( A == B ) puts("True");
533      else puts("False");
534      cout<<"大数加,减法"<<endl;
535      cout<<A.add(B)<<endl;;
536      cout<<A.sub(B)<<endl;;
537      cout<<"大数乘法:"<<endl;
538      cout<<A*B<<endl;
539      cout<<"请输入大数:"<<endl;
540      BigInteger C;
541      cin>>C;
542      cout<<"请输入幂:"<<endl;
543      int p;
544      cin>>p;
545      cout<<"朴素O(N)算法:"<<endl;
546      cout<<(C^p)<<endl;
547      cout<<"二分快速幂O(lgN)算法:"<<endl;
548 
549 
550     
551      BigInteger C;
552      cin>>C;
553      cout<<"请输入幂:"<<endl;
554      int p;
555      cin>>p;
556      cout<<"二分快速幂O(lgN)算法:"<<endl;
557      cout<<C.quickpower(p)<<endl;
558      cout<<(C^p)<<endl;
559 
560 
561      int len = strlen(str);
562      int cur = -1,maxn = -1;
563      for(int i = 0; str[i]; ++i)
564      {
565         if( str[i] == '.' ) 
566         {
567          cur = i; 
568          maxn = (len - cur - 1) * p;
569          break;
570         }
571      }
572      if( cur != -1 )
573      {
574        for(int i = cur; i < len - 1; i++)
575        str[i] = str[i+1];
576        str[len- 1] = 0;
577      }
578      BigInteger A(str);
579      A = A^p;
580      A.format(maxn);
581 
582     while( N-- )
583     {
584      BigInteger A;
585      int p,mod  = 0;
586      cout<<"请输入大整数:"<<endl;
587      cin>>A;
588      cout<<"请输入除数:"<<endl;
589      cin>>p;
590      BigInteger C = A.div2(p,mod);
591      cout<<"商为:"<<C<<endl;
592      cout<<"余数为:"<<mod<<endl;
593      if( (BigInteger(mod) + BigInteger(p) * C)  == A )
594      {
595          puts("yes");
596 
597     }
598 
599 */

 

posted on 2013-04-15 18:30  luckyboy1991  阅读(297)  评论(0编辑  收藏  举报