急!求C语言高精度加法计算的程序
1个回答

#include

#include

using namespace std;

void swap(string plus1, string plus2)/*交换两数*/

{

x05string t;

x05t = plus1;

x05plus1 = plus2;

x05plus2 = t;

}

void BigPlus(string plus1, string plus2)/*大数加法*/

{

x05int i,j,num,len1,len2;

x05len1 = plus1.size()-1;

x05len2 = plus2.size()-1;

x05int *strplus = new int[len1+2];/*动态开辟空间*/

x05for (i = 0; i <= len1+1; ++i)

x05x05strplus[i] = 0;/*初始化为0*/

x05for (i = len1, j = len2; i >= 0; --i,--j)/*plus1位数不小于plus2位数*/

x05{

x05x05if (j >= 0)

x05x05x05num = (plus1[i]-'0') + (plus2[j]-'0'); /*和*/

x05x05else

x05x05x05num = (plus1[i]-'0');/*和*/

x05x05if (num >= 10)

x05x05{

x05x05x05strplus[i+1] += (num-10); /*本位*/

x05x05x05strplus[i] = 1;/*高位:进1*/

x05x05}

x05x05else

x05x05{

x05x05x05strplus[i+1] += num;/*本位*/

x05x05}

x05}

x05i = strplus[0] == 0 ? 1 : 0;/*舍去高位无用0*/

x05for ( ; i <= len1+1; ++i)/*打印结果*/

x05x05cout<

x05cout<

x05delete[] strplus;

x05strplus = NULL;

}

int main()

{

x05string plus1,plus2;

x05

x05cin>>plus1>>plus2;

x05if (plus1.size() < plus2.size())/*使plus1位数不小于plus2*/

x05x05swap(plus1,plus2);

x05BigPlus(plus1,plus2);/*大数加法*/

x05return 0;

}

快速 超级大数加法,支持无上限大数加法~

思想:由于int型最大为10亿左右,当超过这个位数的时候,将会给计算带来问题,这时候就需要大数计算.

这里讲数字按位储存在数组里面进行操作~

然后对数组进行按位加,需要注意的就是进位操作.