顺序链式一元多项式加法、减法、乘法运算的实现.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流顺序链式一元多项式加法、减法、乘法运算的实现.精品文档.1.1设计内容及要求1)设计内容(1)使用顺序存储结构实现多项式加、减、乘运算。例如:求和结果:(2)使用链式存储结构实现多项式加、减、乘运算,求和结果:2)设计要求(1)用C语言编程实现上述实验内容中的结构定义和算法。(2)要有main()函数,并且在main()函数中使用检测数据调用上述算法。(3)用switch语句设计如下选择式菜单。 *数据结构综合性实验* *一、多项式的加法、减法、乘法运算* * 1.多项式创建 * * 2.多项式相加 * * 3.多项式相减 * 4.多项式相乘 * 5.清空多项式 * 0.退出系统 * 请选择(05) *请选择(0-5):1.2数据结构设计根据下面给出的存储结构定义:#define MAXSIZE 20 /定义线性表最大容量 /定义多项式项数据类型typedef structfloat coef; /系数int expn; /指数 term,elemType;typedef structterm termsMAXSIZE; /线性表中数组元素int last; /指向线性表中最后一个元素位置 SeqList;typedef SeqList polynomial;1.3基本操作函数说明 polynomial*Init_Polynomial();/初始化空的多项式int PloynStatus(polynomial*p)/判断多项式的状态 int Location_Element(polynomial*p,term x)在多项式p中查找与x项指数相同的项是否存在int Insert_ElementByOrder(polynomial*p,term x)/在多项式p中插入一个指数项xint CreatePolyn(polynomial*P,int m)/输入m项系数和指数,建立表示一元多项式的有序表pchar compare(term term1,term term2)/比较指数项term1和指数项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)/输出在顺序存储结构的多项式p1.4程序源代码#include<stdlib.h>#include<stdio.h>#include<iostream.h>#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(i<=p->last && p->termsi.expn!=x.expn)i+;if(i>p->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)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->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(m>MAXSIZE)printf("顺序表溢出n");return 0;elseprintf("请依次输入%d对系数和指数.n",m);for(int i=0;i<m;i+)scanf("%f%d",&coef,&expn);x.coef=coef;x.expn=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(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+;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(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*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;i<=p2->last;i+)for(k=0;k<=p2->last;k+)pk=Init_Polynomial();pk->last=p1->last;for(j=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<<"+"<<p->termsi.coef;elsecout<<p->termsi.coef;cout<<"x"<<p->termsi.expn;cout<<endl;void menu()cout<<"tt*数据结构综合性实验*"<<endl;cout<<"tt*一、多项式的加、减、乘法运算*"<<endl;cout<<"tt* 1.多项式创建 *"<<endl;cout<<"tt* 2.多项式相加 *"<<endl;cout<<"tt* 3.多项式相减 *"<<endl;cout<<"tt* 4.多项式相乘 *"<<endl;cout<<"tt* 5.清空多项式 *"<<endl;cout<<"tt* 0.退出系统 *"<<endl;cout<<"tt* 请选择(0-5) *"<<endl;cout<<"tt*"<<endl;void 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("请输入第一个多项式的项数:n");scanf("%d",&m);CreatePolyn(p1,m);printf("第一个多项式的表达式为p1=");printPloyn(p1);printf("请输入第二个多项式的项数: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程序执行结果