高精度模板

用类实现的高精度模板实在是太臃肿了,用过程实现一次方便记忆

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<iomanip>
  5 using namespace std;
  6 
  7 const int MAXN=1000;
  8 const int DLEN=4;
  9 const int WIDE=10000;
 10 class BigNum
 11 {
 12 public:
 13     int NUM[MAXN];
 14     int L;
 15     bool flag;
 16     BigNum(){memset(NUM,0,sizeof(NUM));L=1;flag=0;}
 17     BigNum(const BigNum &T){memcpy(NUM,T.NUM,sizeof(NUM));L=T.L;flag=T.flag;}
 18     BigNum(int n){memset(NUM,0,sizeof(NUM));NUM[0]=n;L=1;while(NUM[L-1]>=WIDE){NUM[L]+=NUM[L-1]/WIDE;NUM[L-1]%=WIDE;L++;}flag=0;}
 19 };
 20 
 21 void Input(string s,BigNum &T)
 22 {
 23     int k=1,num=0;
 24     memset(T.NUM,0,sizeof(int)*MAXN);T.L=0;
 25     for(int i=s.size()-1;i>=0;i--)
 26     {
 27         if(k==WIDE) T.NUM[T.L++]=num,num=0,k=1;
 28         num+=k*(s[i]-'0');
 29         k*=10;
 30     }
 31     if(num>0) T.NUM[T.L++]=num;
 32 }
 33 
 34 void Output(const BigNum T)
 35 {
 36     if(T.flag==1) cout<<'-';
 37     cout<<T.NUM[T.L-1];
 38     for(int i=T.L-2;i>=0;i--)
 39     {
 40         cout.width(DLEN);
 41         cout.fill('0');
 42         cout<<T.NUM[i];
 43     }
 44 }
 45 
 46 bool cmp(const BigNum A,const BigNum B)
 47 {
 48     if(A.L!=B.L) return A.L<B.L;
 49     for(int i=A.L-1;i>=0;i--)
 50         if(A.NUM[i]!=B.NUM[i])
 51             return A.NUM[i]<B.NUM[i];
 52     return 0;
 53 }
 54 
 55 BigNum Add(const BigNum A,const BigNum B)
 56 {
 57     BigNum C;
 58     int L=max(A.L,B.L);C.L=L;
 59     for(int i=0;i<L;i++)
 60     {
 61         C.NUM[i]+=A.NUM[i]+B.NUM[i];
 62         if(C.NUM[i]>=WIDE)
 63             C.NUM[i]-=WIDE,C.NUM[i+1]++;
 64     }
 65     if(C.NUM[L]>0) C.L++;
 66     return C;
 67 }
 68 
 69 BigNum Add(const BigNum A,int B)
 70 {
 71     BigNum C(A);
 72     C.NUM[0]+=B;
 73     for(int i=0;i<C.L;i++)
 74         if(C.NUM[i]>=WIDE)
 75             C.NUM[i+1]+=C.NUM[i]/WIDE,C.NUM[i]%=WIDE;
 76     if(C.NUM[C.L]>0) C.L++;
 77     return C;
 78 }
 79 
 80 BigNum Dec(BigNum A,BigNum B)
 81 {
 82     BigNum *X=&A,*Y=&B,C;
 83     int L=max(X->L,Y->L);
 84     C.L=L;
 85     if(cmp(*X,*Y)) swap(X,Y),C.flag=1;
 86     for(int i=0;i<L;i++)
 87     {
 88         C.NUM[i]+=X->NUM[i]-Y->NUM[i];
 89         if(C.NUM[i]<0)
 90             C.NUM[i]+=WIDE,C.NUM[i+1]--;
 91     }
 92     while(C.NUM[C.L-1]==0) C.L--;
 93     return C;
 94 }
 95 
 96 BigNum Dec(BigNum A,int B)
 97 {
 98     BigNum C(A);
 99     C.NUM[0]-=B;
100     for(int i=0;i<C.L;i++)
101         if(C.NUM[i]<0)
102             C.NUM[i+1]+=C.NUM[i]/WIDE-1,C.NUM[i]=WIDE-abs(C.NUM[i]%WIDE);
103     if(C.NUM[C.L-1]==0) C.L--;
104     return C;
105 }
106 
107 BigNum Mult(const BigNum A,const BigNum B)
108 {
109     BigNum C;
110     for(int i=0;i<A.L;i++)
111         for(int j=0;j<B.L;j++)
112         {
113             C.L=i+j;
114             C.NUM[C.L]+=A.NUM[i]*B.NUM[j];
115             if(C.NUM[C.L]>=WIDE)
116                 C.NUM[C.L+1]+=C.NUM[C.L]/WIDE,C.NUM[C.L]%=WIDE;
117         }
118     C.L=A.L+B.L;
119     if(C.NUM[C.L-1]==0) C.L--;
120     return C;
121 }
122 
123 BigNum Mult(const BigNum A,int B)
124 {
125     BigNum C(A);
126     int i,tmp,k=0;
127     for(i=0;i<C.L||k;i++)
128     {
129         tmp=C.NUM[i]*B+k;
130         k=tmp/WIDE;
131         C.NUM[i]=tmp%WIDE;
132     }
133     C.L=i;
134     return C;
135 }
136 
137 BigNum Div2(const BigNum A)
138 {
139     BigNum C(A);
140     for(int i=C.L-1;i>=0;i--)
141     {
142         if(C.NUM[i]%2&&i>0)
143             C.NUM[i-1]+=WIDE;
144         C.NUM[i]/=2;
145     }
146     if(C.NUM[C.L-1]==0) C.L--;
147     return C;
148 }
149 
150 BigNum Div(const BigNum A,const BigNum B)
151 {
152     BigNum L(1),R(A),T;
153     while(cmp(Add(L,1),R))
154     {
155         T=Div2(Add(L,R));
156         if(cmp(A,Mult(T,B)))
157             R=T;
158         else 
159             L=T;
160     }
161     return L;
162 }
163 
164 BigNum Div(const BigNum A,int B)
165 {
166     BigNum C(A);
167     int k=0;
168     for(int i=C.L-1;i>=0;i--)
169     {
170         k=k*WIDE+C.NUM[i];
171         C.NUM[i]=k/B;
172         k%=B;
173     }
174     while(C.NUM[C.L-1]==0) C.L--;
175     return C;
176 }
177 
178 BigNum A,B,C;
179 
180 int main()
181 {
182     string s1,s2;
183     cin>>s1>>s2;
184     Input(s1,A);
185     Input(s2,B);
186     C=Add(A,B);
187     Output(C);
188     return 0;
189 }

 

posted @ 2016-09-30 21:43  InWILL  阅读(323)  评论(0编辑  收藏  举报