数据结构与算法课程设计说明书--一元多项式的加法.减法的实现.docx
数据结构与算法课程设计说明书设计题目:一元多项式的加法.减法的实现1需求分析功能:首先建立一个有序链表,输入一个多项式,进行降幕排列。接着判断进行加减乘哪种运算,再输入一个多项式进行运算.2概要设计a.建立多项式链表输入m项的系数和指数,建立表示一元多项式的有序链表P。依次输入m个非零项,生成结点并插入链表.再在界面上输入n个非零项的系数和指数structterm*CreatPolyn(structterm*P,intm)/输入m项的系数和指数,建立表示一元多项式的有序链表Pinti;structterm*h-P=(structterm*)malIoc(sizeof(structterm),*q;if(m<=0)returnNULL;P->coef=0.0;Printf("依次输入%d个非零项n”,m);for(i=1;i<=m;+i)依次输入m个非零项SCanf("%f%d”,P->coef,ftP->expn);if(P->coef)q=P;P=P->next=(structterm*)malIoc(sizeof(structterm);q->next=NULL:free(P);returnh;/CreatPolynb.多项式加法利用两个多项式的结点构成“和多项式",Pa=Pa+Pb.structterm*APolyn(structterm*Pa,structterm*Pb)/多项式加法:Pa=Pa+Pb,利用两个多项式的结点构成和多项式structterm*h,*qa=Pa,*qb=Pb,*p,*q;floatsum;h=p=(structterm*)malIoc(sizeof(structterm);p->next=NULL;while(qa&&qb)(/Pa和Pb均非空switch(Compare(qa,qb)(case-1:/多项式PA中当前结点的指数值小p->next=qb;P=qb;qb=qb->next;break;caseO:/两者的指数值相等sum=qa->coef+qb->coef;if(sum!=O.O)(修改多项式PA中当前结点的系数值p->next=qa;qa->coef=sum;P=qa;qa=qa->next;)else(删除多项式PA中当前结点q=qa;qa=qa->next;free(q);)q=qb;qb=qb->next;free(q);break;case1:/多项式PB中当前结点的指数值小p->next=qa;P=qa;qa=qa->next;break;)/switch/whileif(Pa)p->next=qa;/链接Pa中剩余结点if(Pb)p->next=qb;/链接Pb中剩余结点q=h;h=h->next;free(q);returnh;/APolync.多项式减法Pa=Pa-Pb,利用两个多项式的结点构成差多项式。structterm*BPolyn(structterm*Pa,structterm*Pb)/多项式减法:Pa=Pa-Pb,利用两个多项式的结点构成差多项式。structterm*p=Pb;while(p)p->coef*=-1;p=p->next;)returnAPolyn(Pa,Pb);/BPolynd.多项式乘法多项式乘法:Pa=Pa*Pb,利用两个多项式的结点构成积多项式“。tructterm*CPolyn(structterm*Pa,structterm*Pb)/多项式乘法:Pa=Pa*Pb,利用两个多项式的结点构成积多项式。structterm*pa=Pa,*p,*q,*r,*s,*t;r=p=(structterm*)malloc(sizeof(structterm);if(!Pb)returnNULL;while(pa)(p->coef=pa->coef;p->expn=pa->expn;q=P;p=p->next=(structterm*)malIoc(sizeof(structterm);pa=pa->next;)q->next=NULL;free(p);pa=Pa;t=s=(structterm*)malloc(sizeof(structterm);while(pa)s=s->next=(structterm*)malIoc(sizeof(structterm);pa=pa->next;)q->next=NULL;free(s);pa=Pa;while(pa)(pa->coef*=Pb->coef;pa->expn+=Pb->expn;pa=pa->next;)Pb=Pb->next;While(Pb)(P=r;s=t;while(p)(s->coef=p->coef*Pb->coef;s->expn=p->expn+Pb->expn;p=p->next;s=s->next;)Pa=APolyn(Pa,t);Pb=Pb->next;)returnPa;/CPolyne.主函数建立主函数界面,实现加、减、乘主要功能。界面运算由输入第一个一元多项式开始,逐步实现多项式计算。voidmain()(structterm*M,*N;chars2;inti,n;puts元多项式计算:n输入一一元多项式的项数”);scanf("%d",&n);M=CreatPolyn(M,n);M=selsort(M);PrintfPoly(M);p:PUtS("nl:加n2:减n3:乘n4:退出“);getchar();q:gets(s);if(sl=>0,II!isdigit(*s)(PUtS("输入有误,请重新输入!”);gotoq;)i=*s-48;switch(i)case1:M=A(M,N);gotop;case2:M=B(M,N);gotop;case3:M=C(M,N);gotop;case4:break;default:PUtS("输入有误,请重新输入!");gotoq;)3详细设计ttinclude<stdlib.h>include<stdio.h>typedef struct 项的表示, float coef; int expn;struct term term;ttinclude<ctype.h>term多项式的项作为LinkList的数据元素系数指数*next;structterm*CreatPolyn(structterm*P,intm)输入m项的系数和指数,建立表示一元多项式的有序链表Pinti;structterm*h=P=(structterm*)malIoc(sizeof(structterm),*q;if(m<=0)returnNULL;P->coef=0.O;Printf(依次输入%d个非零项n”,m);for(i=1;i<=m;+i)(依次输入m个非零项scanf("%f%d”,&P->coef,P>expn);if(P->coef)q=P;P=P->next=(structterm*)malIoc(sizeof(structterm);)q->next=NULL;free(P);returnh;)/CreatPolynstructterm*selsort(structterm*h)(structterm*g,*p,*q;floatf;inti,fini=1;if(Jh)returnNULL;for(g=h;g->next&&fini;g=g->next)(fini=O;for(p=h,q=h->next;q;p=p->next,q=q->next)if(p->expn<q->expn)(f=p->coef;i=p->expn;p->coef=q->coef;p->expn=q->expn;q->coef=f;q->expn=i;fini=1;)for(g=h,p=g->next;p;)if(g->expn=p->expn)(g->coef+=p->coef;g->next=p->next;q=P;p=p->next;free(q);)elseif(g->next)(g=g->next;p=p->next;)returnh;)PrintfPoly(structterm*P)(structterm*q=P;if(!q)putchar(,0,);return;)if(q->coef!=1)(Printf("%g”,q->coef);if(q->expn=l)putchar('X');elseif(q->expn)Printf("X%d”,q->expn);)elseif(!q->expn)putchar(,1,);elseif(q->expn=l)putchar(,X,);elsePrintf(“c%d”,q->expn);q=q->next;while(q)(if(q->coef>0)putchar(,+,);if(q->coef!=1)printf('%g”,q->coef);if(q->cxpn=l)putchar(>X,);elseif(q->expn)Printf("X%d,q->expn);)elseif(!q->expn)putchar(>11);elseif(q->expn=l)putchar(,X,);elsePrintf(“c%d”,q->expn);q=q->next;)Compare(structterm*a,structterm*b)(if(a->expn<b->expn)returnT;if(a->expn>b->expn)return1;return0;)structterm*APolyn(structterm*Pa,structterm*Pb)/多项式加法:Pa=Pa+Pb,利用两个多项式的结点构成和多项式:structterm*h,*qa=Pa,*qb=Pb,*p,*q;floatsum;h=p=(structterm*)malIoc(sizeof(structterm);p->next=NULL;while(qa&&qb)Pa和Pb均非空switch(Compare(qa,qb)case-1:/多项式PA中当前结点的指数值小p->next=qb;P=qb;qb=qb->next;break;caseO:/两者的指数值相等sum=qa->coef+qb->coef;if(sum!=0.0)(修改多项式PA中当前结点的系数值p->next=qa;qa->coef=sum;P=qa;qa=qa->next;)else(删除多项式PA中当前结点q=qa;qa=qa->next;free(q);)q=qb;qb=qb->next;free(q);break;case1:/多项式PB中当前结点的指数值小p->next=qa;P=qa;qa=qa->next;break;/switch/whileif(Pa)p->next=qa;/链接Pa中剩余结点if(Pb)p->next=qb;/链接Pb中剩余结点q=h;h=h->next;free(q);returnh;)/APolynstructterm*A(structterm*Pa,structterm*Pb)(intn;PUtS(“再输入一一元多项式的项数);scanf("%d",&n);Pb=CreatPolyn(Pb,n);Pb=seisort(Pb);PrintfPoly(Pa);if(Pb&&Pb>coef>0)printf(,+");PrintfPoly(Pb);Pa=APolyn(Pa,Pb);printf=");Pa=selsort(Pa);PrintfPoly(Pa);returnPa;)structterm*BPolyn(structterm*Pa,structterm*Pb)/多项式减法:Pa=Pa-Pb,利用两个多项式的结点构成差多项式。structterm*p=Pb;while(p)p->coef*=-1;p=p->next;)returnAPolyn(Pa,Pb);/BPolynstructterm*B(structterm*Pa,structterm*Pb)intn;PUtS(再输入一一元多项式的项数);scanf("%d",&n);Pb=CreatPolyn(Pb,n);Pb=seisort(Pb);PrintfPoly(Pa);printf("一");putchar(,(,);PrintfPoly(Pb);putchar(,),);Pa=BPolyn(Pa,Pb);printf=");Pa=selsort(Pa);PrintfPoly(Pa);returnPa;)structterm*CPolyn(structterm*Pa,structterm*Pb)/多项式乘法:Pa=Pa*Pb,利用两个多项式的结点构成积多项式。structterm*pa=Pa,*p,*q,*r,*s,*t;r=p=(structterm*)malloc(sizeof(structterm);if(!Pb)returnNULL;while(pa)p->coef=pa->coef;p->expn=pa->expn;q=P;p=p->next=(structterm*)malloc(sizeof(structterm);pa=pa->next;)q->next=NULL;fre(p);pa=Pa;t=s=(structterm*)malloc(sizeof(structterm);while(pa)(q=s;s=s->next=(structterm*)malloc(sizeof(structterm);pa=pa->next;)q->next=NULL;free(s);pa=Pa;while(pa)(pa->coef*=Pb->coef;pa->expn+=Pb->expn;pa=pa->next;)Pb=Pb->next;while(Pb)(P=r;s=t;while(p)(s->coef=p->coef*Pb->coef;s->expn=p->expn+Pb->expn;p=p->next;s=s->next;)Pa=APolyn(Pa,t);Pb=Pb->next;)returnPa;)/CPolynstructterm*C(structterm*Pa,structterm*Pb)(intn;PUtS(再输入一一元多项式的项数);scanf("%d",&n);Pb=CreatPolyn(Pb,n);Pb=seisort(Pb);putchar(,(,);PrintfPoly(Pa);putchar(,),);printfC*);putchar(,(,);PrintfPoly(Pb);putchar(,),);printfC=");Pa=CPolyn(Pa,Pb);Pa=selsort(Pa);PrintfPoly(Pa);returnPa;)voidmain()(structterm*M,*N;chars2;inti,n;puts("一元多项式计算:n输入一一元多项式的项数);scanf("%d",&n);M=CreatPolyn(M,n);M=selsort(M);PrintfPoly(M);p:PUtS("nl:加n2:减n3:乘n4:退出“);getchar();q:gets(s);if(sl=>0,II!isdigit(*s)(PUtS("输入有误,请重新输入!”);gotoq;)i=*s-48;switch(i)(case1:M=A(M,N);gotop;case2:M=B(M,N);gotop;case3:M=C(M,N);gotop;case4:break;default:PUtS("输入有误,请重新输入!");gotoq;)4调试分析ICAwD:112233De¾gl12233.exe*元多项小计算:输入一一元多项式的项数2依次输入2个非零项2 43 53X5+2X4加院4:退出也输入一一元多项式的项数余次输入3个非零项4 35 46 53X5+2X4+4Xa5*3X4+2X3-7X5+5X4+2X31:加2:减4:退出2再输入一一元多项式的项数CA rD: 112233Debugl 12233. exe-ll ×l再输入元多项式的项数2依次输入2个非零项3 45 67X5+5Xa4*2X3 - <5X6*3X4> =1:加2:减3:乘4:退出再输入一一元多项式的项数余次输入2个非零项5 36 2<-5X6*7X5+2X4> * <5X3*6X2>1:加2避3谏4:退出4Press any key to continue.-5Xa6*7X5*2X4-25X9*5X8+52X7÷12X6在设计过程中主要遇到下列问题:多项式的显示方法:比如是降靠还是升鼎排列,哪种排列方式更符合人们经常的习惯。结构体的一些具体用法:结构体的应用不是很熟练,在调试中经常出错。5心得体会通过这次学习,使我了解到:学习数据结构,首先要从具体问题抽象出一个适当的数学模型,然后设计一个解此数学模型的算法,最后编写程序,进行测试、调整直至得到最终答案。经过几天的程序编写,使我对顺序结构有了更深的了解,对编程中的一些操作也更熟练了。此次学习我受益匪浅。