高精度模板

包括$+,-,*$操作,适用于非负整数

减法只能大减小

并没有压位

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 250002
 6 int max(int a,int b){return a>b?a:b;}
 7 struct bignum{
 8     int a[N],len;
 9     bignum(){memset(a,0,sizeof(a));len=0;}
10     void init(){
11         static char b[N];scanf("%s",b);
12         len=strlen(b);
13         for(int i=1;i<=len;++i)a[i]=b[len-i]-48;
14     }
15     void print(){
16         for(int i=len;i>=1;--i) putchar(a[i]+48);
17     }
18     bool operator < (const bignum &tmp) const{
19         if(len!=tmp.len) return len<tmp.len;
20         for(int i=1;i<=len;++i)
21             if(a[i]!=tmp.a[i])
22                 return a[i]<tmp.a[i];
23         return 0;
24     }
25     bignum operator + (const bignum &tmp) const{
26         bignum c; int x=0;
27         c.len=max(len,tmp.len);
28         for(int i=1;i<=c.len;++i){
29             c.a[i]=a[i]+tmp.a[i]+x;
30             x=c.a[i]/10;c.a[i]%=10;
31         }
32         for(;x;x/=10) c.a[++c.len]=x%10;
33         return c;
34     }
35     bignum operator - (const bignum &tmp) const{
36         bignum c; c.len=len;
37         for(int i=len;i>=1;--i){
38             c.a[i]=a[i]-tmp.a[i];
39             for(int j=i;c.a[j]<0;++j)
40                 --c.a[j+1],c.a[j]+=10;
41             while(!c.a[c.len]&&c.len>i) --c.len;
42         }return c;
43     }
44     bignum operator * (const bignum &tmp) const{
45         bignum c;int x=0;
46         c.len=len+tmp.len-1;
47         for(int i=1,j;i<=len;++i,x=0){
48             for(j=1;j<=tmp.len;++j){
49                 c.a[i+j-1]+=a[i]*tmp.a[j]+x;
50                 x=c.a[i+j-1]/10;c.a[i+j-1]%=10;
51             }c.a[i+j-1]+=x;
52         }
53         for(;c.a[c.len]>9;++c.len)
54             c.a[c.len+1]+=c.a[c.len]/10,c.a[c.len]%=10;
55         while(!c.a[c.len]&&c.len>1) --c.len;
56         return c;
57     }
58 }x,y;
59 int main(){
60     x.init(); y.init();
61     x=x+y;
62     //if(x<y) swap(x,y),putchar('-');
63     //x=x-y;
64     //x=x*y;
65     x.print();
66     return 0;
67 }
View Code
原文地址:https://www.cnblogs.com/kafuuchino/p/9932701.html