# 一元多项式的表示及相加 数据结构

#include<stdio.h>
#include<malloc.h>
#include<math.h>

#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0

typedef struct  DataType
{
float coef;
int   expn;
}ELemType;

typedef struct List
{
ELemType   data;
struct List *prior;
struct List *next;

int  CreatePoly(LinkList L,int m)//创建多项式
{
int i;
float f=0.0;
q=L;

L->prior=L->next=L;
for(i=0;i<m;i++)
{
printf("请输入第%d项的系数和指数（用空格隔开）:",i+1);
scanf("%f %d",&p->data.coef,&p->data.expn);
q->next=p;
p->prior=q;
q=q->next;
}
q->next=L;
L->prior=q;
return OK;
}

{
int i;
p=L;
i=0;
while(p->next!=L)
{
i++;
p=p->next;
}
return i;
}

{
Pa=La->next;
Pb=Lb->next;
while(Pa!=La&&Pb!=Lb)
{
if(Pa->data.expn<Pb->data.expn)
Pa=Pa->next;
else if(Pa->data.expn>Pb->data.expn)
{
Pb->prior=Pa->prior;
Pa->prior->next=Pb;
Pb->next=Pa;
Pa->prior=Pb;
Pb=Pb->next;
}
else
{
if((Pa->data.coef+Pb->data.coef)!=0)
{
Pa->data.coef=Pa->data.coef+Pb->data.coef;
Pa=Pa->next;
Pb=Pb->next;
}
}

}
if(Pa==La&&Pb==Lb)
{}
else if(Pa==La)
{
Pa->prior->next=Pb;
Pb->prior=Pa->prior;
while(Pb!=Lb)
{
Pb=Pb->next;
}
Pb->prior->next=La;
La->prior=Pb->prior;
}
else
{}
}

double GetPolynValue(LinkList L,float x)//求多项式的值，变量为x
{
double sum=0.0;
p=L;
while(p->next!=L)
{
sum+=(p->next->data.coef)*pow(x,p->next->data.expn);
p=p->next;
}
return sum;
}

{
p=L->next;
while(p)
{
tmp=p->next;
free(p);
p=tmp;
}
}

{
float f;
p=L;
while(p->next!=L)
{
printf("%.2f*x^%d",p->next->data.coef,p->next->data.expn);
f=p->next->data.coef;
p=p->next;
if(p->next!=L&&f>0)
printf("+");
}
printf("\n");
}
void main()
{
LNode La;
LNode Lb;
int length;
double value;
float x;
int m;
int n;
printf("请输入第一个多项式的项数:");
scanf("%d",&m);
CreatePoly(&La,m);
length=PolynLength(&La);
printf("The length of the Poly is :%d\n",length);
PrintPolyn(&La);

printf("请输入第二个多项式的项数:");
scanf("%d",&n);
CreatePoly(&Lb,n);
length=PolynLength(&Lb);
printf("The length of the Poly is :%d\n",length);
PrintPolyn(&Lb);

printf("两多项式相加得到:");
printf("多项式的值= %.2f\n",value);<span style="font-family: Arial, Helvetica, sans-serif;">}</span>