将数字乘以C中的字符串

时间:2015-01-05 09:43:42

标签: c string multiplication multiplying

我写了一个应该这样做的函数,但是错误的东西,我不知道是什么。

主文件:

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

应该发生什么的例子: 25 * 4 =(4 * 5 * 1)+(0)+(4 * 2 * 10)+(20)= 100

实际发生的事情:

 yogev@yogev-laptop:~/question2$ ./question2//multiplying two numbers
 25
 4
 260//result

1 个答案:

答案 0 :(得分:2)

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

这并不是你认为它做的。它就像这样:

i = 1;
i = 2;

第二行抛弃第一行的结果,这意味着result将指向静态字符串而不是新分配的空格。也许你想要第二行是 strcpy(result, "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;
}

我确定您还有其他错误,但您还没有给我们足够的代码来说明。