高精板子
多次翻车的板子,还在建设中,备注很多是翻车的点......
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
char a1[10002],b1[10002];
int a[10002],b[10002],c[100002],lena,lenb,lenc,i,j,x; //x存进位
void add(){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
lena=strlen(a1);
lenb=strlen(b1);
for(i=0;i<=lena-1;++i)a[lena-i]=a1[i]-‘0‘;
for(i=0;i<=lenb-1;++i)b[lenb-i]=b1[i]-‘0‘;
lenc=1;
x=0;
while(lenc<=lena||lenc<=lenb){
c[lenc]=a[lenc]+b[lenc]+x;
x=c[lenc]/10;
c[lenc]%=10;
lenc++;
}
c[lenc]=x;
if(c[lenc]==0)lenc--; //最高位进位问题;
for(i=lenc;i>=1;--i)printf("%d",c[i]);
cout<<endl;
}
void subtract(){
char n[10002],n1[10002],n2[10002];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(n,0,sizeof(n));
memset(n1,0,sizeof(n1));
memset(n2,0,sizeof(n2));
strcpy(n1,a1);
strcpy(n2,b1);
if(strlen(n1)<strlen(n2)||(strlen(n1)==strlen(n2)&&strcmp(n1,n2)<0)){
strcpy(n,n1);
strcpy(n1,n2);
strcpy(n2,n);
printf("-");
}
lena=strlen(n1);
lenb=strlen(n2);
for(i=0;i<=lena-1;++i)a[lena-i]=n1[i]-‘0‘;
for(i=0;i<=lenb-1;++i)b[lenb-i]=n2[i]-‘0‘;
i=1;
while(i<=lena||i<=lenb){
if(a[i]<b[i]){
a[i]+=10;
a[i+1]--;
}
c[i]=a[i]-b[i];
i++;
}
lenc=i-1;//一定记得位数减去最后自增的1;
while((c[lenc]==0)&&(lenc>1)) lenc--;//去掉前导0;
for(i=lenc;i>=1;--i)printf("%d",c[i]);
cout<<endl;
}
void multiply(){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
lena=strlen(a1);
lenb=strlen(b1);
for(i=0;i<=lena-1;++i)a[lena-i]=a1[i]-‘0‘;
for(i=0;i<=lenb-1;++i)b[lenb-i]=b1[i]-‘0‘;
for(i=1;i<=lena;++i){
x=0;
for(j=1;j<=lenb;++j){//对乘数的每一位进行处理
c[i+j-1]=a[i]*b[j]+x+c[i+j-1];//当前乘积+上次乘积进位+原数
x=c[i+j-1]/10;
c[i+j-1]%=10;
}
c[i+lenb]=x;//进位
}
lenc=lena+lenb;
while((c[lenc]==0)&&(lenc>1)) lenc--;//去掉前导0;
for(i=lenc;i>=1;--i)printf("%d",c[i]);
cout<<endl;
}
void divide(){
}
void mod(){
}
int main(){
// freopen("gj.in","r",stdin);
// freopen("gj.out","w",stdout);
scanf("%s",a1);
scanf("%s",b1);
add();
subtract();
multiply();
// fclose(stdin);
// fclose(stdout);
return 0;
}