BigData.h:
#ifndef __BIG_DATA_H__
#define __BIG_DATA_H__
#include <assert.h>
#include <string>
typedef long long INT64;
//#define MININT64 -9223372036854775808 // 编译器检查有错
//
//#define MAXINT64 +9223372036854775807
const INT64 MININT64 = +9223372036854775807 + 1;
const INT64 MAXINT64 = +9223372036854775807;
class BigData
{
friend std::ostream& operator<<(std::ostream& _cout, const BigData& bigData);
public:
BigData(INT64 value = 0xCCCCCCCCCCCCCCCC);
BigData(const char* str);
BigData operator+(const BigData& bigData);
BigData operator-(const BigData& bigData);
BigData operator*(const BigData& bigdata);
BigData operator/(const BigData& bigdata);
protected:
void _Int64ToStr();
bool _IsINT64OverFlow()const;
std::string AddStr(const BigData& bigData)const;
std::string SubStr(const BigData& bigData)const;
std::string MulStr(const BigData& bigData)const;
std::string DivStr(const BigData& bigData)const;
bool IsLeftStrBig(const char* pLeft, int iLSize, const char* pRight, int iRSize)const;
char LoopSub(char* pLeft, int iLSize, const char* pRight, int iRSize)const;
private:
INT64 _value;
std::string _pData;
};
std::ostream& operator<<(std::ostream& _cout, const BigData& bigData);
#endif /*__BIG_DATA_H__*/
BigData.cpp:
#define _CRT_SECURE_NO_WARNINGS 1
#include "BigData.h"
BigData::BigData(INT64 value) :_value(value)
{
this->_Int64ToStr();
}
BigData::BigData(const char* str) : _value(0)
{
assert(str);
char* temp = (char*)str;
char cSign = '+';
while (isspace(*temp)) //*temp == ' '不能消除tab
{
++temp;
}
if ((*temp == '-') || (*temp == '+'))
{
cSign = *temp;
++temp;
}
this->_pData.resize(strlen(str) + 1);
this->_pData[0] = cSign;
int iCount = 1;
while (*temp == '0')//处理000123
{
++temp;
}
while ((*temp >= '0') && (*temp <= '9'))
{
this->_value = this->_value * 10 + (*temp - '0');
this->_pData[iCount++] = *temp++;
}
this->_pData.resize(iCount);
if (cSign == '-')
{
this->_value = 0 - this->_value;
}
if (!this->_IsINT64OverFlow())//溢出
{
this->_value = 0xCCCCCCCCCCCCCCCC;
}
}/*BigData(const char* str)*/
BigData BigData::operator+(const BigData& bigData)
{
if (this->_IsINT64OverFlow() && bigData._IsINT64OverFlow()) //两个均没有溢出
{
if (this->_pData[0] != bigData._pData[0])//异号,相加肯定没有溢