C语言实现多项式除法
2010-04-30 12:01:29 来源:WEB开发网这是一个C语言实现多项式除法的代码实例,多项式除法和多项式加减乘不同,比较难以实现,所以一直是各个教材和老师避讳的,故小可今天现丑将其算法和代码贴出,算法和效率上我也不甚满意,虽不免遗笑于方家,但本着学习进步的原则,希望能得到诸君点拨。
大概描述:用被除数的最大项除以除数最大项,然后用这个商遍乘除数,其间要申请式子的拷贝,然后被除数再减遍乘过后的那个积,差又是下一轮的被除数,如此直到最后被除数幂小于除数,可能说的不很清楚,全部代码如下(通过turboc调试成功):
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
typedefstructLinkList{
floatcoef;
intexpn;
structLinkList*next;
}LinkList,*PLink;
voidAddPolyn(PLink*p1,PLinkp2);
voidSubPolyn(PLink*p1,PLinkp2);
voidMulPolyn(PLink*p1,PLinkp2);
intcmp(int,int);
voidDel(PLink*,PLink);
voidIns(PLink*,PLink);
voidAppend(PLink*,PLink);
voidDevPolyn(PLink*,PLink*);
voidDestroy(PLink*);
PLinkGetMax(PLink);
voidCreatePolyn(PLink*p)
{
intm;
floatb;
PLinks;
printf(" Howmanypolynomial:");
scanf("%d",&m);
printf("Entertheexpnfromlargetosmall:");
for(m;m>0;m--)
{
s=(PLink)malloc(sizeof(LinkList));
printf(" Enterthecoef:");
scanf("%f",&b);
s->coef=b;
printf(" Entertheexpn:");
scanf("%d",&(s->expn));
s->next=(*p)->next;
(*p)->next=s;
}
}
voidPrintPolyn(PLinkp)
{
PLinkq=p->next;
while(q!=0)
{
printf("%3.1fx^%d+",q->coef,q->expn);
q=q->next;
}
if(q==p->next)
printf("0");
}
main()
{
PLinkp1=(PLink)malloc(sizeof*p1);
PLinkp2=(PLink)malloc(sizeof*p2);
p1->next=p2->next=0;
p1->coef=p1->expn=p2->coef=p2->expn=0;
printf(" Buildthep1 ");
CreatePolyn(&p1);
PrintPolyn(p1);
printf(" Buildthep2 ");
CreatePolyn(&p2);
PrintPolyn(p2);
DevPolyn(&p1,&p2);printf(" ");
PrintPolyn(p1);
printf(" thesurplusis: ");
PrintPolyn(p2);
Destroy(&p1);
Destroy(&p2);
getch();
}
voidAddPolyn(PLink*p1,PLinkp2)//加法
{
PLinkqa=(*p1)->next,ha=*p1;
PLinkqb=p2->next,hb;
inta,b;
while(qa&&qb)
{
a=qa->expn;
b=qb->expn;
switch(cmp(a,b))
{
case-1:
ha=qa;qa=qa->next;break;
case0:
qa->coef+=qb->coef;
if(qa->coef!=0)ha=qa;
elseDel(&ha,qa);
qa=ha->next;
qb=qb->next;break;
case1:
hb=(PLink)malloc(sizeof*hb);//注意:这里是加入一个拷贝,而不是改变一个链接
hb->coef=qb->coef;
hb->expn=qb->expn;
Ins(&ha,hb);
qb=qb->next;
ha=ha->next;
break;
}
}
if(qb!=0)Append(&ha,qb); [Page]
}
voidSubPolyn(PLink*p1,PLinkp2)
{
PLinkqa=(*p1)->next,ha=*p1;
PLinkqb=p2->next,hb;
inta,b;
while(qa&&qb)
{
a=qa->expn;
b=qb->expn;
switch(cmp(a,b))
{
case-1:
ha=qa;qa=qa->next;break;
case0:
qa->coef-=qb->coef;
if(qa->coef!=0)ha=qa;
elseDel(&ha,qa);
qa=ha->next;
qb=qb->next;break;
case1:
hb=(PLink)malloc(sizeof*hb);
hb->coef=-1*qb->coef;
hb->expn=qb->expn;
Ins(&ha,hb);
qb=qb->next;
ha=ha->next;
break;
}
}
if(qb!=0)Append(&ha,qb);
ha=ha->next;
while(ha!=0)
{
ha->coef*=-1;
ha=ha->next;
}
}
voidMulPolyn(PLink*p1,PLinkp2)
{
PLinktemp,res,qa,qb=p2->next;
res=(PLink)malloc(sizeof*res);
res->coef=res->expn=0;
res->next=0;
while(qb!=0)
{
temp=(PLink)malloc(sizeof*temp);
temp->coef=temp->expn=0;
temp->next=0;
AddPolyn(&temp,*p1);
qa=temp->next;
while(qa!=0)
{
qa->coef*=qb->coef;
qa->expn+=qb->expn;
qa=qa->next;
}
AddPolyn(&res,temp);
Destroy(&temp);
qb=qb->next;
}
temp=*p1;
*p1=res;
Destroy(&temp);
}
voidDevPolyn(PLink*p1,PLink*p2)
{
PLinkres2,temp1,temp2,q;
res2=(PLink)malloc(sizeof(*res2));//res2用来暂存商
res2->coef=res2->expn=0;res2->next=0;
while(GetMax(*p1)!=0&&GetMax(*p1)->expn>=GetMax(*p2)->expn)
{
temp2=(PLink)malloc(sizeof*temp2);
temp1=(PLink)malloc(sizeof(LinkList));
temp1->coef=temp1->expn=0;
temp1->next=0;
temp2->coef=(GetMax(*p1)->coef)/(GetMax(*p2)->coef);
temp2->expn=(GetMax(*p1)->expn)-(GetMax(*p2)->expn);
更多精彩
赞助商链接