



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)題目:設(shè)計(jì)一數(shù)據(jù)結(jié)構(gòu)可處理任意長度的整數(shù)概要設(shè)計(jì)1. 數(shù)據(jù)結(jié)構(gòu)的定義采用雙向鏈表存儲任意長整數(shù)。雙向鏈表的定義如下:classDblListprivate:DblNode*head,*tail;DblNode*current;intsign;public:DblList();/ 構(gòu)造函數(shù)DblList();/ 析構(gòu)函數(shù)boolCreatList(string);/ 生成一個(gè)雙向鏈表,存儲整數(shù)intGetCount();/ 獲取整數(shù)的長度voidInsert(DblNode*);/從表尾插入一個(gè)結(jié)點(diǎn)voidInsertFront(DblNode*);/從表頭插入voidClear();/ 清
2、除該鏈表voidoperator+(DblList&);/實(shí)現(xiàn)兩個(gè)任意整數(shù)的加法voidoperator*(DblList&);/實(shí)現(xiàn)兩個(gè)任意整數(shù)的乘法DblList&operator=(DblList&);/重載賦值運(yùn)算符intCompare(DblList&);/兩個(gè)整數(shù)的絕對值比較voidDisplay();/ 任意長度整數(shù)的標(biāo)準(zhǔn)化輸出;說明 : 數(shù)據(jù)的存儲,無外乎順序或者鏈表。順序存儲時(shí),定義數(shù)組無法實(shí)現(xiàn)任意長度,而且需要預(yù)設(shè)一個(gè) maxsize ,不是特別的方便。所以采用鏈?zhǔn)酱鎯Ψ绞?。而且任意長數(shù)據(jù)通過字符串輸入。在鏈表的每一個(gè)結(jié)點(diǎn)中,數(shù)據(jù)域是
3、在該數(shù)位上的數(shù)字大小。2主要功能模塊的功能任意長整數(shù)的輸入任意長整數(shù)的標(biāo)準(zhǔn)化輸出兩個(gè)整數(shù)的加法兩個(gè)整數(shù)的乘法三詳細(xì)設(shè)計(jì)(主模塊流程圖)五、 使用說明及測試結(jié)果1. 使用說明:點(diǎn)擊打開應(yīng)用程序pro1.exe 。依次輸入任意兩個(gè)整數(shù)(例如123456,+1234567),按回車,會出現(xiàn)菜單,如下圖:按 1則實(shí)現(xiàn)兩整數(shù)的加法按 2則實(shí)現(xiàn)兩整數(shù)的乘法按 #結(jié)束注:菜單可重復(fù)出現(xiàn)直至#退出。實(shí)現(xiàn)加法,乘法如下圖:2.測試結(jié)果:(1)123456(2)+1234567(3)(4)12a3(5) +注:當(dāng)輸入錯(cuò)誤時(shí),允許重新輸入。六、 源程序/* 主函數(shù) */*/#include"cal.h&q
4、uot;voidmain()strings;stringp;DblListlist1;while(1)/ 輸入錯(cuò)誤時(shí),允許重新輸入cout<<"Inputnum1"<<endl;cin>>s;boolok1=list1.CreatList(s);if(!ok1)cout<<"error!"<<endl;elsecout<<"num1:"list1.Display();break;DblListlist2;while(1)cout<<"Inpu
5、tnum2:"<<endl;cin>>p;boolok2=list2.CreatList(p);if(!ok2)cout<<"error!"<<endl;elsecout<<"num2:"list2.Display();break;stringchoose;while(1)cout<<" 請選擇運(yùn)算法:"<<endl;cout<<"-"<<endl;/*菜單 */cout<<"
6、|1.num1+num2|"<<endl;/*可以重復(fù)輸入運(yùn)算符,按cout<<"|2.num1*num2|"<<endl;cout<<"|#.exit|"<<endl;cout<<"-"<<endl;'#'退出 */while(1)cin>>choose;if(choose="1")list1+list2;break;elseif(choose="2")list1*list
7、2;break;elseif(choose="#")return;elsecout<<" 輸入有誤,請重新輸入!"<<endl;continue;/* 頭文件,包括長整數(shù)數(shù)據(jù)結(jié)構(gòu)的定義,成員函數(shù)的定義*/*/#include<iostream>#include<string>#include<cmath>usingnamespacestd;structDblNodeintdata;DblNode*prior;DblNode*next;boolIsNum(chara)/ 判斷字符a 是否是便是數(shù)字
8、ints=a-'0'if(s>=0&&s<10)returntrue;elsereturnfalse;boolIsInt(stringa)/ 判斷字符串a(chǎn) 是否表達(dá)一串?dāng)?shù)字boolJud=1;inti=1;chars=a0;if(a="+"|a="-")returnfalse;if(s='+'|s='-')elseif(s>='1'&&s<='9')elseif(a0='0'&&a1=
9、39;0')returntrue;elsereturnfalse;while(ai!='0')Jud=IsNum(ai);if(Jud=0)returnfalse;i+;returntrue;intJudSign(strings)/ 返回?cái)?shù)字的符號if(s0='-')return-1;elseif(s0='0'&&s1='0')return0;elsereturn1;intCtoI(chara)inti=a-'0'returni;classDblList/ 定義一個(gè)雙向鏈表類,存儲任意長度的
10、數(shù)字private:/ 并可以進(jìn)行標(biāo)準(zhǔn)化輸出和加法,乘法。DblNode*head,*tail;DblNode*current;intsign;public:DblList();/ 構(gòu)造函數(shù)DblList();/ 析構(gòu)函數(shù)boolCreatList(string);/ 生成一個(gè)雙向鏈表intGetCount();/ 獲取整數(shù)的長度voidInsert(DblNode*);/從表尾插入一個(gè)結(jié)點(diǎn)voidInsertFront(DblNode*);/從表頭插入一個(gè)結(jié)點(diǎn)voidClear();/ 清除該鏈表voidoperator+(DblList&);/實(shí)現(xiàn)兩個(gè)任意整數(shù)的加法voidoper
11、ator*(DblList&);/實(shí)現(xiàn)兩個(gè)任意整數(shù)的乘法DblList&operator=(DblList&);/重載賦值運(yùn)算符intCompare(DblList&);/兩個(gè)整數(shù)的絕對值比較voidDisplay();/ 任意長度整數(shù)的標(biāo)準(zhǔn)化輸出;DblList:DblList()head=newDblNode();/ 構(gòu)造函數(shù)head->next=NULL;head->prior=NULL;tail=head;current=NULL;sign=0;DblList:DblList()/析構(gòu)函數(shù)while(head->next!=NULL)c
12、urrent=head->next;head->next=current->next;deletecurrent;current=NULL;sign=0;deletehead;head=NULL;tail=NULL;intDblList:GetCount()/返回該數(shù)字的長度(不包括符號位)current=head->next;intcount=0;while(current)count+;current=current->next;current=NULL;returncount;voidDblList:Insert(DblNode*p)/從鏈表尾部插入一個(gè)結(jié)點(diǎn)
13、tail->next=p;p->prior=tail;tail=p;voidDblList:InsertFront(DblNode*q)/ 從鏈表頭部插入一個(gè)結(jié)點(diǎn) if(head->next=NULL)head->next=q;q->prior=head;tail=q;elseq->next=head->next;head->next->prior=q;head->next=q;q->prior=head;boolDblList:CreatList(strings)/輸入的任意長度的表示數(shù)字的字符串boolj=IsInt(s);
14、/ 以此生成雙向鏈表if(!j) returnj;elseinti=0;sign=JudSign(s);if(s0='+'|s0='-')i+;while(si!='0')intia=CtoI(si);current=newDblNode();current->data=ia;current->next=NULL;current->prior=NULL;Insert(current);i+;current=NULL;returntrue;voidDblList:Clear()while(head->next)current
15、=head->next;head->next=current->next;deletecurrent;tail=head;sign=0;current=NULL;intDblList:Compare(DblList&s)/ 任意兩個(gè)長度數(shù)字絕對值比較 inta=GetCount();intb=s.GetCount();if(a>b)return1;elseif(a<b)return-1;elsecurrent=head->next;s.current=s.head->next;while(current!=NULL)intre=current-
16、>data-s.current->data;if(re>0)return1;elseif(re<0)return-1;elsecurrent=current->next;s.current=s.current->next;current=NULL;s.current=NULL;return0;DblList&DblList:operator=(DblList&s)Clear();sign=s.sign;s.current=s.head->next;while(s.current!=NULL)current=newDblNode();cu
17、rrent->data=s.current->data;Insert(current);s.current=s.current->next;s.current=NULL;current=NULL;return*this;voidDblList:operator+(DblList&s)/實(shí)現(xiàn)加法(包括減法)DblListtemp;intda;intf=0;intsi=Compare(s);if(si=0&&(sign+s.sign=0)temp.sign=0;elseif(si=0)temp.sign=sign;elseif(si>0)temp.s
18、ign=sign;elsetemp.sign=s.sign;current=tail;s.current=s.tail;while(1)if(current=head&&s.current=s.head)if(f)da=f;temp.current=newDblNode();temp.current->data=f;temp.InsertFront(temp.current);if(!f)break;f=0;elseif(current!=head&&s.current=s.head)temp.current=newDblNode();temp.curre
19、nt->data=current->data+f;temp.InsertFront(temp.current);current=current->prior;f=0;elseif(current=head&&s.current!=s.head)temp.current=newDblNode();temp.current->data=s.current->data+f;temp.InsertFront(temp.current);s.current=s.current->prior;f=0;elseda=current->data*sig
20、n+s.current->data*s.sign+f;if(da*temp.sign>=10)da=da-10*temp.sign;f=temp.sign;elseif(da*temp.sign<0)da=da+10*temp.sign;f=-temp.sign;elsef=0;temp.current=newDblNode();temp.current->next=NULL;temp.current->data=abs(da);temp.InsertFront(temp.current);current=current->prior;s.current=s
21、.current->prior;current=NULL;s.current=NULL;temp.current=temp.head->next;if(temp.current!=NULL)while(temp.current->data=0)temp.head->next=temp.current->next;deletetemp.current;temp.current=temp.head->next;temp.current=NULL;cout<<"num1+num2="temp.Display();voidDblLis
22、t:operator*(DblList&s)/實(shí)現(xiàn)乘法intcf=0;intans;inti,j;intcount=0;DblListtemp;temp.sign=sign*s.sign;inta1=GetCount();inta2=s.GetCount();inta=a1+a2;for(i=0;i<a;i+)temp.current=newDblNode();temp.current->data=0;temp.current->next=NULL;temp.current->prior=NULL;temp.InsertFront(temp.current);s.current=s.tail;while(s.current!=s.head)current=tail;temp.current=temp.tail;for(i=0;i<count;i+)temp.current=temp.current->prior;for(j=0;j<a1;j+)ans=s.current->data*current->data+temp.current->data+cf;temp.current->data=ans%10;cf=ans/10;cu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025關(guān)于制定個(gè)人的汽車租賃合同范本
- 2025北京市家具買賣合同范本(桌椅類)
- 妊娠合并缺鐵性貧血
- 小說家創(chuàng)作藝術(shù)與職業(yè)發(fā)展全解析
- 幼兒園醫(yī)學(xué)啟蒙教育
- 河北省衡水市2024-2025學(xué)年高二下學(xué)期4月期中聯(lián)考試題 生物 含答案
- 浙江省衢州市五校聯(lián)盟2024-2025學(xué)年高二下學(xué)期期中聯(lián)考語文試卷(PDF版含答案)
- 委托勘察業(yè)務(wù)協(xié)議
- 某酒吧的廣告策劃
- 某房地產(chǎn)販賣技巧培訓(xùn)
- 【MOOC】信號與系統(tǒng)-南京郵電大學(xué) 中國大學(xué)慕課MOOC答案
- DB32T 2334.4-2013 水利工程施工質(zhì)量檢驗(yàn)與評定規(guī)范 第4部分 電氣設(shè)備與自動化
- 導(dǎo)尿術(shù)課件完整版
- 寧夏銀川市一中2025屆高考數(shù)學(xué)押題試卷含解析
- 院感防控應(yīng)急演練方案
- 高考3500詞匯表(完整版)
- 中國咳嗽基層診療與管理指南(2024年)解讀
- 2024年度-工程造價(jià)培訓(xùn)課件全新
- 13馬爾可夫鏈公開課獲獎?wù)n件
- 江蘇省高速公路施工標(biāo)準(zhǔn)化技術(shù)指南-工地建設(shè)篇
- 銀行行長任職表態(tài)發(fā)言稿(7篇)
評論
0/150
提交評論