代码如下:#pragmaonce#include<iostream>#include<assert.h>#include<string>usingnamespacestd;#defineUN_INIT0XCCCCCCCCCCCCCCCC;#defineMAX0x7FFFFFFFFFFFFFFF#defineMIN0X8000000000000000typedeflonglongINT64;classBigData{public:BigData(INT64value=0xCCCCCCCCCCCCCCCC):_value(value){ToString();};BigData(constchar*p);BigDataoperator+(BigData&bigdata);BigDataoperator-(BigData&bigdata);BigDataoperator*(BigData&bigdata);BigDataoperator/(BigData&bigdata);friendostream&operator<<(ostream&_cout,constBigData&bigdata);private:boolIsOver64()const{stringtmp("+9223372036854775807");if(_StrData[0]=='-'){tmp="-9223372036854775808";}if(_StrData.size()<tmp.size())returntrue;elseif(_StrData.size()==tmp.size()&&_StrData<=tmp)returntrue;returnfalse;}stringAdd(stringleft,stringright);stringSub(stringleft,stringright);stringMul(stringleft,stringright);stringDiv(stringleft,stringright);private:boolIsLeftBig(constchar*left,intlSize,constchar*right,intrSize);charSubLoop(char*left,intlSize,constchar*right,intrSize);voidToString();INT64_value;string_StrData;};BigData::BigData(constchar*pData){//1212312asdasd1221312qwe0000qweqweif(pData==NULL)return;char*p=(char*)pData;char_Symble=pData[0];if(_Symble=='+'||_Symble=='-'){p++;}elseif(_Symble>='0'&&_Symble<='9'){_Symble='+';}else_Symble='-';while(*p=='0'){p++;}_StrData.resize(strlen(pData)+1);_StrData[0]=_Symble;intcount=1;_value=0;while(*p>='0'&&*p<='9'){_value=_value*10+(*p-'0');_StrData[count++]=*p;p++;}if(_Symble=='-')_value=0-_value;_StrData.resize(count);};ostream&operator<<(ostream&_cout,constBigData&bigdata){//是否溢出判断//if(bigdata.IsOver64()){_cout<<bigdata._value<<endl;}else_cout<<bigdata._StrData<<endl;return_cout;}voidBigData::ToString(){INT64temp=_value;//1111111111111111if(_value>0)_StrData.append(1,'+');else{_StrData.append(1,'-');}if(_value<0)temp=0-_value;while(temp){charcStr=temp%10+'0';_StrData.append(1,cStr);temp/=10;}intleft=1;intright=_StrData.size()-1;while(left<right){swap(_StrData[left],_StrData[right]);++left;--right;}}boolBigData::IsLeftBig(constchar*left,intlSize,constchar*right,intrSize){if(lSize>rSize||lSize==rSize&&strcmp(left,right)>=0){returntrue;}returnfalse;}charBigData::SubLoop(char*left,intlSize,constchar*right,intrSize){charCret='0';while(true){if(!IsLeftBig(left,lSize,right,rSize))//如果左边小{break;}else//循环相减法{intlsize=lSize-1;intrsize=rSize-1;while(lsize>=0&&rsize>=0){charret=left[lsize]-'0';ret-=right[rsize]-'0';if(ret<0){left[lsize-1]-=1;ret+=10;}left[lsize]=ret+'0';lsize--;rsize--;}while(*left=='0'&&lSize>0){left++;lSize--;}Cret++;}}returnCret;}BigDataBigData::operator+(BigData&bigdata){if(IsOver64()&&bigdata.IsOver64())//都不溢出{if(_StrData[0]!=bigdata._StrData[0]){returnBigData(_value+bigdata._value);}INT64tmp=MIN-_value;if((_value>0&&(MAX-_value>=bigdata._value))||(_value<0&&(tmp<=bigdata._value))){returnBigData(_value+bigdata._value);}}//至少有一个溢出//结果溢出if(_StrData[0]==bigdata._StrData[0])returnBigData(Add(_StrData,bigdata._StrData).c_str());elsereturnBigData(Sub(_StrData,bigdata._StrData).c_str());//符号位相反取反。}stringBigData::Add(stringleft,stringright)//{intiLeft=left.size();intiRight=right.size();stringret;ret.append(1,left[0]);if(left[0]!=right[0]){if(left[0]=='-')ret[0]='+';if(right[0]=='+')ret[0]='-';}if(iLeft<iRight){swap(left,right);swap(iLeft,iRight);}ret.resize(iLeft+1);charstep=0;for(intindex=1;index<iLeft;++index){charCstr=left[iLeft-index]-'0'+step;if(index<iRight){Cstr+=(right[iRight-index]-'0');}ret[iLeft-index+1]=Cstr%10+'0';step=Cstr/10;}ret[1]=step+'0';returnret;}BigDataBigData::operator-(BigData&bigdata){if(IsOver64()&&bigdata.IsOver64())//两个不溢出{if(_StrData[0]==bigdata._StrData[0]){returnBigData(_value-bigdata._value);}else{//10-111-111-12if((_value>0||(_value<0)&&MIN+bigdata._value<=_value))returnBigData(_value-bigdata._value);}}if(_StrData[0]!=bigdata._StrData[0]){//bigdata._StrData[0]=_StrData[0];returnBigData(Add(_StrData,bigdata._StrData).c_str());}else{returnBigData(Sub(_StrData,bigdata._StrData).c_str());//符号位相同}}stringBigData::Sub(stringleft,stringright){//if(left[0]==right[])intiLeft=left.size();intiRight=right.size();stringret;charsymble=left[0];if(iLeft<iRight||(iLeft==iRight&&left<right))//左边的xiao{swap(left,right);swap(iLeft,iRight);if(symble=='+')symble='-';else{symble='+';}}ret.resize(iLeft);ret[0]=symble;charstep=0;for(intindex=1;index<iLeft;++index){charCstr=left[iLeft-index]-'0';if(index<iRight){Cstr-=(right[iRight-index]-'0');}if(Cstr<0){left[iLeft-index-1]-=1;Cstr+=10;}ret[iLeft-index]=Cstr+'0';}returnret;}BigDataBigData::operator*(BigData&bigdata){//if(IsOver64()&&bigdata.IsOver64()){if(_StrData[0]==bigdata._StrData[0])//-10-25{if((_value>0&&MAX/_value>=bigdata._value)||(_value<0&&(MAX/_value<=bigdata._value)))returnBigData(_value*bigdata._value);}else{if((_value>0&&MIN/_value>=bigdata._value)||(_value<0&&(MIN/_value>=bigdata._value)))returnBigData(_value*bigdata._value);}}returnBigData(Mul(_StrData,bigdata._StrData).c_str());}stringBigData::Mul(stringleft,stringright){charsymbol='+';if(left[0]!=right[0])symbol='-';intiLeft=left.size();intiRight=right.size();if(iLeft>iRight){swap(left,right);swap(iLeft,iRight);}stringret;//ret.resize(iLeft+iRight-1);ret.assign(iLeft+iRight-1,'0');ret[0]=symbol;intioffset=0;intrlen=ret.size();for(intindex=1;index<iLeft;++index){charCleft=left[iLeft-index]-'0';charstep=0;if(Cleft=='0'){++ioffset;continue;}for(intindex=1;index<iRight;++index){charCstr=Cleft*(right[iRight-index]-'0')+step;//取到字符Cstr+=(ret[rlen-index-ioffset]-'0');//累加ret[rlen-index-ioffset]=(((Cstr)%10)+'0');step=Cstr/10;}ret[rlen-iRight-ioffset]+=step;ioffset++;}returnret;}BigDataBigData::operator/(BigData&bigdata){assert(bigdata._value!=0);if(IsOver64()&&bigdata.IsOver64())//不溢出{returnBigData(_value/bigdata._value);}if(_StrData.size()<bigdata._StrData.size()||(_StrData.size()==bigdata._StrData.size()&&strcmp(_StrData.c_str()+1,bigdata._StrData.c_str()+1)<0)){returnBigData(INT64(0));}if(bigdata._StrData=="+1"||bigdata._StrData=="-1"){if(_StrData[0]!=bigdata._StrData[0]){_StrData[0]='-';}else_StrData[0]='+';return*this;}if(strcmp(_StrData.c_str()+1,bigdata._StrData.c_str()+1)==0){if(_StrData[0]!=bigdata._StrData[0]){returnBigData(INT64(1));}elsereturnBigData(INT64(-1));}returnBigData(Div(_StrData,bigdata._StrData).c_str());}stringBigData::Div(stringleft,stringright){stringret;ret.append(1,'+');if(left[0]!=right[0]){ret[0]='-';}intLSize=left.size()-1;intRsize=right.size()-1;char*Left=(char*)left.c_str()+1;char*Right=(char*)right.c_str()+1;intdatalen=Rsize;for(intindex=0;index<LSize;){if(!IsLeftBig(Left,datalen,Right,Rsize))//左边小于右边{ret.append(1,'0');datalen++;if(datalen+index>LSize)break;}else{ret.append(1,SubLoop(Left,datalen,Right,Rsize));while(*Left=='0'&&datalen>0){Left++;index++;datalen--;}datalen++;if(index+datalen>LSize)break;}}returnret;}测试用例如下:oidtestAdd(){BigDatab1("-45353");BigDatab2("37353753");BigDatab3("-9223372036854775808");BigDatab4("9223372036854775800");BigDatab5("-9223372036854775810");BigDatab6("9223372036854775900");//1、都在INT64范围内<运算后在范围内,运算后不在范围内>(再进行构造可以解决)cout<<(b1+b1)<<endl;cout<<(b2+b2)<<endl;cout<<(b1+b2)<<endl;cout<<(b1+b4)<<endl;cout<<b3<<endl;cout<<(b1+b3)<<endl;cout<<(b2+b4)<<endl;//2、都不在INT64范围内<运算后在范围内,运算后不在范围内>cout<<(b2+b5)<<endl;cout<<(b1+b6)<<endl;cout<<(b6+b1)<<endl;//3、一个在一个不在<运算后在范围内,运算后不在范围内>cout<<endl;cout<<endl;cout<<endl;cout<<endl;}voidtestSub(){BigDatab1("-45353");BigDatab2("37353753");BigDatab3("-9223372036854775808");BigDatab4("9223372036854775800");BigDatab5("-9223372036854775810");BigDatab6("9223372036854775900");//1、都在INT64范围内<运算后在范围内,运算后不在范围内>(再进行构造可以解决)cout<<(b1-b2)<<endl;cout<<(b2-b1)<<endl;cout<<(b3-b1)<<endl;cout<<(b1-b4)<<endl;cout<<(b3-b2)<<endl;cout<<(b4-b1)<<endl;cout<<(b1-b3)<<endl;cout<<(b2-b4)<<endl;cout<<endl;//2、一个在一个不在<运算后在范围内,运算后不在范围内>cout<<(b5-b1)<<endl;cout<<(b1-b5)<<endl;cout<<endl;cout<<(b6-b2)<<endl;cout<<(b2-b6)<<endl;cout<<endl;cout<<(b6-b5)<<endl;cout<<(b5-b6)<<endl;cout<<(b2-b5)<<endl;cout<<(b1-b6)<<endl;cout<<(b6-b1)<<endl;}voidtest(){BigDatac1("999");BigDatac2("222222222222222222222222222");cout<<c1*c2<<endl;}voidtestMul(){BigDatab1("-45353");BigDatab2("37353753");BigDatab3("-9223372036854775808");BigDatab4("9223372036854775800");BigDatab5("-9223372036854775810");BigDatab6("9223372036854775900");//1、都在INT64范围内<运算后在范围内,运算后不在范围内>(再进行构造可以解决)cout<<(BigData("999")*BigData("22222222222222222222222222222"))<<endl;cout<<(b2*b1)<<endl;cout<<(b1*b2)<<endl;cout<<(b1*BigData("0"))<<endl;cout<<(BigData("0")*b2)<<endl;cout<<endl;cout<<(b3*b1)<<endl;cout<<(b1*b3)<<endl;cout<<(b1*b4)<<endl;//错误cout<<(b4*b1)<<endl;//正确cout<<(b3*b2)<<endl;//错误cout<<(b2*b4)<<endl;//正确cout<<endl;//2、一个在一个不在<运算后在范围内,运算后不在范围内>cout<<(BigData("0")*b6)<<endl;cout<<(b5*BigData("0"))<<endl;cout<<(b5*b1)<<endl;cout<<(b1*b5)<<endl;cout<<endl;cout<<(b6*b2)<<endl;cout<<(b2*b6)<<endl;cout<<endl;cout<<(b6*b5)<<endl;cout<<(b5*b6)<<endl;cout<<(b2*b5)<<endl;cout<<endl;cout<<(b1*b6)<<endl;cout<<(b6*b1)<<endl;}voidtestDiv(){BigDatab1("-45353");BigDatab2("37353753");BigDatab3("-9223372036854775808");BigDatab4("9223372036854775800");BigDatab5("-9223372036854775810");BigDatab6("9223372036854775900");//1、排除除数为0//cout<<(b1/BigData(0))<<endl;//2、在范围内cout<<(b1/b2)<<endl;cout<<(b2/b1)<<endl;//3、不在范围内<左(被除数)比右(除数)小为0,左比右大>cout<<(b2/b5)<<endl;cout<<(b2/b6)<<endl;cout<<(b5/b2)<<endl;cout<<(b6/b2)<<endl;cout<<(b6/b1)<<endl;cout<<(b5/b1)<<endl;BigDatab7("-1231123203367738338252");cout<<b7/b1<<endl;}intmain(){//testMul();//testAdd();//testSub();//testMul();//test();//BigData(10);testDiv();//BigDatab1("2222222222222222222222222");//BigDatab2("33");//cout<<b1/b2<<endl;return0;}