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

下載本文檔

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

文檔簡介

1、1.兀多項式加法、減法、乘法運算的實現(xiàn)1.1設(shè)計內(nèi)容及要求1)設(shè)計內(nèi)容(1) 使用順序存儲結(jié)構(gòu)實現(xiàn)多項式加、減、乘運算。例如:r65425432f(x) =8x5x 10x32 x x 10 , g(x) =7x 10 x 20 x 10 x - x求和結(jié)果:f (x) g(x) =8x6 12x5 _ 20 x3 22 x 2 - 10(2) 使用鏈?zhǔn)酱鎯Y(jié)構(gòu)實現(xiàn)多項式加、減、乘運算,“1005010,、90502010f ( x) =100x 亠 5x 30 x 亠 10 , g(x) =150x 5x - 40 x - 20 x 亠 3x求和結(jié)果:f ( x) - g (x) = 100

2、 x100 - 150 x90 - 40 x 20 - 10 x10 3x - 102)設(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)*請選擇(0 5)*請選擇(0-5)1.2數(shù)據(jù)結(jié)構(gòu)設(shè)計根據(jù)下面給出的存儲結(jié)構(gòu)定義:3#define MAXSIZE 20/定義線性表最大容量定義多項式項數(shù)據(jù)類型系數(shù)指數(shù)線性表中數(shù)組元素

3、指向線性表中最后一個元素位置/typedef structfloat coef;/int expn;/term,elemType;typedef structterm termsMAXSIZE; / 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ù)相同的項是否存

4、在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ù)項 term2 polynomial*addPloyn(polynomial*p1,polynomial*p2)/ 將多項式 p1 和多項式 p2 相加,生成一個新的多項式 polynomial*subStractPloyn(polynomia

5、l*p1,polynomial*p2)/ 多項式 p1 和多項式 p2 相減,生成一個新的多項式 polynomial*mulitPloyn(polynomial*p1,polynomial*p2)/ 多項式 p1 和多項式 p2 相乘,生成一個新的多項式 void printPloyn(polynomial*p)/ 輸出在順序存儲結(jié)構(gòu)的多項式 p 1.4 程序源代碼 #include<stdlib.h> #include<stdio.h> #include<iostream.h> #define NULL 0 #define MAXSIZE 20typed

6、ef 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;else return 1; polynomial*Init_Polynomial()polynomial*P;P=new polyno

7、mial;if(P!=NULL)P->last=-1; return P;else return 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(i<=p->last && p->termsi.expn!=x.expn) i+;if(i>p->last) return 0;

8、else return 1;5int Insert_ElementByOrder(polynomial*p,term x)int j; if(PloynStatus(p)=-1) return 0;if(p->last=MAXSIZE-1) cout<<"The polym is full!"<<endl; return 0; j=p->last;while(p->termsj.expn<x.expn && j>=0) p->termsj+1=p->termsj; j-;p->terms

9、j+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(m>MAXSIZE) printf(" 順序表溢出 n"); return 0;elseprintf(”請依次輸入c對系數(shù)和指數(shù).n",m);for(int i=0;i<m;i+) scanf("%f%d",&coef,&expn); x.coef=coef; x.ex

10、pn=expn;if(!Location_Element(P,x) Insert_ElementByOrder(P,x);#return 1;char compare(term term1,term term2)if(term1.expn>term2.expn)return'>'else if(term1.expn<term2.expn)return'<'elsereturn'='polynomial*addPloyn(polynomial*p1,polynomial*p2)int i,j,k;i=0;j=0;k=0;if

11、(PloynStatus(p1)=-1)|(PloynStatus(p2)=-1)return NULL;polynomial*p3=Init_Polynomial();while(i<=p1->last && j<=p2->last) switch(compare(p1->termsi,p2->termsj) case'>':p3->termsk+=p1->termsi+;p3->last+;break;case'<':p3->termsk+=p2->termsj+;

12、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(i<=p1->last)p3->termsk+=p1->termsi+;p3->last+;return p3; polynomial*subStractPloyn

13、(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;i<=p2->last;i+)p3->termsi.coef=-p2->termsi.coef; p3->termsi.expn=p2->termsi.expn;p3=addPloyn(p1,p3);return p3;polynomial*mul

14、itPloyn(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;i<=p2->last;i+)for(k=0;k<=p2->last;k+) pk=Init_Polynomial(); pk->last=p1->last; for(j

15、=0;j<=p1->last;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;i<=p->last;i+)if(p->termsi.coef>0 && i>0) cout<<"+&qu

16、ot;<<p->termsi.coef;elsecout<<p->termsi.coef;cout<v"xA"vvp->termsi.exp n;cout<<endl;void menu()cout<<"tt*數(shù)據(jù)結(jié)構(gòu)綜合性實驗 *"<<endl;cout<<"tt*一、多項式的加、減、乘法運算 *"<<endlcout<<"tt*1.多項式創(chuàng)建*"<<endl;cout<<

17、;"tt*2.多項式相加*"<<endl;cout<<"tt*3.多項式相減*"<<endl;cout<<"tt*4.多項式相乘*"<<endl;cout<<"tt*5.清空多項式*"<<endl;cout<<"tt*0.退出系統(tǒng)*"<<endl;cout<<"tt*請選擇 (0-5)*"<<endl;7cout<<"tt*

18、、'<<endl;19void main()int sel;polynomial*p1=NULL;polynomial*p2=NULL;polynomial*p3=NULL;while(1)menu();cout<<"tt* 請選擇 (0-5):"cin>>sel; switch(sel) case 1: p1=Init_Polynomial();p2=Init_Polynomial();int m;printf(" 請輸入第一個多項式的項數(shù) :n");scanf("%d",&m);

19、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);brea

20、k;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_Poly no mial(pl);Reset_Poly no mial(p2);Reset_Poly no mial(p3); break;case 0:return;return;1.5程序執(zhí)行結(jié)果2. 迷宮問題實現(xiàn)2.1

21、設(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 struct int x;int y;int d; dataType;/ 定義順序棧的類型定義 typed

22、ef 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_Pat

23、h(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 鄰域方向in

24、t find_Path(int mazeM+2N+2,item move8)/在迷宮maze二維數(shù)組中按move的8鄰域方向探測迷宮路線,存在返回 1否則返回 0void print_Path(SeqStack*s)/ 輸出棧 s 中所有迷宮路徑2.4 程序源代碼 #include<stdio.h> #include<stdlib.h> #define M 6 #define N 8 #define MAXLEN 100 typedef struct int x;int y;item;typedef structint x;int y;int d;dataType; t

25、ypedef struct dataType 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() S

26、eqStack*s; s=new SeqStack;s->top=-1;return s;int Push(SeqStack*s,dataType x)if(s->top=MAXLEN-1) return 0;else s->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-&

27、gt;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("第4次入棧元素為:",+i);printf

28、("(%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(d<8)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

溫馨提示

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

最新文檔

評論

0/150

提交評論