算法导论@大数相乘

题目要求:两个较大的数相乘。以字符串输入两个较大的数,结果保存在字符串中。

注意:
字符串转整数 1-‘0’

处理方式一:

#include"cheng.h"
#define MAX 100
//大数相乘
int main()
{
    char *a = (char*)malloc(sizeof(char)), *b = (char*)malloc(sizeof(char));
    char *res = (char*)malloc(sizeof(char));
    char *m = (char*)malloc(sizeof(char)), *n = (char*)malloc(sizeof(char));
    int carry = 0;
    //处理输入
    cout << "输入两个大数:" << endl;
    cin >> a;
    cin >> b;

    //翻转字符串
    for (int i = 0; i < strlen(a); i++)
    {
        m[i] = a[strlen(a) - i - 1];
    }
    m[strlen(a)] = NULL;

    for (int i = 0; i < strlen(b); i++)
    {
        n[i] = b[strlen(b) - i - 1];
    }
    n[strlen(b)] = NULL;

    //存放结果的字符串清零
    for (int i = 0; i < strlen(a) + strlen(b); i++)
    {
        res[i] = 0;
    }

    //保存结果
    for (int i = 0; i < strlen(a) ; i++)
    {
        for (int j = 0;  j < strlen(b); j++)
        {
            res[i + j] += (m[i] - '0')*(n[j] - '0');
        }
    }

    //进位,移位处理
    for (int i = 0; i < strlen(a)+strlen(b); i++)
    {
        char temp = res[i];
        res[i] = (res[i]+carry)% 10;
        carry = (temp + carry) / 10;

    }

    //若最高位为0,则从次高位开始输出
    if (res[strlen(a) + strlen(b) - 1] == 0)
    {
        for (int i = strlen(a) + strlen(b) - 2; i >= 0; i--)
        {
            printf("%d",res[i]);
        }
    }
    else
    {
        for (int i = strlen(a) + strlen(b) - 1; i >= 0; i--)
        {
            printf("%d", res[i]);
        }

    }

    system("pause");
    return 0;
}

处理方式二:

#include"cheng.h"
#define MAX 100
//大数相乘
int main()
{
    //字符串指针a,b用于接受输入的两个大数
    char *a = (char*)malloc(sizeof(char));
    char *b = (char*)malloc(sizeof(char));

    //字符串指针res用于保存最后的结果
    char *res = (char*)malloc(sizeof(char));

    //进位标志
    int carry = 0;

    //m,n数组用来保存a,b转换为整数后的值
    int m[MAX], n[MAX];

    //输入:
    cout << "请输入两个大数:" << endl;
    cin >> a;
    cin >> b;

    //将字符串逆序排列,并转换为整数
    for (int i = 0; i < strlen(a); i++)
    {
        m[i] = a[strlen(a)-i-1]-'0';
        //指针形式访问
        //m[i]=*(a+strlen(a)-i-1)-'0';
    }

    for (int i = 0; i < strlen(b); i++)
    {
        n[i] = b[strlen(b) - i - 1]-'0';
        //指针形式访问
        //n[i]=*(b+strlen(b)-i-1)-'0';
    }

    //保存结果的整型数组result
    int result[MAX] = { 0 };

    for (int i = 0; i < strlen(a); i++)
    {
        for (int j = 0; j < strlen(b); j++)
        {
            result[i + j] += m[i] * n[j];
        }
    }

    //进位,移位处理
    int temp = 0;
    for (int i = 0; i < strlen(a) + strlen(b); i++)
    {
        temp = result[i];
        result[i] = (result[i] + carry) % 10;
        carry = (temp + carry) / 10;
    }
    /*************** if (result[strlen(a) + strlen(b) - 1] == 0) { for (int i = strlen(a) + strlen(b) - 2; i >= 0; i--) { cout << result[i] ; } } else { for (int i = strlen(a) + strlen(b) - 1; i >= 0; i--) { cout << result[i]; } } ***************/


    //将结果数组转换为字符串
    //注意判断,最高位是0,则从次高位输出
    if (result[strlen(a) + strlen(b)-1] == 0)
    {
        int index = 0;
        for (int i = strlen(a) + strlen(b)-2; i >=0; i--)
        {
            res[index++] = result[i] + '0';
        }
        res[index] = NULL;
    }
    else
    {
        int index = 0;
        for (int i = strlen(a) + strlen(b) - 1; i >= 0; i--)
        {
            res[index++] = result[i] + '0';
        }
        res[index] = NULL;
    }
    printf("%s", res);

    system("pause");
    return 0;
}

你可能感兴趣的:(算法导论)