C++代码
#include<iostream>
#include<string>
#include<cstdlib>
class BigInt {
public:
BigInt(int data = 0)
{
m_data[0] = 0;
if (data == 0) {
m_len = 1;
return;
}
int idx = 0;
while (data > 0) {
m_data[idx++] = data % 10;
data /= 10;
}
m_len = idx;
}
BigInt operator=(BigInt b)
{
m_len = b.m_len;
for (int i=0; i<m_len; ++i) {
m_data[i] = b.m_data[i];
}
}
void scan()
{
std::string strInt;
std::cin >> strInt;
m_len = strInt.size();
for(int i=0; i<m_len; ++i) {
m_data[i] = strInt[m_len - i - 1] - '0';
}
}
void print()
{
for (int i=m_len-1; i>=0; --i) {
std::cout << m_data[i];
}
}
void push_back(int num)
{
if(m_len == 1 && m_data[0] == 0) {
m_data[0] = num;
return;
}
for(int i=m_len-1; i>=0; --i) {
m_data[i+1] = m_data[i];
}
m_data[0] = num;
++m_len;
}
BigInt operator+=(const BigInt& b)
{
m_len = std::max(m_len, b.m_len);
for (int i=0; i<b.m_len; ++i) {
m_data[i] += b.m_data[i];
m_data[i+1] += m_data[i] / 10;
m_data[i] %= 10;
}
if (m_data[m_len] != 0) {
++m_len;
}
return *this;
}
private:
static const int MAX_LEN = 1e5 + 10;
int m_data[MAX_LEN];
int m_len;
};
BigInt operator+(BigInt a, BigInt b)
{
BigInt c(a);
c += b;
return c;
}
int main()
{
BigInt a;
BigInt b;
BigInt c;
a.scan();
b.scan();
c = a + b;
c.print();
return 0;
}