#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).head=(*p2).tail=NULL;
(*p2).len=0;
}
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);
PrintPolyn(Pa);
return 0;
}
4 4
1 0 2 2 -1.2 3 5 4
3 1 1.2 3 7 4 9 5
1+2x^2-1.2x^3+5x^4
3x+1.2x^3+7x^4+9x^5
1+3x+2x^2+12x^4+9x^5
Process returned 0 (0x0) execution time : 41.823 s
Press any key to continue.
来源:CSDN
作者:雾里看花花里看雾
链接:https://blog.csdn.net/qq_43779149/article/details/104157199