/*
1.先将每个字符数字,转换为数字再计算。
2.考虑进位
3.计算单个字符串数字时不要忘了考虑进位
4.最后两个字符串数字都计算完了之后,可能有进位,此时不要忘了加上进位。
*/
# include <stdio.h>
# include <string.h>
int main(void)
{
char str[1000],str2[1000];
int i;
for(i = 0; i < 1000; i++)
{
str[i] = 0;
str2[i] = 0;
}
while(scanf("%s %s", str, str2) != EOF)
{
getchar(); //吸收回车符。
//printf("%s\n", str2);
int i,j,len1,len2,k = 0,ex = 0;
len1 = strlen(str);
len2 = strlen(str2);
int result[1010];
for(i = len1-1, j = len2-1; i >= 0 && j >= 0; i-- ,j--)
{
int x = str[i] - '0';
int y = str2[j] - '0';
result[k++] = (x+y+ex)%10;
ex = (x+y+ex) / 10;
}
if(i < 0 && j >= 0)
{
while(j >= 0)
{
int temp1 = str2[j] - '0';
result[k++] = (temp1 + ex) % 10;
ex = (temp1 + ex) / 10;
j--;
}
}
if(i >= 0 && j < 0)
{
while(i >= 0)
{
int temp2 = str[i] - '0';
result[k++] = (temp2 + ex) % 10;
ex = (temp2 + ex) / 10;
i--;
}
}
if(ex == 1)
{
result[k++] = 1;
}
for(i = k-1; i >= 0; i--)
printf("%d", result[i]);
printf("\n");
}
return 0;
}原文地址:http://blog.csdn.net/xu758142858/article/details/44119631