#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int main() { char str1[255], str2[255]; int num1[255] = { 0 }, num2[255] = { 0 };//用于存放加数,且较短者补前导零 int sum[255] = { 0 }; int length1, length2, length; int i, j; gets(str1); gets(str2); length1 = strlen(str1); length2 = strlen(str2); length = length1 > length2 ? length1 : length2; //将两个加数存入到num[]中 for (i = length - 1,j = length1 - 1; j >= 0; i--,j--) { num1[i] = str1[j] - ‘0‘; } for (i = length - 1, j = length2 - 1; j >= 0; i--, j--) { num2[i] = str2[j] - ‘0‘; } //从后往前进行加法计算 for (i = length - 1; i >= 0; i--) { sum[i] = sum[i] + num1[i] + num2[i]; if ((sum[i] >= 10) && (i != 0)) { sum[i] = sum[i] - 10; sum[i - 1]++;//表示进位 } } for (i = 0; i < length; i++) { printf("%d", sum[i]); } return 0; }
大数加法2——在较短的加数前补充前导零
时间: 2024-11-10 03:47:57