




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗題目:設計一數(shù)據(jù)結構可處理任意長度的整數(shù) 概要設計1.數(shù)據(jù)結構的定義采用雙向鏈表存儲任意長整數(shù)。雙向鏈表的定義如下:class DblList private: DblNode *head, *tail; DblNode *current;int sign;public:DblList(); /構造函數(shù) DblList(); /析構函數(shù)bool CreatList(string); /生成一個雙向鏈表,存儲整數(shù)int GetCount(); /獲取整數(shù)的長度void Insert(DblNode *); /從表尾插入一個結點 void InsertFront(DblNode *); /從表
2、頭插入void Clear(); /清除該鏈表void operator+(DblList &); /實現(xiàn)兩個任意整數(shù)的加法void operator*(DblList &); /實現(xiàn)兩個任意整數(shù)的乘法DblList & operator=(DblList &); /重載賦值運算符 int Compare(DblList &); /兩個整數(shù)的絕對值比較 void Display(); /任意長度整數(shù)的標準化輸出;說明:數(shù)據(jù)的存儲,無外乎順序或者鏈表。順序存儲時,定義數(shù)組無法實現(xiàn)任意長度,而且需要預設一個maxsize,不是特別的方便。所以采用鏈式存儲方式
3、。而且任意長數(shù)據(jù)通過字符串輸入。在鏈表的每一個結點中,數(shù)據(jù)域是在該數(shù)位上的數(shù)字大小。2 主要功能模塊的功能u 任意長整數(shù)的輸入u 任意長整數(shù)的標準化輸出u 兩個整數(shù)的加法u 兩個整數(shù)的乘法三詳細設計(主模塊流程圖)5、 使用說明及測試結果1.使用說明:點擊打開應用程序pro1.exe。依次輸入任意兩個整數(shù)(例如123456,+1234567),按回車,會出現(xiàn)菜單,如下圖:按1則實現(xiàn)兩整數(shù)的加法按2則實現(xiàn)兩整數(shù)的乘法按#結束注:菜單可重復出現(xiàn)直至#退出。實現(xiàn)加法,乘法如下圖:2.測試結果:(1) 123456 (2) +1234567 (3) -987654321 (4) 12a3 (5) +
4、注:當輸入錯誤時,允許重新輸入。6、 源程序/* 主函數(shù) */*/#include "cal.h"void main()string s; string p; DblList list1;while(1) /輸入錯誤時,允許重新輸入 cout<<"Input num1"<<endl; cin>>s; bool ok1=list1.CreatList(s);if (!ok1) cout<<"error!"<<endl;elsecout<<"num1:&qu
5、ot;list1.Display();break;DblList list2;while(1)cout<<"Input num2:"<<endl;cin>>p; bool ok2=list2.CreatList(p);if (!ok2)cout<<"error!"<<endl;elsecout<<"num2:"list2.Display();break;string choose;while (1) cout<<"請選擇運算法:"&
6、lt;<endl;cout<<"-"<<endl; /*菜單*/cout<<"|1.num1+num2 |"<<endl; /*可以重復輸入運算符,按'#'退出*/cout<<"|2.num1*num2 |"<<endl;cout<<"|#.exit |"<<endl;cout<<"-"<<endl;while (1) cin>>choose;
7、 if (choose="1") list1+list2;break; else if (choose="2") list1*list2;break; else if (choose="#") return; else cout<<"輸入有誤,請重新輸入!"<<endl;continue;/*頭文件,包括長整數(shù)數(shù)據(jù)結構的定義,成員函數(shù)的定義*/*/#include <iostream>#include <string>#include <cmath>usi
8、ng namespace std;struct DblNodeint data; DblNode * prior;DblNode * next;bool IsNum(char a) /判斷字符a是否是便是數(shù)字 int s=a-'0'if(s>=0&&s<10)return true;else return false;bool IsInt(string a) /判斷字符串a是否表達一串數(shù)字bool Jud=1;int i=1;char s=a0;if (a="+"|a="-") return false;if
9、(s='+'|s='-')else if (s>='1'&&s<='9')else if(a0='0'&&a1='0') return true;else return false;while (ai!='0')Jud=IsNum(ai);if (Jud=0) return false;i+;return true;int JudSign(string s) /返回數(shù)字的符號if (s0='-') return -1;els
10、e if(s0='0'&&s1='0') return 0;else return 1;int CtoI(char a)int i=a-'0'return i;class DblList /定義一個雙向鏈表類,存儲任意長度的數(shù)字private: /并可以進行標準化輸出和加法,乘法。DblNode *head, *tail; DblNode *current;int sign;public:DblList(); /構造函數(shù) DblList(); /析構函數(shù)bool CreatList(string); /生成一個雙向鏈表int Ge
11、tCount(); /獲取整數(shù)的長度void Insert(DblNode *); /從表尾插入一個結點 void InsertFront(DblNode *); /從表頭插入一個結點void Clear(); /清除該鏈表void operator+(DblList &); /實現(xiàn)兩個任意整數(shù)的加法void operator*(DblList &); /實現(xiàn)兩個任意整數(shù)的乘法DblList & operator=(DblList &); /重載賦值運算符 int Compare(DblList &); /兩個整數(shù)的絕對值比較void Display()
12、; /任意長度整數(shù)的標準化輸出;DblList:DblList() head=new DblNode(); /構造函數(shù)head->next=NULL;head->prior=NULL; tail=head;current=NULL;sign=0;DblList:DblList() /析構函數(shù)while (head->next!=NULL) current=head->next;head->next=current->next;delete current;current=NULL;sign=0;delete head;head=NULL;tail=NULL;
13、int DblList:GetCount() /返回該數(shù)字的長度(不包括符號位) current=head->next;int count=0;while (current)count+;current=current->next;current=NULL;return count;void DblList:Insert(DblNode *p) /從鏈表尾部插入一個結點 tail->next=p; p->prior=tail;tail=p; void DblList:InsertFront(DblNode *q) /從鏈表頭部插入一個結點if (head->nex
14、t=NULL)head->next=q;q->prior=head;tail=q;elseq->next=head->next;head->next->prior=q;head->next=q;q->prior=head;bool DblList:CreatList(string s) /輸入的任意長度的表示數(shù)字的字符串 bool j=IsInt(s); /以此生成雙向鏈表if (!j) return j;elseint i=0;sign=JudSign(s);if (s0='+'|s0='-')i+;while
15、(si!='0')int ia=CtoI(si);current=new DblNode();current->data=ia; current->next=NULL;current->prior=NULL;Insert(current);i+;current=NULL;return true;void DblList:Clear()while (head->next) current=head->next;head->next=current->next;delete current;tail=head;sign=0;current=
16、NULL;int DblList:Compare(DblList & s) /任意兩個長度數(shù)字絕對值比較int a=GetCount();int b=s.GetCount();if (a>b) return 1;else if (a<b) return -1;else current=head->next;s.current=s.head->next;while (current!=NULL)int re=current->data-s.current->data;if (re>0)return 1;else if (re<0) retu
17、rn -1;elsecurrent=current->next; s.current=s.current->next;current=NULL;s.current=NULL;return 0;DblList & DblList:operator =(DblList &s) Clear();sign=s.sign; s.current=s.head->next;while (s.current!=NULL)current=new DblNode();current->data=s.current->data;Insert(current);s.cur
18、rent=s.current->next;s.current=NULL;current=NULL;return *this;void DblList:operator +(DblList & s) /實現(xiàn)加法(包括減法)DblList temp;int da;int f=0;int si=Compare(s); if (si=0&&(sign+s.sign=0)temp.sign=0;else if (si=0) temp.sign=sign;else if(si>0) temp.sign=sign; else temp.sign=s.sign; curre
19、nt=tail; s.current=s.tail; while (1)if (current=head&&s.current=s.head) if (f) da=f; temp.current=new DblNode(); temp.current->data=f; temp.InsertFront(temp.current);if (!f) break;f=0;else if (current!=head&&s.current=s.head)temp.current=new DblNode(); temp.current->data=curren
20、t->data+f;temp.InsertFront(temp.current);current=current->prior;f=0;else if (current=head&&s.current!=s.head)temp.current=new DblNode();temp.current->data=s.current->data+f;temp.InsertFront(temp.current);s.current=s.current->prior;f=0; elseda=current->data*sign+s.current-&g
21、t;data*s.sign+f;if (da*temp.sign>=10)da=da-10*temp.sign;f=temp.sign;else if (da*temp.sign<0)da=da+10*temp.sign;f=-temp.sign;else f=0; temp.current=new DblNode();temp.current->next=NULL;temp.current->data=abs(da);temp.InsertFront(temp.current);current=current->prior;s.current=s.current
22、->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;delete temp.current;temp.current=temp.head->next; temp.current=NULL;cout<<"num1+num2="temp.Display(); void DblList:
23、operator*(DblList & s) /實現(xiàn)乘法int cf=0;int ans;int i,j;int count=0;DblList temp;temp.sign=sign*s.sign;int a1=GetCount();int a2=s.GetCount();int a=a1+a2; for (i=0;i<a;i+) temp.current=new DblNode();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%
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 三人合伙人合同范本
- 七級 試題及答案
- 七匹狼合同范本
- 使用合同補充協(xié)議書
- 中國億萬富豪調查報告
- 中電投工程安全文明施工組織設計
- 2025年醫(yī)用中心吸引系統(tǒng)項目發(fā)展計劃
- 2025年醫(yī)療社會保障服務項目合作計劃書
- 小紅書店鋪運營策略咨詢與市場拓展合同
- 線上直播帶貨傭金分配合作協(xié)議
- 全國青少年機器人技術等級考試一二級講稿124張課件
- 2023年科普知識生活常識知識-糧食知識考試歷年高頻考點試題含答案
- 人教版九年級數(shù)學下冊《特殊角的三角函數(shù)值及用計算器求角的三角函數(shù)值》評課稿
- 建筑消能減震技術規(guī)程2013
- 五年級語文PPT課件13-秦兵馬俑01
- 2023年福建省莆田市城廂區(qū)數(shù)學六年級第二學期期末統(tǒng)考試題含解析
- 關于一校一品一特色的學校匯報材料
- 2023年綜合基礎知識試題及解析
- 成品、半成品保護方案(土建)
- T-ISEAA 001-2020 網絡安全等級保護測評高風險判定指引
- 安徽省合肥一中、六中、八中2021學年上學期高一年級期末考試化學試卷
評論
0/150
提交評論