简体   繁体   English

如何乘以 2 个大数字作为字符串(以前的幻想添加了 2 个字符串)? C

[英]HOW TO MULTIPLY 2 LARGE NUMBERS AS STRINGS(with a previous fanction that adds 2 strings) ? c

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;

} }

void main() {无效主(){

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);

} }

I tried to multiply the first digit of the first number from right with all of the digits from the second number moving forward to the second digit of the first number and then to multiply the placesaver by 10 .我尝试将第一个数字的第一个数字与第二个数字的所有数字相乘,然后向前移动到第一个数字的第二个数字,然后将占位符乘以 10 。

***

  • the problem is that the code outputs usually nothing and sometimes just inccorect result问题是代码通常什么都不输出,有时只是不正确的结果

***

Your approach is basically right, but there are some mistakes in the implementation.你的方法基本上是对的,但是在实现上有一些错误。

  • You allocate space for addvl , but overwrite that pointer later with the return value of add_verylong() .您为addvl分配空间,但稍后用add_verylong()的返回值覆盖该指针。 This gives a memory leak.这会导致内存泄漏。 Further memory leaks are produced by not freeing tempvl , maxvl , minvl and newNum .不释放tempvlmaxvlminvlnewNum会产生进一步的内存泄漏。
  • You forgot to initialize tempvl to the number "0" .您忘记将tempvl初始化为数字"0"
  • We cannot shift the intermediate product by multiplying each digit with the power of ten placeSaver .我们不能通过将每个数字乘以 10 placeSaver的幂来移动中间产品。 What we can do is place 0 digits to the right of the product newNum .我们可以做的是在产品newNum的右侧放置0位数字。
  • You failed to correctly set carry in each loop cycle.您未能在每个循环周期中正确设置carry
  • You missed that even the leftmost digits can produce a carry .你错过了即使最左边的数字也可以产生carry

This code has the mentioned mistakes corrected:此代码已更正上述错误:

    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;

It works for the example multiply_verylong("23345", "34565") , but you should do further tests.它适用于示例multiply_verylong("23345", "34565") ,但您应该做进一步的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM