https://www.cnblogs.com/ECJTUACM-873284962/p/6509429.html
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<string> 6 #include<cmath> 7 #include<set> 8 #include<vector> 9 #include<stack> 10 #include<queue> 11 #include<map> 12 using namespace std; 13 #define ll long long 14 #define se second 15 #define fi first 16 const int INF= 0x3f3f3f3f; 17 const int N=4e6+5; 18 19 string s1,s2; 20 int a[2001],b[2001],c[4002],len1,len2;
1 void cun() 2 { 3 cin>>s1>>s2; 4 len1=s1.length(); 5 len2=s2.length(); 6 for(int i=1;i<=len1;i++) 7 a[i]=s1[len1-i]-'0'; 8 for(int i=1;i<=len2;i++) 9 b[i]=s2[len2-i]-'0'; 10 }
1 int compare() 2 { 3 if(len1>len2) return 1; 4 if(len1<len2) return -1; 5 for(int i=0;i<len1;i++){ 6 if(s1[i]>s2[i]) return 1; 7 if(s1[i]<s2[i]) return -1; 8 } 9 return 0; 10 }
1 void Plus() 2 { 3 int w=max(len1,len2); 4 for(int i=1;i<=w;i++) 5 { 6 a[i+1] +=(a[i]+b[i])/10; 7 a[i] =(a[i]+b[i])%10; 8 } 9 if(a[w+1] ) w++; 10 for(int i=w;i>=1;i--) 11 cout<<a[i]; 12 }
1 void Minus() 2 { 3 int sign=compare(); 4 int w=max(len1,len2); 5 if(sign==0){ 6 cout<<0<<endl; return; 7 } 8 if(sign==1){ 9 for(int i=1;i<=w;i++) 10 { 11 if(a[i]<b[i]) a[i]+=10, a[i+1]--; 12 a[i]=a[i]-b[i]; 13 } 14 } 15 if(sign==-1){ 16 for(int i=1;i<=w;i++) 17 { 18 if(b[i]<a[i]) b[i]+=10, b[i+1]--; 19 a[i]=b[i]-a[i]; 20 } 21 cout<<'-'; 22 } 23 while(a[w]==0){ 24 w--; 25 } //首不能为0: 26 for(int i=w;i>=1;i--) 27 cout<<a[i]; 28 }
1 void Multiply() //高精度*高精度 2 { 3 int len=len1+len2; 4 for(int i=0;i<len1;i++) //只能都从0开始。。上面的cun()函数 要s1[len1-i-1] 5 { 6 for(int j=0;j<len2;j++) 7 { 8 c[i+j] += a[i]*b[j]; 9 c[i+j+1] += c[i+j]/10; 10 c[i+j] = c[i+j]%10; 11 } 12 } 13 if(c[len]==0) len--;//判断存不存在首位进位的情况 14 if(c[len-1]==0) //判断存不存在 0*100=0 这样的情况 15 cout<<0; 16 else 17 for(int i=len-1;i>=0;i--) 18 cout<<c[i]; 19 }
来源:https://www.cnblogs.com/thunder-110/p/9288426.html