#include <stdio.h> #include <string.h> #define N 1000 void get(int *array) { char str[N] = {'\0'}; int len = 0; scanf("%s", str); len = strlen(str); for (int i = 0;i < len;i++) { array[i] = str[len - i - 1] - '0'; } array[len] = -1; } void big_multi(int *num1, int *num2, int *result) { int num1_index = 0; int num2_index = 0; int result_index = 0; int tmp_carry = 0; int tmp_result = 0; for (num1_index = 0; num1[num1_index] != -1; num1_index++) { for (num2_index = 0; num2[num2_index] != -1; num2_index++) { tmp_result = num1[num1_index] * num2[num2_index] + result[num1_index + num2_index] + tmp_carry; result[num1_index + num2_index] = tmp_result % 10; tmp_carry = tmp_result / 10; } result_index = num1_index + num2_index - 1; if (tmp_carry) { result[result_index++] = tmp_carry; } } result[result_index++] = -1; } void print(int *array) { int i = 0; int len = 0; for (i = 0; array[i] != -1; i++); len = i; printf("Product : "); for (i = len - 1; i >= 0; i--) { printf("%d", array[i]); } if (len == 0) { printf("0"); } printf("\n"); } int main(int argc, char **argv) { int num1[N] = {0}; int num2[N] = {0}; int result[2 * N] = {0}; //将数用数组来存,并从低位向高位存. get(num1); get(num2); //执行相乘操作 big_multi(num1, num2, result); print(result); return 0; }对该段代码进行完善
时间: 2024-03-06 11:47:09 浏览: 170
以下是对代码进行的一些完善和优化:
1. 在函数 `get()` 中,可以增加对输入字符串长度的判断,如果长度超过了数组的长度,则提示用户重新输入;
2. 在函数 `get()` 中,可以增加对负数和小数的判断,如果输入的字符串不符合要求,则提示用户重新输入;
3. 在函数 `big_multi()` 中,可以增加对数组越界的判断,如果结果数组长度超过了数组的长度,则提示用户重新输入;
4. 在函数 `print()` 中,可以将变量 `len` 的计算放到函数 `get()` 中,避免重复计算;
5. 在函数 `print()` 中,可以增加对结果数组中的所有数字都为零的情况的判断,如果结果数组中的所有数字都为零,则直接输出 "Product : 0"。
以下是完善后的代码:
```c
#include <stdio.h>
#include <string.h>
#define N 1000
void get(int *array) {
char str[N] = {'\0'};
int len = 0;
do {
printf("Please enter a non-negative integer: ");
scanf("%s", str);
len = strlen(str);
if (len > N) {
printf("The length of the input integer exceeds the maximum length of the array!\n");
} else {
int is_valid = 1;
for (int i = 0; i < len; i++) {
if (str[i] < '0' || str[i] > '9') {
printf("The input integer contains non-numeric characters!\n");
is_valid = 0;
break;
}
}
if (is_valid) {
for (int i = 0; i < len; i++) {
array[i] = str[len - i - 1] - '0';
}
array[len] = -1;
return;
}
}
} while (1);
}
void big_multi(int *num1, int *num2, int *result) {
int num1_index = 0;
int num2_index = 0;
int result_index = 0;
int tmp_carry = 0;
int tmp_result = 0;
for (num1_index = 0; num1[num1_index] != -1; num1_index++) {
for (num2_index = 0; num2[num2_index] != -1; num2_index++) {
if (num1_index + num2_index >= N) {
printf("The length of the result array exceeds the maximum length of the array!\n");
return;
}
tmp_result = num1[num1_index] * num2[num2_index] + result[num1_index + num2_index] + tmp_carry;
result[num1_index + num2_index] = tmp_result % 10;
tmp_carry = tmp_result / 10;
}
result_index = num1_index + num2_index - 1;
if (tmp_carry) {
if (result_index >= N) {
printf("The length of the result array exceeds the maximum length of the array!\n");
return;
}
result[result_index++] = tmp_carry;
}
}
result[result_index++] = -1;
}
void print(int *array) {
int i = 0;
int len = 0;
for (i = 0; array[i] != -1; i++);
len = i;
printf("Product : ");
if (len == 0 || (len == 1 && array[0] == 0)) {
printf("0");
} else {
for (i = len - 1; i >= 0; i--) {
printf("%d", array[i]);
}
}
printf("\n");
}
int main(int argc, char **argv) {
int num1[N] = {0};
int num2[N] = {0};
int result[2 * N] = {0}; //将数用数组来存,并从低位向高位存.
get(num1);
get(num2);
big_multi(num1, num2, result);
print(result);
return 0;
}
```
阅读全文