C语言中字符串中的乘法

C语言中字符串中的乘法,c,string,multiplication,C,String,Multiplication,我写了一个函数,本来应该这样做的,但它出了问题,我不知道是什么 主文件: int main() { char *num1, *num2; num1=(char *) calloc(3, sizeof(char)); num2=(char *) calloc(3, sizeof(char)); mult_str(num1, num2); return 0; } 职能: char *mult_str(char *num1, char *num2)/

我写了一个函数,本来应该这样做的,但它出了问题,我不知道是什么

主文件:

int main()
{
    char *num1, *num2;
    num1=(char *) calloc(3, sizeof(char));
    num2=(char *) calloc(3, sizeof(char));
    mult_str(num1, num2);

    return 0;
}
职能:

    char *mult_str(char *num1, char *num2)//multiplies numbers using strings
    {
        int i, j, temp, fix1=1, fix2;
        char *result, *mult=(char *) calloc(strlen(num1)*2+1, sizeof(char));

        gets(num1);
        gets(num2);

        if(strlen(num2)>strlen(num1))//makes the longer one num1
            swapStr(&num1, &num2);

        result=(char *) calloc(strlen(num1)*2+1, sizeof(char));
        strcpy(result, "0");

        for(i=strlen(num2)-1 ; i>=0 ; i--)
        {
            fix2=fix1;
            for(j=strlen(num1)-1 ; j>=0 ; j--)
            {
                temp=((num2[i]-'0')*(num1[j]-'0'))*fix2;
                itoa(temp, mult);
                result=add_str(result, mult);
                fix2*=10;
            }
            fix1*=10;
        }

return result
}

    char *add_str(char *num1, char *num2)//add positive numbers using strings(for big ones)
    {
        int i, s, size;
        char *sum;

        if(strlen(num2)>strlen(num1))//makes the longer one num1
            swapStr(&num1, &num2);

        size=strlen(num1)+2;
        sum=(char *) realloc(num1, size*sizeof(char));//uses num1 for the sum string
        for(i=size-2 ; i>=0 ; i--)
            sum[i+1]=sum[i];
        sum[0]='0';

        s=strlen(sum)-1;//index for sum
        for(i=strlen(num2)-1 ; i>=0 ; i--)//adds the numbers
        {
            if(sum[s]+num2[i]-2*'0'>9)//in case the sum of two numbers in bigger than 9:
            {
                sum[s-1]=sum[s-1]+(sum[s]+num2[i]-2*'0')/10;
                sum[s]=(sum[s]+num2[i]-2*'0')%10+'0';
            }
            else//in case it's not
                sum[s]=sum[s]-'0'+num2[i]-'0'+'0';
            s--;
        }

        while(sum[0]=='0')
        {
            for(i=0 ; i<size ;i++)
                sum[i]=sum[i+1];
        }

        return sum;
    }

    /* itoa:  convert n to characters in s */
    void itoa(int n, char *s)
    {
        int i, sign;

        if ((sign = n) < 0)  /* record sign */
        {
            n = -n;          /* make n positive */
        }
        i = 0;
        do {       /* generate digits in reverse order */
            s[i++] = n % 10 + '0';   /* get next digit */
        } while ((n /= 10) > 0);     /* delete it */
        if (sign < 0)
            s[i++] = '-';
        s[i] = '\0';
        reverse(s);
    }

    /* reverse:  reverse string s in place */
    void reverse(char *s)
    {
        int i, j;
        char c;

        for (i = 0, j = strlen(s)-1; i<j; i++, j--)
        {
            c = s[i];
            s[i] = s[j];
            s[j] = c;
        }
    }
这和你想象的不一样。就是这样,

i = 1;
i = 2;
第二行丢弃第一行的结果,这意味着
result
指向一个静态字符串,而不是新分配的空间。也许您希望第二行是
strcpy(结果为“0”)
也许不是,这很难说,因为您的代码没有注释,所以无法知道您希望它做什么

如果仔细查看逻辑,您会发现
fix
并不总是具有正确的值。当外循环重复时,
fix
需要是上一次迭代的10倍,只有当内循环正好有一次迭代时才会如此。(您可能应该使用两个
fix
变量,一个用于内部循环,一个用于外部循环。)

像这样:

fix1=1;
for(i=strlen(num2)-1 ; i>=0 ; i--)
{
    fix2=fix1;
    for(j=strlen(num1)-1 ; j>=0 ; j--)
    {
        temp=((num2[i]-'0')*(num1[j]-'0'))*fix2;
        itoa(temp, mult);
        result=add_str(result, mult);//add_str tested and works
        fix2*=10;
    }
    fix1*=10;
}

我肯定你还有其他的bug,但是你没有给我们足够的代码告诉我们。

不要使用strlen
那样,计算一次并存储它的值。给我们足够的代码来重现问题。首先显示调用函数:使用所有警告和调试信息编译(
gcc-Wall-Wextra-g
)。然后学习如何使用与您的问题无关的调试器(
gdb
),但是
mult
在没有
free(mult)的情况下超出范围。不客气。此外,
fix
是一个
int
。我不知道你的平台,但即使在64位平台上,它也会溢出18位。我这样说是因为这看起来像是任意精度算术的课程作业,但你使用
temp
fix
意味着你仍然没有逃脱原语类型的限制。@DanAllen我同意。
fix
变量也应该是字符串。或“移位”寄存器。他可以将
temp
放入
mult
中的正确位置(将上一次迭代的低位数字归零)。尽管如此,他仍然没有完全实现Knuth所描述的。@DanAllen我同意你关于固定变量的观点,但是数字字符串的乘法仍然是我的难题。另一方面,temp变量永远不会超过两位数,所以它不是必需的。@YogevBoaron好吧,乘以10特别容易。;)
i = 1;
i = 2;
fix1=1;
for(i=strlen(num2)-1 ; i>=0 ; i--)
{
    fix2=fix1;
    for(j=strlen(num1)-1 ; j>=0 ; j--)
    {
        temp=((num2[i]-'0')*(num1[j]-'0'))*fix2;
        itoa(temp, mult);
        result=add_str(result, mult);//add_str tested and works
        fix2*=10;
    }
    fix1*=10;
}