![數(shù)據(jù)結(jié)構(gòu)大型實驗(大整數(shù))附源代碼_第1頁](http://file4.renrendoc.com/view14/M04/21/1C/wKhkGWZ0XrKASvYuAAF3I7wtP8Q840.jpg)
![數(shù)據(jù)結(jié)構(gòu)大型實驗(大整數(shù))附源代碼_第2頁](http://file4.renrendoc.com/view14/M04/21/1C/wKhkGWZ0XrKASvYuAAF3I7wtP8Q8402.jpg)
![數(shù)據(jù)結(jié)構(gòu)大型實驗(大整數(shù))附源代碼_第3頁](http://file4.renrendoc.com/view14/M04/21/1C/wKhkGWZ0XrKASvYuAAF3I7wtP8Q8403.jpg)
![數(shù)據(jù)結(jié)構(gòu)大型實驗(大整數(shù))附源代碼_第4頁](http://file4.renrendoc.com/view14/M04/21/1C/wKhkGWZ0XrKASvYuAAF3I7wtP8Q8404.jpg)
![數(shù)據(jù)結(jié)構(gòu)大型實驗(大整數(shù))附源代碼_第5頁](http://file4.renrendoc.com/view14/M04/21/1C/wKhkGWZ0XrKASvYuAAF3I7wtP8Q8405.jpg)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
數(shù)據(jù)結(jié)構(gòu)大型實驗實驗報告(大整數(shù)運算)
主要負責人:朱鎮(zhèn)洋
參與者:曹耀明陳華族
目錄
第一部分要求與概述
一、實驗目的以及準備
1.1.1問題描述.........................................................
1.1.2基本要求.........................................................
1.1.3設(shè)計思路.........................................................
第二部分具體實現(xiàn)
一、代碼部分
2.1.1鏈表類及大數(shù)類的部分說明以及部分源碼.............................
2.1.2部分簡單函數(shù)功能..................................................
2.1.3加法...............................................................
2.1.4減法...............................................................
2.1.5乘法...............................................................
2.1.6除法...............................................................
2.1.7基運算.............................................................
2.1.8二進制和十進制的相互轉(zhuǎn)化..........................................
二、程序流程及函數(shù)間關(guān)系
2.2.1程序流程圖........................................................
2.2.2函數(shù)調(diào)用關(guān)系分析..................................................
三、實驗驗證分析
2.3.1輸入的形式和輸入值的范圍..........................................
2.3.2輸出的形式.........................................................
2.3.3程序所能達到的功能.................................................
2.3.4測試數(shù)據(jù)...........................................................
四、調(diào)試分析
2.4.1調(diào)試過程中的主要技術(shù)問題以及具體的解決方法........................
2.4.2印象最深刻的3個調(diào)試錯誤,及修正方法..............................
五、附錄
2.5.1源代碼及其所屬文件.................................................
第一部分要求與概述
1.1.1問題描述
?密碼學分為兩類密碼:對稱密碼和非對稱密碼。對稱密碼主要用于數(shù)據(jù)的加/解密,而非對
稱密碼則主要用于認證、數(shù)字簽名等場合。非對稱密碼在加密和解密時,是把加密的數(shù)據(jù)
當作一個大的正整數(shù)來處理,這樣就涉及到大整數(shù)的加、減、乘、除和指數(shù)運算等,同時,
還需要對大整數(shù)進行輸出。請采用相應的數(shù)據(jù)結(jié)構(gòu)實現(xiàn)大整數(shù)的加、減、乘、除和指數(shù)運
算,以及大整數(shù)的輸入和輸出。
1.1.2基本要求
?要求采用鏈表來實現(xiàn)大整數(shù)的存儲和運算,不允許使用標準模板類的鏈表類(list)和函
數(shù)。同時要求可以從鍵盤輸入大整數(shù),也可以文件輸入大整數(shù),大整數(shù)可以輸出至顯示器,
也可以輸出至文件。大整數(shù)的存儲、運算和顯示,可以同時支持二進制和十進制,但至少
要支持十進制。大整數(shù)輸出顯示時,必須能清楚地表達出整數(shù)的位數(shù)。測試時,各種-況都
需要測試,并附上測試截圖;
?要求大整數(shù)的長度可以不受限制,即大整數(shù)的十進制位數(shù)不受限制,可以為十幾位的整
數(shù),也可以為500多位的整數(shù),甚至更長;?大整數(shù)的運算和顯示時,只需要考慮正的大
整數(shù)。如果可能的話,請以秒為單位顯示每次大整數(shù)運算的時間;
?要求采用類的設(shè)計路,不允許出現(xiàn)類以外的函數(shù)定義,但允許友元函數(shù)。主函數(shù)中只能
出現(xiàn)類的成員函數(shù)的調(diào)用,不允許出現(xiàn)對其它函數(shù)的調(diào)用。
?要求采用多文件方式:」文件存儲類的聲明,.cpp文件存儲類的實現(xiàn),主函數(shù)main存
儲在另外一個單獨的cpp文件中。如果采用類模板,則類的聲明和實現(xiàn)都放在.h文件中;
?要求源程序中有相應注釋;?不強制要求采用類模板,也不要求采用可視化窗口;?要
求測試例子要比較詳盡,各種極限一況也要考慮到,測試的輸出信。要詳細易懂,表明各個
功能的執(zhí)行正確;?要求采用VisualC++6.0及以上版本進行調(diào)試。
1.1.3設(shè)計思路
■根據(jù)題目要求,用鏈表來實現(xiàn)大整數(shù)的存儲,用大整數(shù)類Long_Num并實現(xiàn)一些基本操作,
設(shè)計界面菜單類LNmenu來顯示界面;然后,在main函數(shù)中來完成各種操作與檢驗。
第二部分具體實現(xiàn)
2.1.1鏈表類及大數(shù)類的部分說明以及部分源碼
鏈表類:
List由使用指針連接的節(jié)點組成,每個節(jié)點都保存著數(shù)據(jù),以及指向前驅(qū)和后繼的指針。
每個節(jié)點中存儲一個數(shù)字表示大數(shù)的一位。
源代碼:
node::node()
{
next=NULL;
pre=NULL;
)
node::node(intp)〃節(jié)點構(gòu)造函數(shù)
(
value二p;
next=NULL;
pre=NULL;
)
大數(shù)類:
大數(shù)類有一個頭節(jié)點和尾節(jié)點,分別為head和back,而back的下一個節(jié)點為NULL。當
head與back指向同一位置時表示List為只含一個節(jié)點。Head與back都為NULL表示List
為空。而且用一個int類型的變量存儲大數(shù)的長度,為以后的運算提供便利。
構(gòu)造函數(shù):
,Long_Num()默認構(gòu)造函數(shù)。
Long_Num::Long_Num()〃構(gòu)造函數(shù)
(
head=NULL;
back二NULL;
len=0;
)
?voidequal(Long_Numtemp)用equal代替了構(gòu)造函數(shù)。
voidLong_Num::equal(Long_Numtemp)//復制構(gòu)造函數(shù),將形參復制給當前對象,而非地址傳遞
{
node*p=temp.head;
len=temp.len;
head=newnode(p->value);
back二head;
p=p->next;
while(p)
(
node*Newnode=newnode(p->value);
back->next=Newnode;
Newnode->pre=back;
back=back->next;
p=p->next;
)
}
2.1.2部分簡單函數(shù)功能
?voidlength。把大整數(shù)運算過后前面的0去掉,并且更正len長度。
voidLong_Num::length()〃更正對象長度并規(guī)范大整數(shù)
(
node*p=head;
intn_len=0;
while(p->next)
(
if(p->value==0)
(
p=p->next;
head=head-〉next;
head->pre=NULL;
)
else
break;
}
while(p)
{
n_len++;
p=p->next;
}
len=n_len;
)
?voidequalto(Long_Numtemp)判斷兩大整數(shù)是否相等
boolLong_Num::equalto(Long_Numtemp)〃判斷兩大整數(shù)是否相等
node*pl=head,*p2=temp.head;
boolflag=true;
while(flag&&pl)
(
if(pl->value!=p2->value)
flag二false;
else
(
pl=pl->next;
p2=p2->next;
)
)
returnflag;
}
,voidprint()打印并存儲十進制數(shù)據(jù)。
voidLong_Num::print()〃打印十進制數(shù)并存儲
(
if(head=NULL)
(
cout<<"******數(shù)據(jù)為空!********〃<<endl;
}
else
(
ofstreamoutfile;
outfile.open(/zoutput_Num.ios::app);
node*p=head;
while(p->next)
(
if(p->value==0)
p=p->next;
else
break;
)
while(p)
(
cout<<p->value;
outfile<<p->value;
p=p->next;
)
outfile?endl;
cout?endl;
}
}
?voidprint2()打印并存儲二進制數(shù)據(jù)。
voidLong_Num::print2()〃打印二進制數(shù)并存儲數(shù)
據(jù)
(
if(head二二NULL)
(
cout?!?*****數(shù)據(jù)為空!********”<<endl;
}
else
(
ofstreamoutfile;
outfile.open(zzoutput2_Num.txt〃,ios::app);
node*p二head;
while(p->next)
(
if(p->value==0)
p=p->next;
else
break;
)
while(p)
(
cout<<p->value;
outfile?p->value;
p=p->next;
)
outfile<<endl;
cout<<endl;
)
}
?boolcompare(Long_Numtemp)實現(xiàn)兩個長度相等的大整數(shù)的比較。
boolLong_Num::compare(Long_Numtemp)〃初級比較,只用于長度相等的
大正間的比較,大于等于返回true
boolflag二false;
node*templ=head,*temp2=temp.head;
while(tempi)
(
if(templ->value!=temp2->value)
(
flag=templ->value>temp2->value?true:false;
break;
)
else
(
templ=templ->next;
temp2=temp2->next;
)
}
if([tempi)
flag=true;
returnflag;
}
?booIcompare2(Long_Numtemp)實現(xiàn)兩個大整數(shù)的比較。
boolLong_Num::compare2(Long_Numtemp)〃升級版比較,可比較長度不等
的大整數(shù),大于等于返回true
(
if(len>temp.len)
returntrue;
else
(
if(len==temp.len)
returncompare(temp);
else
returnfalse;
)
)
,voidset_Long_Num(strings)創(chuàng)建大整數(shù),把字符串的內(nèi)容轉(zhuǎn)為int賦給大
整數(shù)。
voidLong_Num::set_Long_Num(strings)〃將輸入的字符串轉(zhuǎn)成大整
數(shù)
intstr_len=s.length();
head二newnode((int)s[0]-48);
len++;
back=head;
if(str_len>l)
(
for(inti=l;i<str_len;i++)
(
node*Newnode=newnode((int)s[i]-48);
len++;
back->next=Newnode;
Newnode-〉pre=back;
back=back->next;
)
}
length();
}
voidLong_Num::set_Long_Num2(inttemp)〃建立大整數(shù),將參數(shù)建立新
節(jié)點加在尾部
(
node*Newnode=newnode(temp);
if(back)
(
back->next=Newnode;
Newnode->pre=back;
back=back->next;
)
else
(
head=Newnode;
back=head;
}
len++;
)
,voidset_Long_Num2(inttemp)如果當前大整數(shù)為空,則創(chuàng)建大整數(shù),并把
值賦給它,如果非空,則在后面添加節(jié)點,把值賦給節(jié)點。
voidLong_Num::set_Long_Num2(inttemp)〃建立大整數(shù),將參數(shù)建立新
節(jié)點加在尾部
node*Newnode=newnode(temp);
if(back)
back->next=Newnode;
Newnode->pre=back;
back=back->next;
)
else
(
head=Newnode;
back二head;
)
len++;
)
LongNumLongNum::subNum(LongNumtemp,node*&p)〃截取一定長度,用于試商
法
(
Long_Numtempp;
node*pl=head,*p2=temp,head;
strings=;
while(p2)
(
s+=pl->value+,O';
pl=pl->next;
p2=p2->next;
p=p->next;
)
tempp.set_Long_Num(s);
if(!tempp.compare(temp))〃如果截取的子串還是比
參數(shù)小,再向后截一位
(
p=p->next;
tempp.setLongNum2(pl->value);
}
tempp.length();
returntempp;
)
?Long_Numsub_Num(Long_Numtemp,node*&p)截取被除數(shù)的字串,用于
除法與除數(shù)的比較,如果小,則再截取后面一位。
Long_NumLong_Num::subNum(LongNumtemp,node*&p)〃截取一定長度,用于試商
法
(
Long_Numtempp;
node*pl=head,*p2=temp,head;
,〃〃
strings二;
while(p2)
(
s+=pl->value+,O';
pl=pl->next;
p2=p2->next;
p=p->next;
}
tempp.set_Long_Num(s);
if(!tempp.compare(temp))〃如果截取的子串還是比
參數(shù)小,再向后截一位
(
p=p->next;
tempp.set_Long_Num2(pl->value);
)
tempp.length();
returntempp;
}
intLong_Num::result(Long_Numtemp)〃除法中的試商法得到當前位
的商
(
length();
intresult=0;
while(compare2(temp))
(
decrease(temp);
result++;
)
length();
returnresult;
)
,intresu11(Long_Numtemp)得到當前大整數(shù)除以除數(shù)的商。
intLong_Num::result(Long_Numtemp)〃除法中的試商法得到當前位
的商
length();
intresult=0;
while(compare2(temp))
decrease(temp);
result++;
}
length();
returnresult;
)
2.1.3加法
實現(xiàn)思路:
加法通過兩個操作數(shù)的每一位相加,滿io進位的思路設(shè)計。
代碼實現(xiàn):
Long_NumLong_Num::add(Long_Numtemp)〃大整數(shù)加法
(
if(temp.head==NULL||head==NULL)
cout<<〃數(shù)據(jù)為空!z/?endl;〃考慮數(shù)據(jù)的特殊情況
else
(
node*templ,*temp2;
inti=0,j;
if(len>temp.len)
(
templ=back;
temp2=temp.back;〃如果當前大整數(shù)大于后一個,則tempi為長的最后一
個節(jié)點
for(;;)
(
while(temp2!=NULL)
(
j=templ->value+temp2->value+i;
templ->value=j%10;
if(j>=10)
i=l;
else
i=0;
temp2=temp2->pre;
tempi=tempi->pre;
)〃在短的那個數(shù)還沒有遍歷完的情況下,實現(xiàn)每一位的加法,設(shè)置
一個i變量,默認設(shè)為0。如果滿10則設(shè)為1,并在前一位加上,否則設(shè)為0。
if(tempi==NULL)
if(i==l)
node*Newnode=newnode(1);
Newnode->next=head;
head->pre=Newnode;
head=head->pre;
break;
)
else
break;〃如果到了長的那個數(shù)的頭一位,而i仍為1,則在
大整數(shù)的前面添加值1的節(jié)點。
)
if(tempi!=NULL)
(
j=templ->value+i;
templ->value=j%10;
if(j>=10)
i=l;
else
i=0;
)
templ=templ->pre;〃如果還沒到長的那個數(shù)的頭一個,執(zhí)行i與當前
數(shù)的加法,如果滿10則i為1,否則i為0。
)
length();
return*this;〃把第一個不為0的數(shù)的前面的0去掉,并
更正大整數(shù)的長度,并返回長的那個對象。
)
if(len==temp.len)
(
i=0;
templ=back;
temp2=temp.back;
while(tempi.->pre!=NULL)
(
j=templ->value+temp2->value+i;
if(j>=10)
(
templ->value=j%10;
i=l;
)
else
templ->value=j;
i=0;
}
temp2=temp2->pre;
templ=templ->pre;
)
J=templ->value+temp2->value+i;〃兩個大整數(shù)位數(shù)相同,每一位都相
加,滿10進1。
if(j>=10)
(
templ->value=j%10;
node*Newnode=newnode(l);
head->pre=Newnode;
Newnode->next=head;
head=head->pre;
}
else
templ->value=j;//到了頭一位時,如果i為1,則在大整
數(shù)前面添加值為1的節(jié)點。
length();
return*this;
)
if(len<temp.len)
(
tempi=temp,back;
temp2=back;
for(;;)
{
while(temp2!=NULL)
(
j=templ->value+temp2->value+i;
templ->value=j%10;
if(j>=10)
i=l;
else
i=0;
temp2=temp2->pre;
tempi二tempi->pre;
}//在短的那個數(shù)還沒有遍歷完的情況下,實現(xiàn)每一位的加法,設(shè)
置一個i變量,默認設(shè)為0。如果滿10則設(shè)為1,并在前一位加上,否則設(shè)為0。
if(templ=NULL)
(
if(i==l)
(
node*Newnode=newnode(1);
Newnode->next=temp.head;
temp.head->pre=Newnode;
temp.head=temp.head->pre;
break;
}
else
break;
}
if(tempi!=NULL)
(
j=templ->value+i;
templ->value=j%10;
if(j>=10)
i=l;
else
i=0;
}
templ=templ->pre;//如果還沒到長的那個數(shù)的頭一個,執(zhí)行i
與當前數(shù)的加法,如果滿10則i為1,否則i為0。
)
equal(temp);
length();
return*this;〃把第一個不為0的數(shù)的前面的0去掉,并更正大整數(shù)
的長度,并返回長的那個對象。
)
)
2.1.4減法
實現(xiàn)思路:
減法通過兩個操作數(shù)每一位相減,不夠則借位的思路設(shè)計。
代碼實現(xiàn):
Long_NumLong_Num::decrease(Long_Numtemp)〃大整數(shù)減法
{
if(head==NULL||temp.head==NULL)
cout?〃數(shù)據(jù)為空!〃<<endl;〃考慮數(shù)據(jù)的特殊情況
else
(
node*templ,*temp2;
inti=0,j;
if(len>temp.len)
tempi二back;
temp2=temp.back;
while(tempi)
(
while(temp2!=NULL)
(
j=templ->value-temp2->value-i;
if(j<0)
(
templ->value=j+10;
i=l;
)
else
(
templ->value=j;
i=0;
)
templ=templ->pre;
temp2=temp2->pre;
}〃在短的那個數(shù)還沒有遍歷完的情況下,實現(xiàn)每一位
的減法,設(shè)置一個i變量,默認設(shè)為0。如果不夠則設(shè)為1,并在前一位減去,否則設(shè)為0。
j=templ->value-i;
if(j<0)
{
templ->value=j+10;
i=l;
)
else
{
templ->value=j;
i=0;
)
templ=templ->pre;〃如果還沒到長的那個數(shù)的頭一個,執(zhí)行i
與當前數(shù)的減法,如果不夠則i為1,否則i為。。
)
length();
return*this;〃把第一個不為0的數(shù)的前面的0去掉,
并更正大整數(shù)的長度,并返回長的那個對象。
)
if(len==temp.len)
(
inti=0,j;
node*templ,*temp2;
if(compare(temp))
if(equalto(temp))
{
set_Long_Num(z,0,z);
return*this;
)〃兩個大整數(shù)相等則直接返回0
else
(
tempi=back;
temp2=temp.back;
while(tempi)
(
j=templ->value-temp2->value-i;
if(j<0)
(
templ->value=j+10;
i=l;
)
else
(
templ->value=j;
i=0;
)
templ=templ->pre;
temp2=temp2->pre;//兩個大整數(shù)位數(shù)相同,每
一位都相減,不夠則把i=l,在前面一位減的時候減去,再把i=0,否則就i=0。
)
length();
return*this;〃把第一個不為0的數(shù)的前面的0去
掉,并更正大整數(shù)的長度,并返回長的那個對象。
)
)
else
(
Long_Numtempp;
tempp.equal(temp);
templ=tempp.back;
temp2=back;
while(tempi)
(
j=templ->value-temp2->value-i;
if(j<0)
(
templ->value=j+10;
i=l;
else
(
templ->value=j;
i=0;
)
tempi=tempi->pre;
temp2=temp2->pre;
)
equal(tempp);
length();
return*this;
)
)
if(len<temp.len)
(
Long_Numtempp;
tempp.equal(temp);
templ=tempp.back;
temp2=back;
while(tempi)
(
while(temp2!=NULL)
(
j=templ->value-temp2->value-i;
if(j<0)
(
templ->value=j+10;
i二1;
}
else
(
templ->value=j;
i=0;
)
tempi二tempi->pre;
temp2=temp2->pre;
}〃在短的那個數(shù)還沒有遍歷完的情況下,實現(xiàn)每一位的減法,但是用長
的那個減去短的那個,設(shè)置一個i變量,默認設(shè)為0。如果不夠則設(shè)為1,并在前一位減去,
否則設(shè)為0。
j=templ->value-i;
if(j<0)
templ->value=j+10;
i二1;
)
else
(
templ->value=j;
i=0;
}
templ=templ->pre;〃如果還沒到長的那個數(shù)的頭一個,執(zhí)行
i與當前數(shù)的減法,用長的那個數(shù)減短的,如果不夠則i為1,否則i為0。
)
equal(tempp);
length();
return*this;〃把第一個不為0的數(shù)的前面的0去掉,
并更正大整數(shù)的長度,并返回長的那個對象。
)
2.1.5乘法
實驗思路:
乘法通過一個操作數(shù)的每一位分別于另一個操作數(shù)的每一位相乘,得到的結(jié)果再相加設(shè)
計。
代碼實現(xiàn):
Long_NumLong_Num::changeO〃刪除頭尾節(jié)點(乘法專
用)
{
node*pl=head->next;
node*p2=head;
while(pl->next!=NULL)
(
p2->value=pl->value;
pl=pl->next;
p2=p2->next;
}
node*p=back;
p->pre->next=NULL;
back=p->pre;
p->pre=NULL;
p二back;
p->pre->next=NULL;
back=p->pre;
p->pre=NULL;
return*this;〃刪除乘法一開始創(chuàng)建的頭尾兩個節(jié)點,并重新定義
頭尾節(jié)點。
)
Long_Nummultiply(Long_Numtempp,Long_Numtemps)〃大整數(shù)乘法
(
Long_Numsum,ex,ex;
ex.head二newnode(0);
ex.len++;
ex.back=ex.head;
sum.head=newnode(0);
sum.len++;
sum.back=sum.head;
ex.head=newnode(0);
ex.len++;
ex.back=ex.head;
node*templ,*temp2,*tempt;
templ=tempp.back;
temp2=temps.back;
for(intw=0;temp2!=NULL;w++)〃設(shè)置w變量,從0循環(huán)到第二個大整數(shù)的長度。
(
Long_Num1;
inti=0,j;
1.head=newnode(0);
1.back=newnode(0);
1.head->next=l.back;
1.back->pre=l.head;
temptG.back;〃先創(chuàng)建一個大整數(shù)1,頭尾結(jié)點分別為0。
for(;;)
(
while(tempi!=NULL)
(
j=templ->value*temp2->value+i;
i二j/10;
templ=templ->pre;
node*Newnode=newnode();
Newnode->pre=l.head;
Newnode->next=l.head->next;
tempt->pre=Newnode;
1.head->next=Newnode;
tempt二tempt->pre;
tempt->value=j%10;
1.len++;
}〃把一個大整數(shù)的每一位乘以另一個大整數(shù)的第一
位,設(shè)置i=0,結(jié)果加上i除以10,商賦給當前值,余數(shù)賦給i,把當前值插入到1的頭結(jié)
點后,1的長度加1。
for(intww=0;w<w;ww++)
(
node*Newnode=newnode(0);
1.back->next=Newnode;
Newnode->pre=l.back;
1.back=Newnode;
1.len++;
)〃當一個大整數(shù)的每一位乘完以后,把另一個大整數(shù)的位
數(shù)往前移一位,并且移動一次,要在1后面添加一個值為0的節(jié)點,并且1的長度加1。
if(tempi二二NULL)
(
if(i!=0)
(
node*Newnode=newnode(i);
Newnode->next=l.head->next;
1.head->next->pre=Newnode;
1.head->next=Newnode;
Newnode->pre=l.head;
1.len++;
}
}//當?shù)竭_第一個大整數(shù)的頭一位時i仍不為0,則在1
的頭結(jié)點后面插入值為i的節(jié)點,并且1的長度加lo
templ=tempp.back;
temp2=temp2->pre;
ex.len=l.len-2;
ex二1.change。;〃去掉原先創(chuàng)建的頭尾兩個值為0的節(jié)點,長度減2。
cx=sum;
ex.len=sum.len;
sum=ex.add2(ex);
break;
)
}
sum.len++;
sum.length();
returnsum;〃把第一個不為0的數(shù)的前面的0去掉,并更正大整數(shù)的長度,
并返回。
2.1.6除法
實驗思路:
除法通過先在被除數(shù)中取跟除數(shù)一樣的位數(shù),進行不斷的減法操作,直至小于除數(shù),然后
再向后面取數(shù)直至位數(shù)于除數(shù)相同,重復。
代碼實現(xiàn):
intLong_Num::result(Long_Numtemp)〃除法中的試商法得到當前
位的商
(
length();
intresult=O;
while(compare2(temp))
(
decrease(temp);
result++;
}
length();
returnresult;
)
voidLong_Num::set_Long_Num2(inttemp)〃建立大整數(shù),將參數(shù)建立新
節(jié)點加在尾部
(
node*Newnode=newnode(temp);
if(back)
(
back->next=Newnode;
Newnode->pre=back;
back=back->next;
)
else
(
head二Newnode;
back=head;
}
len++;
}
Long_NumLong_Num::sub_Num(Long_Numtemp,node*&p)〃截取一定長度,用于試商
法
(
LongNumtempp;
node*pl=head,*p2=temp.head;
?〃〃
strings=;
while(p2)
(
s+=pl->value+,O';
pl=pl->next;
p2=p2->next;
p=p->next;
)
tempp.set_Long_Num(s);
if(!tempp.compare(temp))〃如果截取的子串還是比
參數(shù)小,再向后截一位
{
p=p->next;
tempp.set_Long_Num2(pl->value);
)
tempp.length();
returntempp;
)
Long_NumLong_Num::divide(Long_Numtempi,Long_Num&temp2)〃大整數(shù)除法
(
strings_result二〃〃;
intsh;
Long_Numtemp;
if(compare2(tempi))
(
if(len>templ.len)
(
node*curren=head;
temp=sub_Num(tempi,curren);//當被除數(shù)位數(shù)比除數(shù)
大,先創(chuàng)建一個大整數(shù)temp,存儲在被除數(shù)上截取除數(shù)長度的一段值,如果不夠大,則往
后面再截取一位。
sh=temp.result(tempi);//sh為一次除法得
到的商,temp為得到的余數(shù)
s_result+=sh+,;〃創(chuàng)建一個字符串s,
來累加商
while(curren)
(
temp.set_Long_Num2(curren->value);
sh=temp.result(tempi);
s_result+z:sh+,0,;
curren=curren->next;
)〃余數(shù)加上后一位的值構(gòu)成大整數(shù)
除以除數(shù),如果比除數(shù)小則s加上0,否則s加上商,重復直至最后。
temp2.equal(temp);
set_Long_Num(s_resu11);
length();
return*this;〃返回字符串s為總的商,把余數(shù)賦給
第二個大整數(shù)參數(shù)
)
else
sh=result(tempi);
s_result+=sh+,O';
temp2.equal(*this);
set_Long_Num(s_result);
length();
return*this;〃當被除數(shù)位數(shù)和除數(shù)相同,如上進
行一次除法操作。
)
}
else
(
temp2.equal(*this);
set_Long_Num(〃0");
return*this;〃當被除數(shù)位數(shù)比除數(shù)小,就直接返回s
為0,余數(shù)為被除數(shù).
)
)
2.1.7幕運算
實驗思路:
幕運算則通過底數(shù)自乘,累減1,結(jié)合加法,乘法,減法的方法實現(xiàn)。
代碼實現(xiàn):
boolIf_one(Long_Numtemp)〃判斷對象是為1
{
boolflag=false;
node*p=temp.back;
if(p->value!=l)
returnflag;
else
{
p=p->pre;
flag=true;
while(p)
(
if(p->value!=0)
(
flag=false;
break;
)
else
p=p->pre;
)
returnflag;
boolIf_zero(Long_Numtemp)〃判斷對象是否為0
(
boolflag=false;
node*p=temp.back;
if(p->value!=0)
returnflag;
else
(
p=p->pre;
flag=true;
while(p)
(
if(p->value!=0)
(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 現(xiàn)代醫(yī)療辦公環(huán)境的電氣化改造
- 2024年五年級品社下冊《祖國不會忘記他們》說課稿 山東版
- 2023二年級數(shù)學上冊 6 表內(nèi)乘法(二)綜合與實踐 量一量比一比說課稿 新人教版
- 1 北京的春節(jié) 說課稿-2023-2024學年語文六年級下冊統(tǒng)編版
- 9《生活離不開他們》 感謝他們的勞動 說課稿-2023-2024學年道德與法治四年級下冊統(tǒng)編版
- Unit 2 Weather Lesson 1(說課稿設(shè)計)-2023-2024學年人教新起點版英語二年級下冊001
- 2024年高中英語 Unit 3 Welcome to the unit and reading I說課稿 牛津譯林版選擇性必修第二冊
- 2024-2025學年高中歷史 第五單元 經(jīng)濟全球化的趨勢 第26課 經(jīng)濟全球化的趨勢(1)教學說課稿 岳麓版必修2
- Unit 2 Bridging Cultures Discover Useful Structures 說課稿-2024-2025學年高中英語人教版(2019)選擇性必修第二冊
- Unit 2 Wildlife Protection Discovering Useful Structures 說課稿-2024-2025學年高一上學期英語人教版(2019)必修第二冊
- 基底節(jié)腦出血護理查房
- 糧食貯藏課件
- 工程公司總經(jīng)理年終總結(jié)
- 2024年海南省高考地理試卷(含答案)
- 【企業(yè)盈利能力探析的國內(nèi)外文獻綜述2400字】
- 三年級上冊數(shù)學口算題1000道帶答案
- GB/T 44311-2024適老環(huán)境評估導則
- 蘇教版(2024新版)一年級上冊科學全冊教案教學設(shè)計
- 【長安的荔枝中李善德的人物形象分析7800字(論文)】
- 期末綜合測試卷一(試題)-2023-2024學年一年級下冊數(shù)學滬教版
- QB/T 6019-2023 制漿造紙專業(yè)設(shè)備安裝工程施工質(zhì)量驗收規(guī)范 (正式版)
評論
0/150
提交評論