首页 > 代码库 > 练习2014082701

练习2014082701

/********************************************************************* @file     Main_practise.cpp* @date     2014-8-27* @author   Tiger* @brief    练习   * @details  大数加法********************************************************************/#include <iostream>#include <cstring>const int SIZE = 100;int Add(const char* a, const char* b, char* sum);int main(int argc, const char* argv[]){    char* a = "123";    char* b = "89";    char sum[SIZE]= {0};    int nPos = Add(a, b, sum);    for (int i=nPos; i<SIZE; ++i)    {        std::cout << sum[i];    }    std::cout << std::endl;    system("pause");    return 0;}int Add(const char* a, const char* b, char* sum){    int i = 0, j = 0, k = 0;    for (i=strlen(a)-1, j=SIZE-1; i>=0; --i, --j)    {        sum[j] = a[i] - 0;    }    for (i=strlen(b)-1, k=SIZE-1; i>=0; --i, --k)    {        sum[k] += b[i] - 0;    }    int nMin = (j < k ? j : k);    for (int i=SIZE-1; i>nMin; --i)    {        sum[i-1] += sum[i]/10;        sum[i] = sum[i]%10 + 0;    }    if (sum[nMin-1] != 0 && sum[nMin-1] != 0)    {        return nMin;    }    else    {        return nMin+1;    }}

 

练习2014082701