[英]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 。
***
***
Your approach is basically right, but there are some mistakes in the implementation.你的方法基本上是对的,但是在实现上有一些错误。
addvl
, but overwrite that pointer later with the return value of add_verylong()
.addvl
分配空间,但稍后用add_verylong()
的返回值覆盖该指针。 This gives a memory leak.tempvl
, maxvl
, minvl
and newNum
.tempvl
、 maxvl
、 minvl
和newNum
会产生进一步的内存泄漏。tempvl
to the number "0"
.tempvl
初始化为数字"0"
。placeSaver
.placeSaver
的幂来移动中间产品。 What we can do is place 0
digits to the right of the product newNum
.newNum
的右侧放置0
位数字。carry
in each loop cycle.carry
。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.