超长整型数相加
#include <stdio.h>#include <string.h>
#include<math.h>
char a[102],b[102];
int pd(char e[],char f[],int m,int n,int p)
{
while(m<=p){
if(e[m]>f[n])
return 0;
else if(e[m]<f[n])
return 1;
else{
m++;
n++;
pd(e,f,m,n,p);
}
}
return 1;
}
int main()
{
int len1,len2;
void zs(char a[],char b[],int len1,int len2);
void fs(char a[],char b[],int len1,int len2);
scanf("%s%s",a,b);
len1=strlen(a)-1;
len2=strlen(b)-1;
if(a[0]!='-'&&b[0]!='-')
zs(a,b,len1,len2);
else
fs(a,b,len1,len2);
return 0;
}
void zs(char a[],char b[],int len1,int len2)
{
char sum[102],c[102],d[102];
int i=0,count=0,m1,m2,h;
while(len1>=0||len2>=0){
if(len1<0)
c[i]='0';
else
c[i]=a[len1];
if(len2<0)
d[i]='0';
else
d[i]=b[len2];
m1=c[i]-'0';
m2=d[i]-'0';
h=m1+m2+count;
count=h/10;
sum[i]=h%10+'0';
len1--;
len2--;
i++;
}
if(h>=10)
printf("1");
for(i--;i>=0;i--)
printf("%c",sum[i]);
printf("\n");
}
void fs(char a[],char b[],int len1,int len2)
{
char c[102],d[102],sum[103],e[102],f[102];
int i=0,count=0,m1,m2,h,len3,len4,p,q,t=1,s,len5,len6;
if(a[0]=='-'&&b[0]=='-')
{
strcpy(e,a+1);
strcpy(f,b+1);
len5=len3=strlen(e)-1;
len6=len4=strlen(f)-1;
while(len3>=0||len4>=0){
if(len3<0)
c[i]='0';
else
c[i]=e[len3];
if(len4<0)
d[i]='0';
else
d[i]=f[len4];
m1=c[i]-'0';
m2=d[i]-'0';
h=m1+m2+count;
count=h/10;
sum[i]=h%10+'0';
len3--;
len4--;
i++;
}
if((len5==len6)&&(e[0]=='0')&&(f[0]=='0'));
else
printf("-");
if(h>=10)
printf("1");
for(i--;i>=0;i--)
printf("%c",sum[i]);
printf("\n");
}
/***************************************************/
else
{
if(a[0]=='-'&&b[0]!='-'){
strcpy(e,a+1);
p=len3=strlen(e)-1;
strcpy(f,b);
q=len4=len2;
s=pd(e,f,i,i,p);
while(len3>=0||len4>=0){
if(len3<0)
c[i]='0';
else
c[i]=e[len3];
if(len4<0)
d[i]='0';
else
d[i]=f[len4];
m1=c[i]-'0';
m2=d[i]-'0';
if(p==1&&q==1){
h=m1-m2;}
else if(len3==len4){
if(s==0){
if(m1-count<m2){
h=m1+10-m2-count;
count=1;}
else{
h=m1-m2-count;
count=0;}}
else{
if(m2-count<m1){
h=m2+10-m1-count;
count=1;}
else{
h=m2-m1-count;
count=0;}}
}
else{
if(len3>len4)
{
if(m1-count<m2){
h=m1+10-m2-count;
count=1;}
else{
h=m1-m2-count;
count=0;}
}
else
{
if(m2-count<m1){
h=m2+10-m1-count;
count=1;}
else{
h=m2-m1-count;
count=0;}
}
}
sum[i]=(int)fabs(h)+'0';
len3--;
len4--;
i++;
}
if(p==1&&q==1&&m2<m1)
printf("-");
else if(len3==len4){
if(s==0){
printf("-");}}
else{
if(len3>len4)
printf("-");}
for(i--;i>=0;i--)
{
while(t&&i)
{
if(sum[i]=='0')
i--;
else{
t=0;
}
}
while(t==0&&i>0||i==0)
{
printf("%c",sum[i]);
i--;
}}
printf("\n");
}
/*****************************************************/
if(b[0]=='-'&&a[0]!='-'){
strcpy(e,b+1);
strcpy(f,a);
p=len3=strlen(e)-1;
q=len4=len1;
s=pd(e,f,i,i,p);
while(len3>=0||len4>=0){
if(len3<0)
c[i]='0';
else
c[i]=e[len3];
if(len4<0)
d[i]='0';
else
d[i]=f[len4];
m1=c[i]-'0';
m2=d[i]-'0';
if(p==1&&q==1){
h=m1-m2;}
else if(len3==len4){
if(s==0){
if(m1-count<m2){
h=m1+10-m2-count;
count=1;}
else{
h=m1-m2-count;
count=0;}}
else{
if(m2-count<m1){
h=m2+10-m1-count;
count=1;}
else{
h=m2-m1-count;
count=0;}}
}
else{
if(len3>len4)
{
if(m1-count<m2){
h=m1+10-m2-count;
count=1;}
else{
h=m1-m2-count;
count=0;}
}
else
{
if(m2-count<m1){
h=m2+10-m1-count;
count=1;}
else{
h=m2-m1-count;
count=0;}
}
}
sum[i]=(int)fabs(h)+'0';
len3--;
len4--;
i++;
}
if(p==1&&q==1&&m2<m1)
printf("-");
else if(len3==len4){
if(s==0){
printf("-");}}
else{
if(len3>len4)
printf("-");}
for(i--;i>=0;i--){
while(t&&i)
{
if(sum[i]=='0')
i--;
else{
t=0;
}
}
while(t==0&&i>0||i==0)
{
printf("%c",sum[i]);
i--;
}}
printf("\n");
}
}
}题目,输入两个数a,b,输出a+b的值,(|a|,|b|<=10^100),这个代码太长,有个测试用例老超时,求解,谢谢了