# include & lt列表& gt
# include & lt字符串& gt
# include & ltiomanip & gt
使用命名空間std
BigIntAdd類
{
公共:
朋友團隊& amp操作員& gt& gt(istream & amp在,BigIntAdd & amp號);
牡蠣之友& amp操作員& lt& lt(ostream & ampout,const BigIntAdd & amp號);
friend BigIntAdd運算符+(BigIntAdd & amp;加數1。addend 2);
私人:
//聲明壹個表對象來存儲整數。
列表& ltshort int & gtmyInt
};
//整數輸入函數
istream & amp操作員& gt& gt(istream & amp在,BigIntAdd & amp編號)
{
字符串str
在& gt& gtstr//用字符串存儲整數
size _ t len = str . length();
size_t q,r,first
//if對應於構造符號的節點和位數小於三的節點。
if(is digit(str[0])//對應的無符號數。
{
//建立無符號數的有符號節點,將無符號數視為正數。
number . Myint . push _ back(1);
//分組,如果r==0,則確定q個組,即q個節點。
//如果r!=0,則確定q+1個節點。
q = len/3;
r = len % 3;
first = r;
if(r!=0)
{
int nodeValue(0);
size _ t I = 0;
for(;我& lt= r-1;i++)
nodeValue = 10 * nodeValue+(str[I]--0 ');
//註意這裏的表達式對應的是r!=0的節點值
number . Myint . push _ back(nodeValue);
}
}
Else //對應的有符號數
{
//用有符號的數字建立壹個有符號的節點
if(str[0]=='-')
number . Myint . push _ back(-1);
其他
number . Myint . push _ back(1);
//這裏的有符號數需要從第二個元素開始處理。
q =(len-1)/3;
r =(len-1)% 3;
first = r+1;
if(r!=0)
{
int nodeValue(0);
size _ t I = 1;
for(;我& lt= r;i++)
nodeValue = 10 * nodeValue+(str[I]--0 ');
number . Myint . push _ back(nodeValue);
}
}
//for循環//對應的是數字完整的節點。
for(size _ t j = 0;j & ltq;j++)
{
int nodeValue(0);
//壹次有三位,也就是壹個節點的增量。
size _ t index = first+3 * j;
for(int I = 0;我& lt=2;i++)
nodeValue = 10 * nodeValue+(str[index+I]--0 ');
number . Myint . push _ back(nodeValue);
}
返回;
}
//整數輸出函數
牡蠣&;操作員& lt& lt(ostream & ampout,const BigIntAdd & amp編號)
{
列表& ltshort int & gt* const _ iterator ITER = number . Myint . begin();
//指向符號節點
if(*iter==-1)
out & lt& lt'-';//如果是負數,則輸出“--”,如果是正數,則不輸出任何符號。
if(++iter==number.myInt.end())
out & lt& lt0;//對應的數字是零
Else //輸出每個節點對應的數字。
{
out & lt& lt* iter
for(iter++;iter!= number . Myint . end();++iter)
out & lt& lt環境運輸及工務局局長(三)
& lt& ltset fill(“0”)
& lt& lt* iter
}
退了出來;
}
//整數加法函數
BigIntAdd運算符+(BigIntAdd & amp;add1,BigIntAdd & ampadd2)
{
//取兩個數的符號
列表& ltshort int & gt*叠代器ITER 1 = add 1 . Myint . begin();
列表& ltshort int & gt*叠代器ITER 2 = add 2 . Myint . begin();
短sign1=*iter1,sign2 = * iter2
短求和符號;//保存結果符號
size _ t SZ 1 = add 1 . Myint . size()-1;//add1數值部分的節點數
size _ t sz2 = add 2 . Myint . size()-1;//add2的數值部分的節點數
//確定運算結果的符號。
if(sign 1 * sign 2 & gt;0) //兩個數符號相同。
{
if(sign 1 =-1)//兩個數都是負數。
{
sumSign =-1;
sign 1 = 1;
sign 2 = 1;
}
Else //兩個數字都是正數。
sumSign = 1;
}
Else //兩個數符號不同。
if(sign 1 * sign 2 & lt;0)
If(sz1==sz2) //兩位數相等。
{
//從第壹個數值節點,即最高位開始
for(iter1++,ITER 2++;iter1!= add 1 . Myint . end();iter1++,iter2++)
{
if(* ITER 1 * sign 1+* ITER 2 * sign 2 & lt;0)//負數的絕對值大。
{
sumSign =-1;
sign 1 =-sign 1;
sign 2 =-sign 2;
打破;
}
if((* ITER 1 * sign 1)+(* ITER 2 * sign 2)>0)//正數的絕對值大。
{
sumSign = 1;
打破;
}
}
如果(ITER 1 = =加1。Myint。end())//絕對值相等。
sumSign = 1;
}
Else //兩位數不相等。
if(SZ 1 * sign 1+sz2 * sign 2 & lt;0)//負數位數多。
{
sumSign =-1;
sign 1 =-sign 1;
sign 2 =-sign 2;
}
Else //正數有很多位數。
sumSign = 1;
//確定運算結果的數值部分。
列表& ltshort int & gt::reverse _ iterator riter 1 = add 1 . Myint . Rb egin();
列表& ltshort int & gt* reverse _ iterator riter 2 = add 2 . Myint . Rb egin();
//定義兩個反向叠代器。
BigIntAdd sum//存儲運算結果。
短進位= 0;//存儲進位值
//兩個數對應的加法部分不為零。
while(riter1!=-add 1 . Myint . rend()& amp;& ampriter2!= - add2.myInt.rend())
{
short temp =(* riter 1 * sign 1)+(* riter 2 * sign 2)+進位;
if(temp & lt;0) //運算值為負。
{
temp+= short(1000);
進位=-1;
}
Else //運算值為正。
{
進位= temp/short(1000);
temp % = 1000;
}
sum . Myint . push _ front(temp);
riter 1++;
riter 2++;
}
//對應的//add2的相加部分為零。
while(riter1!= - add1.myInt.rend())
{
short temp = * riter 1+進位;
if(temp & lt;0) //運算值為負。
{
temp+= short(1000);
進位=-1;
}
Else //運算值為正。
{
進位= temp/1000;
temp % = 1000;
}
sum . Myint . push _ front(temp);
riter 1++;
}
//add1對應零加法。
while(riter2!= - add2.myInt.rend())
{
short temp = * riter 2+進位;
if(temp & lt;0) //運算值為負。
{
temp+= short(1000);
進位=-1;
}
Else //運算值為正。
{
進位= temp/1000;
temp % = 1000;
}
sum . Myint . push _ front(temp);
riter 2++;
}
如果(進位!=0) //最後對應節點的運算結果有進位。
sum.myInt.push_front(進位);
while(*sum.myInt.begin()==0)
sum . Myint . pop _ front();
//當最高位對應的節點沒有有效值時,刪除該節點。
sum . Myint . push _ front(sumSign);
//將運算結果的符號添加到結果鏈表中。
返回總和;
}