This is a C-language implementation of polynomial division code example, polynomial division and polynomial multiplication and subtraction, more difficult to achieve, so has been a variety of textbooks and teachers taboo, so small Hong today will its algorithm and code posted, algorithm and efficiency I am not very satisfied, although not to laugh at the side, but the spirit of the principle of progress in learning, Hope to get the help of the gentlemen.
Description: By dividing the maximum term of dividend by the maximum of the divisor, and then use this quotient multiplication and division number, in the meantime to apply for the copy of the formula, and then by the divisor again after the multiplication, the next round of dividend, so until the end of the divisor is less than divisor, may not be very clear, all the code as follows ( Successful through TURBOC debugging):
#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);p rintf ("\ n");
Printpolyn (p1);
printf ("\nthe surplus is:\n");
Printpolyn (p2);
Destroy (&P1);
Destroy (&P2);
Getch ();
}
void Addpolyn (Plink *p1,plink p2)/addition
{
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)//NOTE: Here is a copy, not a link change.
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 for temporary storage
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 is a copy of P2, P2 has remained unchanged during this period.
q=temp1->next;
Ins (&RES2,TEMP2);
while (q!=0)//multiplication of divisor
{
q->coef*=temp2->coef;
q->expn+=temp2->expn;
q=q->next;
}
Subpolyn (P1,TEMP1);//dividend minus the product after multiplication
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)//note, here Destroy will free U-turn node
{
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;
}