#include <iostream>
#include <cstring>
using namespace std;
//声明超大数类
class BigNum
{
private:
char *pszNnum;
int length;
public :
BigNum();
BigNum(const char *);
BigNum(const BigNum & bigNum);
virtual ~BigNum();
int Display();
BigNum operator + (BigNum);
BigNum & operator = (BigNum);
};
BigNum & BigNum::operator = (BigNum bigNum)
{
this->length = bigNum.length;
delete this->pszNnum;
this->pszNnum = new char[this->length+1];
for(int i = 0; i< this->length; i++)
{
this->pszNnum[i] = bigNum.pszNnum[i];
}
this->pszNnum[length]='\0';
return *this;
}
//默认构造函数
BigNum::BigNum()
{
this->length = 0;
this->pszNnum = NULL;
}
//构造函数初始化
BigNum::BigNum(const char *bigNum)
{
this->length = strlen(bigNum);
pszNnum = new char[length+1];
for(int i=0; i<length; i++)
{
this->pszNnum[i] = bigNum[length-1-i];
}
this->pszNnum[length] = '\0';
}
//重写复制构造函数
BigNum::BigNum(const BigNum & bigNum)
{
this->length = bigNum.length;
this->pszNnum = new char[this->length+1];
for(int i = 0;i < this->length; i++)
{
this->pszNnum[i] = bigNum.pszNnum[i];
}
this->pszNnum[length] = '\0';
}
//打印超大数
int BigNum::Display()
{
for(int i = length-1;i >= 0 ;i--)
{
cout << this->pszNnum[i] ;
}
cout << endl;
return this->length;
}
//重载运算符,两个超大数相加
BigNum BigNum::operator + (BigNum bigNum)
{
char * pszTem;
int nSize = max(this->length, bigNum.length);
int minLength = min(this->length, bigNum.length); //取最小数组大小,用于for循环相加
pszTem = new char[nSize+2];
int carry = 0;
int i;
for(i = 0;i < minLength; i++)
{
carry = this->pszNnum[i] - '0' + bigNum.pszNnum[i] - '0' + carry;
pszTem[i] = carry % 10 + '0';
carry /= 10;
}
for (; i < this->length; i++ )
{
carry = this->pszNnum[i] - '0' + carry;
pszTem[i] = carry % 10 + '0';
carry /= 10;
}
for (; i < bigNum.length; i++ )
{
carry = bigNum.pszNnum[i] - '0' + carry;
pszTem[i] = carry%10 + '0';
carry /= 10;
}
if (carry != 0) //判断是否最后进位的情况
{
pszTem[i] = carry + '0';
pszTem[i+1] = '\0';
this->length = nSize + 1;
}
else
{
pszTem[i] = '\0';
this->length = nSize ;
}
delete this->pszNnum;
this->pszNnum = pszTem;
return *this;
}
BigNum::~BigNum()
{
}
int main()
{
BigNum bigNum_1("99999999999999999999999999999999999999999999999999999999999999999999888888888888");
bigNum_1.Display();
BigNum bigNum_2("1999999999999999999999999999999999999999999999999999999999997777777777777777777");
bigNum_2.Display();
bigNum_1=bigNum_2+bigNum_1;
bigNum_1.Display();
int wait;
cin >> wait;
return 0;
}