Project Modules
/* 总结: 1.不要忘了溢出 2.单独处理最高位的加法更容易些。 summary: 1.The overflow of addition of large numbers should be noticed. 2.It's easier to deal with the highest bit when adding,I think. */ #include <stdio.h> #include <string.h> #include <memory.h> int main() { char srca[1000], srcb[1000]; int dsta[1000], dstb[1000], sum[1000]; int carry = 0; while (1) { memset(dsta, 0x00, sizeof(dsta)); memset(dstb, 0x00, sizeof(dstb)); memset(sum , 0x00, sizeof(sum)); //test if the highest bit has a carry //to know the length of "sum" array to be displayed. carry = 0; //Don't forget test EOF, or I will get //time limit exceed. if (scanf("%s", srca) == EOF) { break; } getchar(); scanf("%s", srcb); getchar(); int i, j = 999; int longer = strlen(srca) > strlen(srcb) ? strlen(srca) : strlen(srcb); for (i = strlen(srca)-1; i >= 0; i--) { dsta[j--] = srca[i] - '0'; } j = 999; for (i = strlen(srcb)-1; i >= 0; i--) { dstb[j--] = srcb[i] - '0'; } for (i = 999; i >= 1; i--) { sum[i] += dsta[i] + dstb[i]; if (sum[i] >= 10) { sum[i] = sum[i] - 10; sum[i-1] += 1; if (i == 1000 - longer) { carry = 1; } } } sum[0] += dsta[0] + dstb[0]; if (sum[0] >= 10) { sum[0] = sum[0] - 10; } if (carry == 1) longer++; for (i = 1000 - longer; i <1000; i++) { printf("%d", sum[i]); } printf("\n"); } return 0; }