【问题标题】:HOW TO MULTIPLY 2 LARGE NUMBERS AS STRINGS(with a previous fanction that adds 2 strings) ? c如何将 2 个大数字乘以字符串(使用之前添加 2 个字符串的函数)? C
【发布时间】:2020-01-19 10:38:05
【问题描述】:
verylong multiply_verylong(verylong vl1, verylong vl2)'verylong defines as typedef char* verylong'

{

size_t maxln, minln;
int carry=0, placeSaver=1, i, k ,sum=0,dif,newln,newln2,ln1,ln2;
verylong newNum , maxVl,minVl,tempvl,addvl;
ln1 = strlen(vl1); // 'length of first str'
ln2 = strlen(vl2);'length of second str'

if (ln1 >= ln2)
{


    maxln = ln1;
    minln = ln2;
    maxVl = (verylong)calloc(maxln + 1, sizeof(char));
    assert(maxVl);
    minVl = (verylong)calloc(minln + 1, sizeof(char));
    assert(minVl);
    strcpy(maxVl, vl1); 
    strcpy(minVl, vl2);
    dif = maxln - minln;
}


else 'stops debuging here'
{

    maxln = ln2;
    minln = ln1;
    maxVl = (verylong)calloc(maxln + 1, sizeof(char));
    assert(maxVl);
    minVl = (verylong)calloc(minln + 1, sizeof(char));
    assert(minVl);
    strcpy(maxVl, vl2);
    strcpy(minVl, vl1);
    dif = maxln - minln;
}

newln = 2 * maxln + 1; 'maximum length of new  required string'
newln2 = newln - 1; 'the index of the new string'

newNum = (verylong)calloc(newln,sizeof(char)); 
addvl = (verylong)calloc(newln, sizeof(char));
tempvl = (verylong)calloc(newln, sizeof(char));
for (i = minln - 1; i >= 0; i--) ' elementry school multiplication'
{


    for (k = maxln - 1; k >= 0; k--)
    {

        sum = ((minVl[i] - '0')*(maxVl[k] - '0')*placeSaver)+carry;
        if (sum >= 10)
            carry = sum / 10;
        if (k == 0)
            newNum[newln2] = '0' + sum;
        else
            newNum[newln2] = '0' + sum%10;
    newln2--;

    }


    placeSaver*=10; 
    addvl=add_verylong(newNum,tempvl);'sending the 2 strings to a previous function that adds 2 strings'
    strcpy(tempvl, addvl);


}


return addvl;

}

空主() {

char vl1[80], vl2[80];
printf("enter  large number\n"); 
gets(vl1);
printf("enter  large number\n");
gets(vl2);
verylong res = multiply_verylong(vl1, vl2);'saves the string  '
printf("%s", res);
free(res);

}

我尝试将第一个数字的第一个数字从右边乘以第二个数字的所有数字向前移动到第一个数字的第二个数字,然后将占位符乘以 10 。

***

  • 问题是代码通常什么也不输出,有时只是不正确的结果

***

【问题讨论】:

  • 请提供您的代码minimal verifiable example(即包括main 以及编译和重现问题所需的所有定义)。还请描述您在显示代码时遇到的问题 - 是否崩溃、是否一直产生错误结果、有时是否产生错误结果等。提供输入、预期结果和实际结果。
  • 例如它应该得到 23345,34565 并打印 806919925 但它什么也没打印

标签: c string function


【解决方案1】:

你的方法基本上是对的,但是在实现上还是有一些错误。

  • 您为addvl 分配空间,但稍后用add_verylong() 的返回值覆盖该指针。这会导致内存泄漏。不释放tempvlmaxvlminvlnewNum 会导致进一步的内存泄漏。
  • 您忘记将tempvl 初始化为数字"0"
  • 我们不能通过将每个数字乘以十次方placeSaver 来移动中间产品。我们可以做的是将0数字放在产品newNum的右侧。
  • 您未能在每个循环周期中正确设置carry
  • 您错过了即使是最左边的数字也可以生成carry

此代码已更正上述错误:

    newNum = calloc(newln, sizeof(char)); 
    // do not allocate space for addvl - it would be lost
    tempvl = malloc(newln);
    strcpy(tempvl, "0");    // don't forget to initialize tempvl to "0"
    for (i = minln - 1; i >= 0; i--)    // elementry school multiplication
    {
        newln2 = newln - 1; // the index of the new string
        for (carry = 0, k = maxln - 1; k >= 0; k--) // clear carry before each loop
        {
            sum = (minVl[i] - '0')*(maxVl[k] - '0') + carry;
            carry = sum/10;
            newNum[--newln2] = '0' + sum%10;
        }
        if (carry) newNum[--newln2] = '0' + carry;
        addvl = add_verylong(newNum+newln2, tempvl);    // number starts at +newln2
        free(tempvl);                                   // free obsolete number
        tempvl = addvl;                                 // rather than strcpy()
        newNum[--newln-1] = '0';                        // instead of placeSaver*=10
    }
    free(maxVl), free(minVl), free(newNum);
    return addvl;

它适用于示例multiply_verylong("23345", "34565"),但您应该做进一步的测试。

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 2022-01-19
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2014-07-07
    • 1970-01-01
    相关资源
    最近更新 更多