c++ 大数相加

int main()
{
    char* data1 =   "1234567891234567";
    char* data2 = "234567899876543221";
    char result[100];
    int value[1000] = { 0 };

    int temp;
    int iData1;
    int iData2;

    int i = strlen(data1) - 1;
    int j = strlen(data2) - 1;
    int len = i > j ? (i + 1) : (j + 1);

    int count = 0;
    do 
    {
        if ((data1[i] >= '0') && (data1[i] <= '9'))
        {
            iData1 = data1[i] - '0';
        }
        else
        {
            iData1 = 0;
        }

        if ((data2[j] >= '0') && (data2[j] <= '9'))
        {
            iData2 = data2[j] - '0';
        }
        else
        {
            iData2 = 0;
        }

        temp = iData1 + iData2 + value[count];
        if (temp >= 10)
        {
            value[count] = temp - 10;
            value[count + 1] = 1;
        }
        else
        {
            value[count] = temp;
        }

        count++;
        i--;
        j--;
    } while ((i > -1)||(j > -1));


    for (int i = 0; i < 50; i++)
    {
        result[len - i - 1] = value[i] + '0';
    }
    result[len] = '\0';

    printf("%s\n", result);
    getchar();

    return 0;
}

得到结果: 235802467767777788

你可能感兴趣的:(c++ 大数相加)