c++實現任意長整數的四則運算_第1頁
c++實現任意長整數的四則運算_第2頁
c++實現任意長整數的四則運算_第3頁
c++實現任意長整數的四則運算_第4頁
c++實現任意長整數的四則運算_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗題目:設計一數據結構可處理任意長度的整數 概要設計1.數據結構的定義采用雙向鏈表存儲任意長整數。雙向鏈表的定義如下:class DblList private: DblNode *head, *tail; DblNode *current;int sign;public:DblList(); /構造函數 DblList(); /析構函數bool CreatList(string); /生成一個雙向鏈表,存儲整數int GetCount(); /獲取整數的長度void Insert(DblNode *); /從表尾插入一個結點 void InsertFront(DblNode *); /從表

2、頭插入void Clear(); /清除該鏈表void operator+(DblList &); /實現兩個任意整數的加法void operator*(DblList &); /實現兩個任意整數的乘法DblList & operator=(DblList &); /重載賦值運算符 int Compare(DblList &); /兩個整數的絕對值比較 void Display(); /任意長度整數的標準化輸出;說明:數據的存儲,無外乎順序或者鏈表。順序存儲時,定義數組無法實現任意長度,而且需要預設一個maxsize,不是特別的方便。所以采用鏈式存儲方式

3、。而且任意長數據通過字符串輸入。在鏈表的每一個結點中,數據域是在該數位上的數字大小。2 主要功能模塊的功能u 任意長整數的輸入u 任意長整數的標準化輸出u 兩個整數的加法u 兩個整數的乘法三詳細設計(主模塊流程圖)5、 使用說明及測試結果1.使用說明:點擊打開應用程序pro1.exe。依次輸入任意兩個整數(例如123456,+1234567),按回車,會出現菜單,如下圖:按1則實現兩整數的加法按2則實現兩整數的乘法按#結束注:菜單可重復出現直至#退出。實現加法,乘法如下圖:2.測試結果:(1) 123456 (2) +1234567 (3) -987654321 (4) 12a3 (5) +

4、注:當輸入錯誤時,允許重新輸入。6、 源程序/* 主函數 */*/#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;/*頭文件,包括長整數數據結構的定義,成員函數的定義*/*/#include <iostream>#include <string>#include <cmath>usi

8、ng namespace std;struct DblNodeint data; DblNode * prior;DblNode * next;bool IsNum(char a) /判斷字符a是否是便是數字 int s=a-'0'if(s>=0&&s<10)return true;else return false;bool IsInt(string a) /判斷字符串a是否表達一串數字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) /返回數字的符號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 /定義一個雙向鏈表類,存儲任意長度的數字private: /并可以進行標準化輸出和加法,乘法。DblNode *head, *tail; DblNode *current;int sign;public:DblList(); /構造函數 DblList(); /析構函數bool CreatList(string); /生成一個雙向鏈表int Ge

11、tCount(); /獲取整數的長度void Insert(DblNode *); /從表尾插入一個結點 void InsertFront(DblNode *); /從表頭插入一個結點void Clear(); /清除該鏈表void operator+(DblList &); /實現兩個任意整數的加法void operator*(DblList &); /實現兩個任意整數的乘法DblList & operator=(DblList &); /重載賦值運算符 int Compare(DblList &); /兩個整數的絕對值比較void Display()

12、; /任意長度整數的標準化輸出;DblList:DblList() head=new DblNode(); /構造函數head->next=NULL;head->prior=NULL; tail=head;current=NULL;sign=0;DblList:DblList() /析構函數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() /返回該數字的長度(不包括符號位) 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) /輸入的任意長度的表示數字的字符串 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) /任意兩個長度數字絕對值比較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) /實現加法(包括減法)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) /實現乘法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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論