string.Contains(",")和string.IndexOf(",")!=-1比那个效率高

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是对代码进行的一些完善和优化: 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; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值