#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct
{
float coef;
int expn;
}term,ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*Link,*Position;
typedef struct
{
Link head,tail;
int len;
}LinkList;
typedef LinkList Polynomial;
Status InitList(LinkList *L)
{
Link p;
p=(Link)malloc(sizeof(LNode));
if(!p) exit(OVERFLOW);
p->next=NULL;
(*L).head=(*L).tail=p;
(*L).len=0;
return OK;
}
Position GetHead(LinkList L)
{
return L.head;
}
void SetCurElem(Link p,ElemType e)
{
p->data=e;
}
Status MakeNode(Link *p,ElemType e)
{
(*p)=(Link)malloc(sizeof(LNode));
if(!(*p)) exit(OVERFLOW);
(*p)->data=e;
(*p)->next=NULL;
return OK;
}
void InsFirst(LinkList *L,Link h,Link s)
{
s->next=h->next;
h->next=s;
if(h==(*L).tail)
(*L).tail=h->next;
(*L).len++;
}
void CreatPolyn(Polynomial *p,int m)
{
InitList(p);
Link h=GetHead(*p);
Link q;
ElemType e;
e.coef=0.0;
e.expn=-1;
SetCurElem(h,e);
int i;
for(i=1;i<=m;i++)
{
scanf("%f%d",&e.coef,&e.expn);
MakeNode(&q,e);
InsFirst(p,h,q);
h=h->next;
}
}
void PrintPolyn(Polynomial p)
{
Link q=p.head->next;
int i;
for(i=1;i<=p.len;i++)
{
if(i==1)
printf("%g",q->data.coef); //%g表示是实数,不会像%g那样小数点后会有很多0
else
{
if(q->data.coef>0)
{
printf("+");
printf("%g",q->data.coef);
}
else
{
printf("-");
printf("%g",-q->data.coef);
}
}
if(q->data.expn)
{
printf("x");
if(q->data.expn!=1)
printf("^%d",q->data.expn);
}
q=q->next;
}
printf("\n");
}
int compare(ElemType e1,ElemType e2)
{
if(e1.expn>e2.expn) return 1;
else if(e1.expn<e2.expn) return -1;
else if(e1.expn==e2.expn) return 0;
}
Position NextPos(LinkList L,Link p)
{
return p->next;
}
ElemType GetCurElem(Link p)
{
return p->data;
}
void FreeNode(Link *p)
{
free(*p);
(*p)->next=NULL;
}
Status DelFirst(LinkList *L,Link h,Link *q)
{
*q=h->next;
if(*q)
{
h->next=(*q)->next;
if(!h->next)
(*L).tail=h;
(*L).len--;
return OK;
}
return ERROR;
}
void Append(LinkList *L,Link s)
{
int count=0;
(*L).tail->next=s;
while(s)
{
(*L).tail=s;
s=s->next;
count++;
}
(*L).len+=count;
}
void AddPolyn(Polynomial *p1,Polynomial *p2)
{
Link ha,hb,pa,pb;
ha=GetHead(*p1);
hb=GetHead(*p2);
pa=NextPos(*p1,ha);
pb=NextPos(*p2,hb);
ElemType a,b;
float sum;
while(pa&&pb)
{
a=GetCurElem(pa);
b=GetCurElem(pb);
switch(compare(a,b))
{
case -1:
ha=pa;
pa=NextPos(*p1,ha);
break;
case 0:
sum=a.coef+b.coef;
if(sum!=0.0)
{
pa->data.coef=sum;
SetCurElem(pa,pa->data);
ha=pa;
}
else
{
DelFirst(p1,ha,&pa);
FreeNode(&pa);
}
DelFirst(p2,hb,&pb);
FreeNode(&pb);
pa=NextPos(*p1,ha);
pb=NextPos(*p2,hb);
break;
case 1:
DelFirst(p2,hb,&pb);
InsFirst(p1,ha,pb);
pb=NextPos(*p2,hb);
ha=NextPos(*p1,ha);
break;
}
}
if(pb) Append(p1,pb);
FreeNode(p2); //这里应该是p2而不是hb,因为在变化过程中hb变化了不再是头结点
(*p2).head=(*p2).tail=NULL;
(*p2).len=0;
}
void SubtractPolyn(Polynomial *p1,Polynomial *p2)
{
Link ha,hb,pa,pb;
ha=GetHead(*p1);
hb=GetHead(*p2);
pa=NextPos(*p1,ha);
pb=NextPos(*p2,hb);
ElemType a,b;
float sum;
while(pa&&pb)
{
a=GetCurElem(pa);
b=GetCurElem(pb);
switch(compare(a,b))
{
case -1:
ha=pa;
pa=NextPos(*p1,ha);
break;
case 0:
sum=a.coef-b.coef;
if(sum!=0.0)
{
pa->data.coef=sum;
SetCurElem(pa,pa->data);
ha=pa;
}
else
{
DelFirst(p1,ha,&pa);
FreeNode(&pa);
}
DelFirst(p2,hb,&pb);
FreeNode(&pb);
pb=NextPos(*p2,hb);
pa=NextPos(*p1,ha);
break;
case 1:
DelFirst(p2,hb,&pb);
pb->data.coef=-pb->data.coef;
InsFirst(p1,ha,pb);
pb=NextPos(*p2,hb);
ha=NextPos(*p1,ha);
break;
}
}
if(pb)
{
Link r=pb;
while(r)
{
r->data.coef=-r->data.coef;
r=r->next;
}
Append(p1,pb);
}
FreeNode(p2); //这里应该是p2而不是hb,因为在变化过程中hb变化了不再是头结点
(*p2).head=(*p2).tail=NULL;
(*p2).len=0;
}
int PolynLength(Polynomial p)
{
return p.len;
}
void ClearList(LinkList *L)
{
Link p,q;
p=(*L).head->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
(*L).head->next=NULL;
(*L).tail=(*L).head;
(*L).len=0;
}
void DestoryList(LinkList *L)
{
ClearList(L);
free((*L).head);
(*L).head=(*L).tail=NULL;
}
void DestoryPolyn(Polynomial *p)
{
DestoryList(p);
}
void MultiplyPolyn(Polynomial *p1,Polynomial *p2)
{
Polynomial pc,ptmp;
Link h1=GetHead(*p1);
Link h2=GetHead(*p2);
Link qa,qb;
Link s;
int a=PolynLength(*p1);
int b=PolynLength(*p2);
int i,j;
InitList(&pc);
for(i=1;i<=a;i++)
{
InitList(&ptmp);
DelFirst(p1,h1,&qa);
qb=NextPos(*p2,h2);
for(j=1;j<=b;j++)
{
s=(Link)malloc(sizeof(LNode));
if(!s) exit(OVERFLOW);
s->data.coef=qa->data.coef*qb->data.coef;
s->data.expn=qa->data.expn+qb->data.expn;
s->next=NULL;
Append(&ptmp,s);
qb=qb->next;
}
AddPolyn(&pc,&ptmp);
}
AddPolyn(p1,&pc);
DestoryPolyn(p2);
}
int main()
{
Polynomial Pa,Pb;
int m,n;
scanf("%d%d",&m,&n);
CreatPolyn(&Pa,m);
CreatPolyn(&Pb,n);
PrintPolyn(Pa);
PrintPolyn(Pb);
//AddPolyn(&Pa,&Pb);
//SubtractPolyn(&Pa,&Pb);
MultiplyPolyn(&Pa,&Pb);
PrintPolyn(Pa);
return 0;
}
2 2
1 1 3 3
2 0 5 2
1x+3x^3
2+5x^2
2x+11x^3+15x^5
Process returned 0 (0x0) execution time : 21.121 s
Press any key to continue.
来源:CSDN
作者:雾里看花花里看雾
链接:https://blog.csdn.net/qq_43779149/article/details/104158298