#include <stdio.h>
#include <string.h>
int main()
{
char a[1001],b[1001],r[1003],ta[1003],tb[1003];
char tp;
int h,i = 0,len_a = 0,len_b = 0,max,j,k=0,rs;
while(scanf("%d",&h)!=EOF)
{
i = 0;
while(i<h)
{
if(i) printf("\n");
scanf("%s%s",a,b);
len_a = strlen(a);len_b = strlen(b);
max = len_a>len_b?len_a:len_b;
strcpy(ta,a);strcpy(tb,b);
if (len_b < max)
{
for(j = len_b;j<max;j++)
{
b[j] = '0';
}
b[max] = '\0';
for(k = 1;k<=(max-len_b);k++)
{
for(j = max-1;j>0;j--)
{
tp = b[j];
b[j] = b[j-1];
b[j-1] = tp;
}
}
}
if (len_a < max)
{
for(j = len_a;j<max;j++)
{
a[j] = '0';
}
a[max] = '\0';
for(k = 1;k<=(max-len_a);k++)
{
for(j = max-1;j>0;j--)
{
tp = a[j];
a[j] = a[j-1];
a[j-1] = tp;
}
}
}
for(k = 0,j = max - 1;j>=0;j--)
{
rs = (a[j] - 48 + b[j] - 48) + k;
if (rs>=10)
{
k = rs/10;
rs = rs%10;
}
else k = 0;
r[j] = rs + 48;
}
r[max] = '\0';
if (k != 0)
{
r[max] = k + 48;r[max+1]='\0';
for(j = max;j>0;j--)
{
tp = r[j];
r[j] = r[j-1];
r[j-1] = tp;
}
}
printf("Case %d:\n%s + %s = %s\n",i+1,ta,tb,r);
i++;
}
}
return 0;
}
要注意大数加法的实现
算法还不是很好
还能改进