gpt4 book ai didi

c - 如何将 2 个大数作为字符串相乘(使用之前添加 2 个字符串的函数)? C

转载 作者:行者123 更新时间:2023-11-30 21:04:46 25 4
gpt4 key购买 nike

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 。

***

  • 问题是代码通常不输出任何内容,有时只是不正确的结果
<小时/>

***

最佳答案

你的做法基本是对的,但是在实现过程中存在一些错误。

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

此代码已更正上述错误:

    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"),但您应该做进一步的测试。

关于c - 如何将 2 个大数作为字符串相乘(使用之前添加 2 个字符串的函数)? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59809221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com