C語言實現多項式除法

來源:互聯網
上載者:User

這是一個C語言實現多項式除法的代碼執行個體,多項式除法和多項式加減乘不同,比較難以實現,所以一直是各個教材和老師避諱的,故小可今天現醜將其演算法和代碼貼出,演算法和效率上我也不甚滿意,雖不免遺笑於方家,但本著學習進步的原則,希望能得到諸君點撥。

大概描述:用被除數的最大項除以除數最大項,然後用這個商遍乘除數,其間要申請式子的拷貝,然後被除數再減遍乘過後的那個積,差又是下一輪的被除數,如此直到最後被除數冪小於除數,可能說的不很清楚,全部代碼如下(通過turboc調試成功):

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
typedef struct LinkList{
float coef;
int expn;
struct LinkList* next;
}LinkList,*PLink;
void AddPolyn(PLink *p1,PLink p2);
void SubPolyn(PLink *p1,PLink p2);
void MulPolyn(PLink *p1,PLink p2);
int cmp(int,int);
void Del(PLink*,PLink);
void Ins(PLink*,PLink);
void Append(PLink*,PLink);
void DevPolyn(PLink*,PLink*);
void Destroy(PLink*);
PLink GetMax(PLink);
void CreatePolyn(PLink *p)
{
int m;
float b;
PLink s;
printf("\nHow many polynomial:");
scanf("%d",&m);
printf("Enter the expn from large to small:");
for(m;m>0;m--)
{
s=(PLink)malloc(sizeof(LinkList));
printf("\nEnter the coef:");
scanf("%f",&b);
s->coef=b;
printf("\nEnter the expn:");
scanf("%d",&(s->expn));
s->next=(*p)->next;
(*p)->next=s;
}
}
void PrintPolyn(PLink p)
{
PLink q=p->next;
while(q!=0)
{
printf("%3.1fx^%d+",q->coef,q->expn);
q=q->next;
}
if(q==p->next)
printf("0");
}
main()
{
PLink p1=(PLink)malloc(sizeof*p1);
PLink p2=(PLink)malloc(sizeof*p2);
p1->next=p2->next=0;
p1->coef=p1->expn=p2->coef=p2->expn=0;
printf("\nBuild the p1\n");
CreatePolyn(&p1);
PrintPolyn(p1);
printf("\nBuild the p2\n");
CreatePolyn(&p2);
PrintPolyn(p2);
DevPolyn(&p1,&p2);printf("\n");
PrintPolyn(p1);
printf("\nthe surplus is:\n");
PrintPolyn(p2);
Destroy(&p1);
Destroy(&p2);
getch();
}
void AddPolyn(PLink *p1,PLink p2)//加法
{
PLink qa=(*p1)->next,ha=*p1;
PLink qb=p2->next,hb;
int a,b;
while(qa&&qb)
{
a=qa->expn;
b=qb->expn;
switch(cmp(a,b))
{
case -1:
ha=qa;qa=qa->next;break;
case 0:
qa->coef+=qb->coef;
if(qa->coef!=0)ha=qa;
else Del(&ha,qa);
qa=ha->next;
qb=qb->next;break;
case 1:
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);
}
void SubPolyn(PLink *p1,PLink p2)
{
PLink qa=(*p1)->next,ha=*p1;
PLink qb=p2->next,hb;
int a,b;
while(qa&&qb)
{
a=qa->expn;
b=qb->expn;
switch(cmp(a,b))
{
case -1:
ha=qa;qa=qa->next;break;
case 0:
qa->coef-=qb->coef;
if(qa->coef!=0)ha=qa;
else Del(&ha,qa);
qa=ha->next;
qb=qb->next;break;
case 1:
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;
}
}
void MulPolyn(PLink *p1,PLink p2)
{
PLink temp,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);
}
void DevPolyn(PLink *p1,PLink *p2)
{
PLink res2,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);
AddPolyn(&temp1,*p2);//temp1是p2的一個拷貝,p2在此期間一直不變
q=temp1->next;
Ins(&res2,temp2);
while(q!=0)//除數的遍乘
{
q->coef*=temp2->coef;
q->expn+=temp2->expn;
q=q->next;
}
SubPolyn(p1,temp1);//被除數減去遍乘後的積
Destroy(&temp1);
}
temp1=*p1;
temp2=*p2;
*p1=res2;*p2=temp1;
Destroy(&temp2);
}
int cmp(int a,int b)
{
if(a>b)return 1;
if(a<b)return -1;
return 0;
}
void Del(PLink *p,PLink pa)
{
(*p)->next=pa->next;
free(pa);
}
void Ins(PLink *p,PLink pa)
{
pa->next=(*p)->next;
(*p)->next=pa;
}
void Append(PLink *p,PLink pa)
{
PLink temp,end=*p;
while(pa!=0)
{
temp=(PLink)malloc(sizeof(LinkList));
temp->coef=pa->coef;
temp->expn=pa->expn;
temp->next=0;
end->next=temp;
end=temp;
pa=pa->next;
}
}
void Destroy(PLink *p)//注意,這裡destroy將free迴轉結點
{
while((*p)->next!=0)
Del(p,(*p)->next);
free(*p);
}
PLink GetMax(PLink p)
{
PLink q=p->next;
if(q!=0)
while(q->next!=0)
q=q->next;
else
return 0;
return q;
}

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。