一元多项式运算c语言版.docx
- 文档编号:5956247
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:16
- 大小:16.69KB
一元多项式运算c语言版.docx
《一元多项式运算c语言版.docx》由会员分享,可在线阅读,更多相关《一元多项式运算c语言版.docx(16页珍藏版)》请在冰豆网上搜索。
一元多项式运算c语言版
#include
#include
#include
#defineOK1;
#defineERROR0;
#defineTURE1;
#defineFALSE0;
#defineMAXSIZE100;
typedefstruct{
floatcoef;
intexpn;
}term,elemtype;
typedefstructLNode{
elemtypedata;
structLNode*next;
}List,*LinkList;
typedefLinkListpolynomail;
intInitlist(LinkList*p){
*p=(LinkList)malloc(sizeof(List));
(*p)->next=NULL;
returnOK;
}
intcmp(terma,termb){
if(a.expn elseif(a.expn==b.expn)return0; elsereturn1; } intLocateElem(LinkList*p,elemtypee,LinkList*q){ if(*p==NULL)returnOVERFLOW; *q=(*p)->next; if((*q)==NULL){ *q=*p; returnERROR; } while((*q)! =NULL){ if(cmp(e,(*q)->data)==0)break; else*q=(*q)->next; } if((*q)! =NULL)returnTURE; if((*q)==NULL){ returnFALSE; } } intMakeNode(LinkList*s,elemtypee){ if(! (*s))returnOVERFLOW; (*s)->data=e; (*s)->next=NULL; returnOK; } voidInsFirst(LinkList*q,LinkList*s){ (*s)->next=(*q)->next; (*q)->next=*s; } intDelFirst(LinkList*q,LinkList*s){ LinkListp; p=*s; (*q)->next=p->next; returnOK; } voidFreeNode(LinkList*p){ LinkListq; q=*p; free(q); } LinkListNextPos(LinkList*p,LinkList*q){ return(*q)->next; } voidSetCurElem(polynomail*p,floatm){ ((*p)->data).coef=m; } intListEmpty(LinkListp){ if(p->next==NULL)returnTURE; if(p->next! =NULL)returnFALSE; } voidAppend(LinkList*p,LinkList*s){ LinkListq; q=*p; while(q->next! =NULL)q=q->next; q->next=*s; } voidSortPolyn(polynomail*p){ polynomailq,s,t,m,n,x,y,x1,x2; q=*p; Initlist(&m); m->data.coef=0.0; m->data.expn=-1; n=m; s=q; y=s->next; t=y->next; x=q->next; x1=s; x2=t; while(x2! =NULL){ while(t! =NULL){ if(cmp(x->data,y->data)<=0){ s=s->next; y=y->next; t=t->next; } else{ x=y; x1=s; x2=t; } } if(cmp(x->data,y->data)>0){ x=y; x1=s; x2=t; } x1->next=x2; n->next=x; x->next=NULL; n=n->next; x1=q; x=q->next; x2=x->next; s=x1; y=x; t=x2; } n->next=x; x->next=NULL; q->next=NULL; free(q); *p=m; } voidCreatePolyn(polynomail*p,intm){ polynomailh,q[100],s[100],x; inti; elemtypee; elemtypet[100]; h=*p; e.coef=0.0; e.expn=-1; h->data=e; h->next=NULL; q[0]=h; Initlist(&x); for(i=0;i Initlist(&s[i]); printf("pleaseinputthecoef: theexpn: \n"); scanf("%f%d",&t[i].coef,&t[i].expn); if(! LocateElem(p,t[i],&x)){ if(MakeNode(&s[i],t[i]))InsFirst(&q[i],&s[i]); q[i+1]=s[i]; } elseq[i+1]=q[i]; } SortPolyn(p); } voidPrintPolyn(polynomailq){ q=q->next; if((q->data).coef>0.0&&(q->data).expn! =0)printf("%.4f*x^(%d)",(q->data).coef,(q->data).expn); elseif((q->data).expn==0)printf("%.4f",(q->data).coef); elseprintf("%.4f*x^(%d)",(q->data).coef,(q->data).expn); q=q->next; while(q! =NULL){ if((q->data).coef>0.0&&(q->data).expn! =0)printf("+%.4f*x^(%d)",(q->data).coef,(q->data).expn); elseif((q->data).expn==0&&(q->data).coef>0.0)printf("+%.4f",(q->data).coef); elseif((q->data).expn==0&&(q->data).coef<0.0)printf("%.4f",(q->data).coef); elseprintf("%.4f*x^(%d)",(q->data).coef,(q->data).expn); q=q->next; } printf("\n"); } voidAddPolyn(polynomail*pa,polynomail*pb){ polynomailha,hb,qa,qb; ha=*pa; hb=*pb; qa=NextPos(pa,&ha); qb=NextPos(pb,&hb); elemtypea,b; floatsum; while(qa&&qb){ a=qa->data; b=qb->data; switch(cmp(a,b)){ case-1: ha=qa; qa=NextPos(pa,&qa); break; case0: sum=a.coef+b.coef; if(sum! =0.0){ SetCurElem(&qa,sum); ha=qa; } else{ DelFirst(&ha,&qa); FreeNode(&qa); } DelFirst(&hb,&qb); FreeNode(&qb); qb=NextPos(pb,&hb); qa=NextPos(pa,&ha); break; case1: DelFirst(&hb,&qb); InsFirst(&ha,&qb); qb=NextPos(pb,&hb); ha=NextPos(pa,&ha); break; } } if(! ListEmpty(*pb))Append(pa,&qb); FreeNode(&hb); } voidSubStractPolyn(polynomail*pa,polynomail*pb){ polynomailha,hb,qa,qb; ha=*pa; hb=*pb; qa=NextPos(pa,&ha); qb=NextPos(pb,&hb); elemtypea,b; floatsum; while(qb! =NULL){ qb->data.coef=-qb->data.coef; qb=qb->next; } qb=NextPos(pb,&hb); while(qa&&qb){ a=qa->data; b=qb->data; switch(cmp(a,b)){ case-1: ha=qa; qa=NextPos(pa,&qa); break; case0: sum=a.coef+b.coef; if(sum! =0.0){ SetCurElem(&qa,sum); ha=qa; } else{ DelFirst(&ha,&qa); FreeNode(&qa); } DelFirst(&hb,&qb); FreeNode(&qb); qb=NextPos(pb,&hb); qa=NextPos(pa,&ha); break; case1: DelFirst(&hb,&qb); InsFirst(&ha,&qb); qb=NextPos(pb,&hb); ha=NextPos(pa,&ha); break; } } if(! ListEmpty(*pb))Append(pa,&qb); FreeNode(&hb); } doubleEvaluatePolyn(polynomailp,floatx){ polynomailq; doubley=0.0; q=p->next; while(q! =NULL){ y+=(q->data.coef)*pow(x,q->data.expn); q=q->next; } returny; } voidDestroyPolyn(polynomail*p){ free(*p); } voidClearPolyn(polynomail*p){ polynomailq; q=*p; while(*p! =NULL){ *p=(*p)->next; free(q); q=*p; } } intInsertPolyn(polynomail*q,polynomail*s){ while((*q)->next! =NULL){ switch(cmp((*q)->next->data,(*s)->data)){ case-1: *q=(*q)->next; break; case0: (*q)->data.coef+=(*s)->data.coef; returnOK; case1: (*s)->next=(*q)->next; (*q)->next=*s; returnOK; } (*q)->next=*s; returnOK; } } intDeletePolyn(polynomail*q,elemtypex){ polynomailt; while((*q)->next! =NULL){ if(((*q)->next->data.coef==x.coef)&&((*q)->next->data.expn==x.expn)){ t=(*q)->next; (*q)->next=t->next; free(t); returnOK; } else*q=(*q)->next; } if((*q)->next==NULL)returnFALSE; } intChangePolyn(polynomail*p,elemtypex,elemtypey){ polynomails; MakeNode(&s,y); DeletePolyn(p,x); InsertPolyn(p,&s); returnOK; } voidDifferentialPolyn(polynomail*p,intn){ polynomailq,s; inti; q=*p; s=q->next; for(i=0;i while(s! =NULL){ if(s->data.expn! =0){ s->data.coef*=s->data.expn; s->data.expn--; q=q->next; s=q->next; } else{ q->next=s->next; s=q->next; } } q=*p; s=q->next; } } voidMultiplyPolyn(polynomail*pa,polynomail*pb){ intn=0,i; polynomails,t,q[100]; s=*pb; while(s->next! =NULL){ n++; s=s->next; } s=(*pb)->next; for(i=0;i q[i]=*pa; t=q[i]->next; while(t! =NULL){ t->data.coef*=s->data.coef; t->data.expn+=s->data.expn; t=t->next; } s=s->next; } for(i=1;i AddPolyn(&q[0],&q[i]); } *pa=q[0]; } voidIntegratePolyn(polynomail*p){ polynomailq,s; q=*p; s=q->next; while(s! =NULL){ s->data.coef/=s->data.expn+1; s->data.expn++; q=q->next; s=q->next; } } doubleDefiniteIntegralPolyn(polynomail*p,floatx,floaty){ IntegratePolyn(p); doublef; f=EvaluatePolyn(*p,y)-EvaluatePolyn(*p,x); returnf; } voidInvolutionPolyn(polynomail*p,intn){ polynomailq[100]; inti; for(i=0;i for(i=1;i *p=q[0]; } voidDivisionPolyn(polynomail*p,polynomail*q){ polynomails,t,x,q1,q2,rest; intn,m; s=*p; t=*q; while(s->next! =NULL)s=s->next; while(t->next! =NULL)t=t->next; m=s->data.expn; n=t->data.expn; Initlist(&x); x->data.coef=0.0; x->data.expn=-1; Initlist(&rest); rest->data.coef=0.0; rest->data.expn=-1; while(m! =0&&m>=n){ Initlist(&q1); q1->data.coef=0.0; q1->data.expn=-1; AddPolyn(&q1,q); x->data.coef=s->data.coef/(t->data.coef); x->data.expn=s->data.expn-t->data.expn; InsFirst(&rest,&x); q2=(*q)->next; while(q2! =NULL){ q2->data.coef*=x->data.coef; q2->data.expn+=x->data.expn; q2=q2->next; } SubStractPolyn(p,&q1); DestroyPolyn(&q1); s=*p; t=*q; while(s->next! =NULL)s=s->next; while(t->next! =NULL)t=t->next; m=s->data.expn; n=t->data.expn; } q1=*p; q2=*q; *p=rest; *q=q1; DestroyPolyn(&q2); } voidmain(){ polynomailp,q,p1,q1; intm,n,t; Initlist(&p); Initlist(&q); Initlist(&p1); Initlist(&q1); printf("pleaseinputthelengthofpolynomail: \n"); scanf("%d",&m); CreatePolyn(&p,m); CreatePolyn(&q,m); PrintPolyn(p); PrintPolyn(q); AddPolyn(&p,&q); PrintPolyn(p); printf("pleaseinputn=\n"); scanf("%d",&n); DifferentialPolyn(&p,n); PrintPolyn(p); IntegratePolyn(&p); PrintPolyn(p); printf("pleaseinputthelengthofpolynomail: \n"); scanf("%d",&t); CreatePolyn(&p1,t); CreatePolyn(&q1,t); PrintPolyn(p1); PrintPolyn(q1); DivisionPolyn(&p1,&q1); PrintPolyn(p1); PrintPolyn(q1); printf("%lf\n",EvaluatePolyn(p,3.0)); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一元 多项式 运算 语言版
![提示](https://static.bdocx.com/images/bang_tan.gif)