數(shù)據(jù)結(jié)構(gòu)課程設(shè)計—一元多項式加法、減法、乘法運算的實現(xiàn).doc_第1頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計—一元多項式加法、減法、乘法運算的實現(xiàn).doc_第2頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計—一元多項式加法、減法、乘法運算的實現(xiàn).doc_第3頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計—一元多項式加法、減法、乘法運算的實現(xiàn).doc_第4頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計—一元多項式加法、減法、乘法運算的實現(xiàn).doc_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1.一元多項式加法、減法、乘法運算的實現(xiàn)1.1設(shè)計內(nèi)容及要求1)設(shè)計內(nèi)容(1)使用順序存儲結(jié)構(gòu)實現(xiàn)多項式加、減、乘運算。例如:,求和結(jié)果:(2)使用鏈?zhǔn)酱鎯Y(jié)構(gòu)實現(xiàn)多項式加、減、乘運算,求和結(jié)果:2)設(shè)計要求(1)用C語言編程實現(xiàn)上述實驗內(nèi)容中的結(jié)構(gòu)定義和算法。(2)要有main()函數(shù),并且在main()函數(shù)中使用檢測數(shù)據(jù)調(diào)用上述算法。(3)用switch語句設(shè)計如下選擇式菜單。 *數(shù)據(jù)結(jié)構(gòu)綜合性實驗* *一、多項式的加法、減法、乘法運算* * 1.多項式創(chuàng)建 * * 2.多項式相加 * * 3.多項式相減 * 4.多項式相乘 * 5.清空多項式 * 0.退出系統(tǒng) * 請選擇(05) *請選擇(0-5):1.2數(shù)據(jù)結(jié)構(gòu)設(shè)計根據(jù)下面給出的存儲結(jié)構(gòu)定義:#define MAXSIZE 20 /定義線性表最大容量 /定義多項式項數(shù)據(jù)類型typedef structfloat coef; /系數(shù)int expn; /指數(shù) term,elemType;typedef structterm termsMAXSIZE; /線性表中數(shù)組元素int last; /指向線性表中最后一個元素位置 SeqList;typedef SeqList polynomial;1.3基本操作函數(shù)說明 polynomial*Init_Polynomial();/初始化空的多項式int PloynStatus(polynomial*p)/判斷多項式的狀態(tài) int Location_Element(polynomial*p,term x)在多項式p中查找與x項指數(shù)相同的項是否存在int Insert_ElementByOrder(polynomial*p,term x)/在多項式p中插入一個指數(shù)項xint CreatePolyn(polynomial*P,int m)/輸入m項系數(shù)和指數(shù),建立表示一元多項式的有序表pchar compare(term term1,term term2)/比較指數(shù)項term1和指數(shù)項term2polynomial*addPloyn(polynomial*p1,polynomial*p2)/將多項式p1和多項式p2相加,生成一個新的多項式polynomial*subStractPloyn(polynomial*p1,polynomial*p2)/多項式p1和多項式p2相減,生成一個新的多項式polynomial*mulitPloyn(polynomial*p1,polynomial*p2)/多項式p1和多項式p2相乘,生成一個新的多項式void printPloyn(polynomial*p)/輸出在順序存儲結(jié)構(gòu)的多項式p1.4程序源代碼#include#include#include#define NULL 0#define MAXSIZE 20typedef structfloat coef;int expn;term,elemType;typedef structterm termsMAXSIZE;int last;SeqList;typedef SeqList polynomial;void printPloyn(polynomial*p);int PloynStatus(polynomial*p)if(p=NULL)return -1;else if(p-last=-1)return 0;elsereturn 1;polynomial*Init_Polynomial()polynomial*P;P=new polynomial;if(P!=NULL)P-last=-1;return P;elsereturn NULL;void Reset_Polynomial(polynomial*p)if(PloynStatus(p)=1)p-last=-1;int Location_Element(polynomial*p,term x)int i=0;if(PloynStatus(p)=-1)return 0;while(ilast & p-termsi.expn!=x.expn)i+;if(ip-last)return 0;elsereturn 1;int Insert_ElementByOrder(polynomial*p,term x)int j;if(PloynStatus(p)=-1)return 0;if(p-last=MAXSIZE-1)coutThe polym is full!last;while(p-termsj.expn=0) p-termsj+1=p-termsj; j-; p-termsj+1=x;p-last+;return 1;int CreatePolyn(polynomial*P,int m)float coef;int expn;term x;if(PloynStatus(P)=-1)return 0;if(mMAXSIZE)printf(順序表溢出n);return 0;elseprintf(請依次輸入%d對系數(shù)和指數(shù).n,m);for(int i=0;iterm2.expn)return;else if(term1.expnterm2.expn)return;elsereturn=;polynomial*addPloyn(polynomial*p1,polynomial*p2)int i,j,k;i=0;j=0;k=0;if(PloynStatus(p1)=-1)|(PloynStatus(p2)=-1)return NULL;polynomial*p3=Init_Polynomial();while(ilast & jlast)switch(compare(p1-termsi,p2-termsj)case:p3-termsk+=p1-termsi+;p3-last+;break;casetermsk+=p2-termsj+;p3-last+;break;case=:if(p1-termsi.coef+p2-termsj.coef!=0)p3-termsk.coef=p1-termsi.coef+p2-termsj.coef;p3-termsk.expn=p1-termsi.expn;k+;p3-last+;i+;j+;while(ilast)p3-termsk+=p1-termsi+;p3-last+;return p3;polynomial*subStractPloyn(polynomial*p1,polynomial*p2)int i;i=0;if(PloynStatus(p1)!=1)|(PloynStatus(p2)!=1)return NULL;polynomial*p3=Init_Polynomial();p3-last=p2-last;for(i=0;ilast;i+)p3-termsi.coef=-p2-termsi.coef;p3-termsi.expn=p2-termsi.expn;p3=addPloyn(p1,p3);return p3;polynomial*mulitPloyn(polynomial*p1,polynomial*p2)int i;int j;int k;i=0;if(PloynStatus(p1)!=1)|(PloynStatus(p2)!=1)return NULL;polynomial*p3=Init_Polynomial();polynomial*p=new polynomial*p2-last+1;for(i=0;ilast;i+)for(k=0;klast;k+)pk=Init_Polynomial();pk-last=p1-last;for(j=0;jlast;j+)pk-termsj.coef=p1-termsj.coef*p2-termsk.coef;pk-termsj.expn=p1-termsj.expn+p2-termsk.expn;p3=addPloyn(p3,pk);return p3;void printPloyn(polynomial*p)int i;for(i=0;ilast;i+)if(p-termsi.coef0 & i0)cout+termsi.coef;elsecouttermsi.coef;coutxtermsi.expn;coutendl;void menu()couttt*數(shù)據(jù)結(jié)構(gòu)綜合性實驗*endl;couttt*一、多項式的加、減、乘法運算*endl;couttt* 1.多項式創(chuàng)建 *endl;couttt* 2.多項式相加 *endl;couttt* 3.多項式相減 *endl;couttt* 4.多項式相乘 *endl;couttt* 5.清空多項式 *endl;couttt* 0.退出系統(tǒng) *endl;couttt* 請選擇(0-5) *endl;couttt*endl;void main()int sel;polynomial*p1=NULL;polynomial*p2=NULL;polynomial*p3=NULL;while(1)menu();coutsel;switch(sel)case 1:p1=Init_Polynomial();p2=Init_Polynomial();int m;printf(請輸入第一個多項式的項數(shù):n);scanf(%d,&m);CreatePolyn(p1,m);printf(第一個多項式的表達式為p1=);printPloyn(p1);printf(請輸入第二個多項式的項數(shù):n);scanf(%d,&m);CreatePolyn(p2,m);printf(第二個多項式的表達式為p2=);printPloyn(p2);break;case 2:printf(p1+p2=);if(p3=subStractPloyn(p1,p2)!=NULL)printPloyn(p3);break;case 3:printf(np1-p2=);if(p3=subStractPloyn(p1,p2)!=NULL)printPloyn(p3);break;case 4:printf(np1*p2=);if(p3=mulitPloyn(p1,p2)!=NULL)printPloyn(p3);case 5:Reset_Polynomial(p1);Reset_Polynomial(p2);Reset_Polynomial(p3);break;case 0:return;return;1.5程序執(zhí)行結(jié)果2.迷宮問題實現(xiàn)2.1設(shè)計內(nèi)容及要求1)設(shè)計內(nèi)容以一個m*n的長方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設(shè)計一個程序,對任意設(shè)定的迷宮,求出一條從入口到出口的道路,或得出沒有通路的結(jié)論。2)設(shè)計要求(1)用C語言編程實現(xiàn)上述實驗內(nèi)容中的結(jié)構(gòu)定義和算法;(2)要有main()函數(shù),并且在main()函數(shù)中使用檢測數(shù)據(jù)調(diào)用上述算法;2.2數(shù)據(jù)結(jié)構(gòu)設(shè)計根據(jù)以上問題給出存儲結(jié)構(gòu)定義:typedef struct /定義坐標(biāo)int x;int y;item; /定義坐標(biāo)和方向typedef structint x;int y;int d;dataType; /定義順序棧的類型定義typedef structdataType dataMAXLEN;int top;SeqStack;item move8; /8鄰域試探方向數(shù)組int mazeM+2N+2=1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,; /定義迷宮數(shù)組,0表示有路徑,1表示不存在路徑2.3基本操作函數(shù)說明void print_Path(SeqStack*s);/輸出迷宮路線SeqStack*InitSeqStack()/該函數(shù)初始化一個空棧,并返回指向該棧的存儲單元首地址int Push(SeqStack*s,dataType x)/將元素x入棧s,若入棧成功返回結(jié)果1;否則返回0int StackEmpty(SeqStack*s)/該函數(shù)判斷棧是否為空,若??辗祷亟Y(jié)果1;否則返回0int Pop(SeqStack*s,dataType*x)/將棧頂元素出棧,放入x所指向的存儲單元中,若出棧返回結(jié)果1;否則返回0void init_move(item move8)/初始化8鄰域方向int find_Path(int mazeM+2N+2,item move8)/在迷宮maze二維數(shù)組中按move的8鄰域方向探測迷宮路線,存在返回1,否則返回0void print_Path(SeqStack*s)/輸出棧s中所有迷宮路徑2.4程序源代碼#include#include#define M 6#define N 8#define MAXLEN 100typedef structint x;int y;item;typedef structint x;int y;int d;dataType;typedef structdataType dataMAXLEN;int top;SeqStack;item move8;int mazeM+2N+2=1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,;void print_Path(SeqStack*s);SeqStack*InitSeqStack()SeqStack*s;s=new SeqStack;s-top=-1;return s;int Push(SeqStack*s,dataType x)if(s-top=MAXLEN-1)return 0;elses-top+;s-datas-top=x;return 1;int StackEmpty(SeqStack*s)if(s-top=-1)return 1;elsereturn 0;int Pop(SeqStack*s,dataType*x)if(StackEmpty(s)return 0;else*x=s-datas-top;s-top-;return 1;void init_move(item move8)move0.x=0;move0.y=1;move1.x=1;move1.y=1;move2.x=1;move2.y=0;move3.x=1;move3.y=-1;move4.x=0;move4.y=-1;move5.x=-1;move5.y=-1;move6.x=-1;move6.y=0;move7.x=-1;move7.y=1;void printS(dataType temp)int static i=0;printf(第%d次入棧元素為:,+i);printf(%d,%d)%dn,temp.x,temp.y,temp.d);int find_Path(int mazeM+2N+2,item move8)SeqStack*s=InitSeqStack();dataType temp;int x,y,d,i,j;temp.x=1;temp.y=1;temp.d=-1;Push(s,temp); while(!StackEmpty(s)Pop(s,&temp);x=temp.x;y=temp.y;d=temp.d+1;while(d8)i=x+moved.x;j=y+moved.y;if(mazeij=0)temp.x=x;temp.y=y;temp.d=d;Push(s,temp);printS(temp);x=i;y=j;mazexy=-1;if(x=M & y=N)print_Path(s);return 1;elsed=0;elsed+;return 0;void print_Path(SeqStack*s)printf(迷宮路徑為

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論