大数加法出问题了
程序代码:
char *add(char *a, char *b) { char temp[STR_LEN]; size_t len_a, len_b, len_result; int carry1 = 0, carry2 = 0; unsigned i, j; if (strlen(a) < strlen(b)) { strcpy(temp, a); strcpy(a, b); strcpy(b, temp); } len_a = strlen(a); len_b = strlen(b); char *result = (char *)malloc(sizeof(char) * (len_a + 1)); for (i = len_a - 1; i >= 0; i--) { carry1 = carry2; carry2 = (a[i] + (i - len_a + len_b >= 0 ? b[i - len_a + len_b] : '0') - 2 * '0' + carry1) / 10; result[i + 1] = (a[i] + (i - len_a + len_b >= 0 ? b[i - len_a + len_b] : '0') - 2 * '0' + carry1) % 10 + '0';//这里有问题 } result[0] = carry1; result[len_a] = '\0'; //去掉最前面无意义的0 for (i = 0; result[i] == '0'; i++); for (j = 0; result[i]; i++, j++) result[j] = result[i]; if (j == 0) result[j++] = '0'; result[j] = '\0'; return result; }
调试的时候发现的问题:
但是我申请的空间够啊,这个提示是什么意思?