《数据结构课程设计报告—一元多项式加法、减法、乘法运算的实现》由会员分享,可在线阅读,更多相关《数据结构课程设计报告—一元多项式加法、减法、乘法运算的实现(24页珍藏版)》请在金锄头文库上搜索。
1、1.一元多项式加法、减法、乘法运算的实现1.1设计容及要求1)设计容1使用顺序存储构造实现多项式加、减、乘运算例如:65425432f(x)8x5x10x32xx10,g(x)7x10x20x10xx求和结果:f(x)g(x)8x612x520x322x2102使用链式存储构造实现多项式加、减、乘运算,100501090502010f(x)100x1005x5030x1010,g(x)150x905x5040x2020x103x求和结果:f(x)g(x)100x100150x9040x2010x103x102设计要求1用C语言编程实现上述实验容中的构造定义和算法。2要有main()函数,并且在
2、main()函数中使用检测数据调用上述算法。3用switch语句设计如下选择式菜单。*数据构造综合性实验*、多项式的加法、减法、乘法运算*1.多项式创立*2.多项式相加*3.多项式相减*4.多项式相乘*5.清空多项式*0.退出系统*请选择0-51.2数据构造设计根据下面给出的存储构造定义:/定义线性表最大容量/定义多项式项数据类型/系数/指数#defineMAXSIZE20typedefstructfloatcoef;intexpn;term,elemType;typedefstruct/线性表中数组元素/指向线性表中最后一个元素位置termtermsMAXSIZE;intlast;SeqLi
3、st;typedefSeqListpolynomial;1.3根本操作函数说明polynomial*Init_Polynomial();/初始化空的多项式intPloynStatus(polynomial*p)/判断多项式的状态intLocation_Element(polynomial*p,termx)在多项式p中查找与x项指数一样的项是否存在intInsert_ElementByOrder(polynomial*p,termx)/在多项式p中插入一个指数项xintCreatePolyn(polynomial*P,intm)/输入m项系数和指数,建立表示一元多项式的有序表pcharcompa
4、re(termterm1,termterm2)/比拟指数项term1和指数项term2polynomial*addPloyn(polynomial*p1,polynomial*p2)/将多项式p1和多项式p2相加,生成一个新的多项式polynomial*subStractPloyn(polynomial*p1,polynomial*p2)/多项式pl和多项式p2相减,生成一个新的多项式polynomial*mulitPloyn(polynomial*p1,polynomial*p2)/多项式pl和多项式p2相乘,生成一个新的多项式voidprintPloyn(polynomial*p)/输出在
5、顺序存储构造的多项式p1.4程序源代码#include#include#include#defineNULL0#defineMAXSIZE20typedefstructfloatcoef;intexpn;term,elemType;typedefstructtermtermsMAXSIZE;intlast;SeqList;typedefSeqListpolynomial;voidprintPloyn(polynomial*p);intPloynStatus(polynomial*p)if(p=NULL)return-1;elseif(p-last=-1)return0;elsereturn1;
6、polynomial*Init_Polynomial()polynomial*P;P=newpolynomial;if(P!=NULL)P-last=-1;returnP;elsereturnNULL;voidReset_Polynomial(polynomial*p)if(PloynStatus(p)=1)p-last=-1;intLocation_Element(polynomial*p,termx)inti=0;if(PloynStatus(p)=-1)return0;while(ilast&p-termsi.expn!=x.expn)if(ip-last)return0;elseret
7、urn1;intInsert_ElementByOrder(polynomial*p,termx)intj;if(PloynStatus(p)=-1)return0;if(p-last=MAXSIZE-1)coutThepolymisfull!last;while(p-termsj.expn=0)p-termsj+1=p-termsj;j-;p-termsj+1=x;p-last+;return1;intCreatePolyn(polynomial*P,intm)floatcoef;intexpn;termx;if(PloynStatus(P)=-1)return0;if(mMAXSIZE)p
8、rintf(顺序表溢出n);return0;elseprintf(请依次输入%d对系数和指数.n,m);for(inti=0;iterm2.expn)return;elseif(term1.expnterm2.expn)return;elsereturn=;polynomial*addPloyn(polynomial*p1,polynomial*p2)inti,j,k;i=0;j=0;k=0;if(PloynStatus(p1)=-1)|(PloynStatus(p2)=-1)returnNULL;polynomial*p3=Init_Polynomial();while(ilast&jlas
9、t)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+;retur
10、np3;polynomial*subStractPloyn(polynomial*p1,polynomial*p2)inti;i=0;if(PloynStatus(p1)!=1)|(PloynStatus(p2)!=1)returnNULL;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);returnp3;polynomial*mulitPloyn(pol
11、ynomial*p1,polynomial*p2)inti;intj;intk;i=0;if(PloynStatus(p1)!=1)|(PloynStatus(p2)!=1)returnNULL;polynomial*p3=Init_Polynomial();polynomial*p=newpolynomial*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;
12、pk-termsj.expn=p1-termsj.expn+p2-termsk.expn;p3=addPloyn(p3,pk);returnp3;voidprintPloyn(polynomial*p)inti;for(i=0;ilast;i+)if(p-termsi.coef0&i0)cout+termsi.coef;elsecouttermsi.coef;couttermsi.expn;coutendl;voidmenu()couttt*数据构造综合性实验*endl;couttt*1.多项式创立*endl;couttt*2.多项式相加*Wn1couttt*3.多项式相减*Wn1couttt*4.多项式相乘*Wnz*l1couttt*5.清空多项式*endl;couttt*