将大整数string化为bigint的数组存储,再进行运算。代码很简单易懂如下。注意:carry进制的做法有点抽象
#include <iostream>
#include<string>
using namespace std;
struct bigInt
{
int len;
int d[1000];
} ;
bigInt change( string s)
{
bigInt a;
int num = 0;
for (int i = 0; i < 1000; i++)
a.d[i] =0 ;
int len = s.size();
a.len = len;
for (int i =0;i<len;i++)
{
a.d[i] = s[len - 1 - i] - '0';//倒序放置,方便低位对齐运算
}
return a;
}
//大整数加法
bigInt add(bigInt a, bigInt b)
{
bigInt c;
int carry = 0;//表进位的数字
int len = 0;
c.len = len;
for (int i = 0; i < a.len || i < b.len; i++)
{
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len] = temp % 10;
c.len++;
carry= temp / 10;
}
if (carry != 0)
{
c.d[c.len] = carry;
c.len++;
}
return c;
}
bigInt sub(bigInt a, bigInt b)
{
bigInt c;
c.len = 0;
for (int i = 0; i <