Code
#include<stdio.h>
#include<stdlib.h>
typedef struct NodeList
{
float a;
int e;
struct NodeList *next;
}lNodeList,*pNodeList;
lNodeList *La,*Lb;
lNodeList *attach(lNodeList *p,float a,int e)
{
lNodeList *t;
t=(lNodeList*)malloc(sizeof(lNodeList));
t->a=a;
t->e=e;
p->next=t;
return t;
}
lNodeList* Create_Poly()
{
lNodeList *H,*p;
float a;
int e;
H=p=(lNodeList*)malloc(sizeof(lNodeList));
p->e=-1;
p->a=0;
p->next=NULL;
scanf("%f%d",&a,&e);
while(e>=0)
{
p=attach(p,a,e);
scanf("%f%d",&a,&e);
}
p->next=NULL;
return H;
}
void print_Polly(lNodeList *p)
{
p=p->next;
while(p)
{
printf("(%5f,%5d) ",p->a,p->e);
p=p->next;
}
printf("\n");
}
lNodeList *Add_Poly(lNodeList *La,lNodeList *Lb)
{
lNodeList *pa,*pb,*H,*pc;
float a;
pa=La->next;
pb=Lb->next;
H=pc=(lNodeList*)malloc(sizeof(lNodeList));
pc->a=0;
pc->e=-1;
pc->next=NULL;
while(pa&&pb)
{
if(pa->e>pb->e)
{
pc=attach(pc,pa->a,pa->e);
pa=pa->next;
}
else
if(pa->e<pb->e)
{
pc=attach(pc,pb->a,pb->e);
pb=pb->next;
}
else
{
a=pa->a+pb->a;
if(a)
{
pc=attach(pc,a,pa->e);
pa=pa->next;
pb=pb->next;
}
}
}
if(pb!=NULL)
pa=pb;
while(pa)
{
pc=attach(pc,pa->a,pa->e);
pa=pa->next;
}
pc->next=NULL;
return H;
}
void Release_Poly(lNodeList *p)
{
lNodeList *t;
while(p)
{
t=p;
p=p->next;
free(t);
}
}
lNodeList *Dao_Poly(lNodeList *p)
{
lNodeList *L,*p_Dao,*t;
float a;
int e;
p=p->next;
L=p_Dao=(lNodeList*)malloc(sizeof(lNodeList));
p_Dao->e=-1;
p_Dao->a=0;
p_Dao->next=NULL;
while(p)
{
a=p->a*p->e;
e=p->e-1;
p_Dao=attach(p_Dao,a,e);
p=p->next;
}
p_Dao->next=NULL;
return L;
}
lNodeList *Mul_Poly(lNodeList *La,lNodeList *Lb)
{
lNodeList *pa,*pb,*H,*tH,*pc,*t,*tc;
pb=Lb->next;
H=pc=(lNodeList*)malloc(sizeof(lNodeList));
pc->a=0;
pc->e=-1;
pc->next=NULL;
while(pb)
{
tH=H;
tc=t=(lNodeList*)malloc(sizeof(lNodeList));
t->a=0;
t->e=-1;
t->next=NULL;
pa=La->next;
while(pa)
{
t=attach(t,pb->a*pa->a,pa->e+pb->e);
pa=pa->next;
}
t->next=NULL;
H=Add_Poly(tc,tH);
Release_Poly(tc);
Release_Poly(tH);
pb=pb->next;
}
return H;
}
main()
{
lNodeList *La,*Lb,*Lc,*Ld,*L_Dao;
printf("please input poly a\n");
La=Create_Poly();
printf("please input poly b\n");
Lb=Create_Poly();
printf("the poly a is:\n");
print_Polly(La);
printf("the poly b is:\n");
print_Polly(Lb);
Lc=Add_Poly(La,Lb);
printf("the add_poly c is:\n");
print_Polly(Lc);
Ld=Mul_Poly(La,Lb);
printf("the Mul_poly d is:\n");
print_Polly(Ld);;
L_Dao=Dao_Poly(Lc);
printf("the c's Dao_poly is:\n");
print_Polly(L_Dao);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct NodeList
{
float a;
int e;
struct NodeList *next;
}lNodeList,*pNodeList;
lNodeList *La,*Lb;
lNodeList *attach(lNodeList *p,float a,int e)
{
lNodeList *t;
t=(lNodeList*)malloc(sizeof(lNodeList));
t->a=a;
t->e=e;
p->next=t;
return t;
}
lNodeList* Create_Poly()
{
lNodeList *H,*p;
float a;
int e;
H=p=(lNodeList*)malloc(sizeof(lNodeList));
p->e=-1;
p->a=0;
p->next=NULL;
scanf("%f%d",&a,&e);
while(e>=0)
{
p=attach(p,a,e);
scanf("%f%d",&a,&e);
}
p->next=NULL;
return H;
}
void print_Polly(lNodeList *p)
{
p=p->next;
while(p)
{
printf("(%5f,%5d) ",p->a,p->e);
p=p->next;
}
printf("\n");
}
lNodeList *Add_Poly(lNodeList *La,lNodeList *Lb)
{
lNodeList *pa,*pb,*H,*pc;
float a;
pa=La->next;
pb=Lb->next;
H=pc=(lNodeList*)malloc(sizeof(lNodeList));
pc->a=0;
pc->e=-1;
pc->next=NULL;
while(pa&&pb)
{
if(pa->e>pb->e)
{
pc=attach(pc,pa->a,pa->e);
pa=pa->next;
}
else
if(pa->e<pb->e)
{
pc=attach(pc,pb->a,pb->e);
pb=pb->next;
}
else
{
a=pa->a+pb->a;
if(a)
{
pc=attach(pc,a,pa->e);
pa=pa->next;
pb=pb->next;
}
}
}
if(pb!=NULL)
pa=pb;
while(pa)
{
pc=attach(pc,pa->a,pa->e);
pa=pa->next;
}
pc->next=NULL;
return H;
}
void Release_Poly(lNodeList *p)
{
lNodeList *t;
while(p)
{
t=p;
p=p->next;
free(t);
}
}
lNodeList *Dao_Poly(lNodeList *p)
{
lNodeList *L,*p_Dao,*t;
float a;
int e;
p=p->next;
L=p_Dao=(lNodeList*)malloc(sizeof(lNodeList));
p_Dao->e=-1;
p_Dao->a=0;
p_Dao->next=NULL;
while(p)
{
a=p->a*p->e;
e=p->e-1;
p_Dao=attach(p_Dao,a,e);
p=p->next;
}
p_Dao->next=NULL;
return L;
}
lNodeList *Mul_Poly(lNodeList *La,lNodeList *Lb)
{
lNodeList *pa,*pb,*H,*tH,*pc,*t,*tc;
pb=Lb->next;
H=pc=(lNodeList*)malloc(sizeof(lNodeList));
pc->a=0;
pc->e=-1;
pc->next=NULL;
while(pb)
{
tH=H;
tc=t=(lNodeList*)malloc(sizeof(lNodeList));
t->a=0;
t->e=-1;
t->next=NULL;
pa=La->next;
while(pa)
{
t=attach(t,pb->a*pa->a,pa->e+pb->e);
pa=pa->next;
}
t->next=NULL;
H=Add_Poly(tc,tH);
Release_Poly(tc);
Release_Poly(tH);
pb=pb->next;
}
return H;
}
main()
{
lNodeList *La,*Lb,*Lc,*Ld,*L_Dao;
printf("please input poly a\n");
La=Create_Poly();
printf("please input poly b\n");
Lb=Create_Poly();
printf("the poly a is:\n");
print_Polly(La);
printf("the poly b is:\n");
print_Polly(Lb);
Lc=Add_Poly(La,Lb);
printf("the add_poly c is:\n");
print_Polly(Lc);
Ld=Mul_Poly(La,Lb);
printf("the Mul_poly d is:\n");
print_Polly(Ld);;
L_Dao=Dao_Poly(Lc);
printf("the c's Dao_poly is:\n");
print_Polly(L_Dao);
}