大数乘法?

#define MAX_INPUT_SIZE 10000
#define MAX_RESULT_SIZE (MAX_INPUT_SIZE * 2 - 1)
char a[MAX_INPUT_SIZE],b[MAX_INPUT_SIZE],res[MAX_RESULT_SIZE];
void mult(char result[], char a[], char b[])//res一定要清空
{
    int al, bl;    // a length, b length
    int ri, ai, bi;    // r index, a index, b index
    char tmp, carry;
    al = strlen(a);
    bl = strlen(b);

    carry = 0;
    for (bi = bl-1; bi >= 0; bi--)
    {
        for (ai = al-1; ai >= 0; ai--)
        {
            ri = (bl-1-bi) + (al-1-ai);
            tmp = (a[ai]-'0') * (b[bi]-'0') + result[ri] + carry;
            result[ri] = tmp % 10;
            carry = tmp / 10;
        }
        while (carry)
        {
            ri++;
            tmp = result[ri] + carry;
            result[ri] = tmp % 10;
            carry = tmp / 10;
        }
    }
    int i = MAX_RESULT_SIZE - 1;
    while (!result[i]) i--;
    while (i >= 0) result[i--] += '0';
}
void print(char result[])
{
    int i = MAX_RESULT_SIZE - 1;
    while (!result[i]) i--;
    while (i >= 0) putchar(result[i--] + '0');
    putchar('\n');
}

 

posted on 2016-09-22 11:54  very_czy  阅读(145)  评论(0编辑  收藏  举报

导航