#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
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;
typedef int Status;
Status InitList(LinkList *L)
{
Link p;
p = (Link)malloc(sizeof(LNode));
if(p)
{
p->next = NULL;
(*L).head = (*L).tail = p;
(*L).len = 0;
return OK;
}
else
return ERROR;
}
Status MakeNode(Link *p,ElemType e)
{
(*p) = (Link)malloc(sizeof(LNode));
if(!(*p)) return ERROR;
(*p)->data = e;
return OK;
}
Status InsFirst(LinkList *L,Link h,Link s)
{
s->next = h->next;
h->next = s;
if(h==(*L).tail)
(*L).tail = h->next;
(*L).len++;
return OK;
}
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;
}
else
return FALSE;
}
Status ClearList(LinkList *L)
{
Link p, q;
if(L->head != L->tail)
{
p = q = L->head->next;
L->head->next = NULL;
while(p != L->tail)
{
p = q->next;
free(q);
q = p;
}
free(q);
L->tail = L->head;
L->len = 0;
}
return OK;
}
Position GetHead(LinkList *L)
{
return L->head;
}
Position NextPos(Link p)
{
return p->next;
}
Position PriorPos(LinkList L, Link p)
{
Link q;
q = L.head;
while(q->next != p)
{
q = q->next;
}
return q;
}
void FreeNode(Link *p)
{
free((*p));
(*p) = NULL;
}
Status ListEmpty(LinkList L)
{
return L.len==0;
}
ElemType GetCurElem(Link p)
{
return p->data;
}
Status Append(LinkList *L,Link s)
{
int i=1;
(*L).tail->next = s;
while(s->next)
{
i++;
s = s->next;
}
(*L).tail = s;
(*L).len+=i;
return OK;
}
void DestroyPolyn(LinkList *L)
{
ClearList(L);
FreeNode(&(*L).head);
(*L).tail = NULL;
(*L).len = 0;
}
int cmp(term e1, term e2)
{
if(e1.expn == e2.expn) return 0;
if(e1.expn < e2.expn) return -1;
return 1;
}
Status LocateElem(LinkList L, ElemType e, Position *q, int (*compare)(ElemType, ElemType))
{
Link p, pp;
p = L.head;
do
{
pp = p;
p = p->next;
}while(p && compare(p->data, e) < 0);
if(!p || compare(p->data, e) >0)
{
(*q) = pp;
return FALSE;
}
else
{
(*q) = p;
return TRUE;
}
}
Status OrderInsert(LinkList *L, ElemType e, int(*compare)(ElemType, ElemType))
{
Position q, s;
if(LocateElem(*L, e, &q, compare))
{
q->data.coef += e.coef;
if(!q->data.coef)
{
s = PriorPos(*L, q);
if(!s)
s = L->head;
DelFirst(L, s, &q);
FreeNode(&q);
}
return OK;
}
else
{
if(MakeNode(&s, e))
{
InsFirst(L, q, s);
return OK;
}
return ERROR;
}
}
void CreatPolyn(polynomial *P, int m)
{
Position q, s;
int i;
term e;
InitList(P);
printf("请输入%d个系数,指数:\n", m);
for(i = 1; i <= m; i++)
{
scanf("%f %d", &e.coef, &e.expn);
if(!LocateElem(*P, e, &q, cmp))
{
if(MakeNode(&s, e))
InsFirst(P, q, s);
}
}
}
void PrintPolyn(polynomial P)
{
Link q;
q = P.head->next;
printf("系数 指数\n");
while(q)
{
printf("%.2f %d\n", q->data.coef, q->data.expn);
q = q->next;
}
}
int PolynLength(polynomial P)
{
Link q;
int i;
q = P.head;
i = 0;
while(q != P.tail)
{
q = q->next;
i++;
}
return i;
}
void AddPolyn(polynomial *Pa, polynomial *Pb)
{
Position ha, hb, qa, qb;
term a, b;
ha = GetHead(Pa);
hb = GetHead(Pb);
qa = NextPos(ha);
qb = NextPos(hb);
while(!ListEmpty(*Pa) && !ListEmpty(*Pb) && qa)
{
a = GetCurElem(qa);
b = GetCurElem(qb);
switch(cmp(a, b))
{
case -1:
ha = qa;
qa = NextPos(qa);
break;
case 0:
qa->data.coef += qb->data.coef;
if(qa->data.coef)
{
ha = qa;
}
else
{
DelFirst(Pa, ha, &qa);
FreeNode(&qa);
}
DelFirst(Pb, hb, &qb);
FreeNode(&qb);
qa = NextPos(ha);
qb = NextPos(hb);
break;
case 1:
DelFirst(Pb, hb, &qb);
InsFirst(Pa, ha, qb);
qb = NextPos(hb);
ha = NextPos(ha);
}
}
if(!ListEmpty(*Pb))
{
Pb->tail = hb;
Append(Pa, qb);
}
DestroyPolyn(Pb);
}
void Oppsite(polynomial *Pa)
{
Position p;
p = Pa->head->next;
while(p)
{
p->data.coef *= -1;
p = p->next;
}
}
void SubtractPolyn(polynomial *Pa, polynomial *Pb)
{
Position ha, hb, qa, qb, qqb;
term a, b;
ha = GetHead(Pa);
hb = GetHead(Pb);
qa = NextPos(ha);
qb = NextPos(hb);
while(!ListEmpty(*Pa) && !ListEmpty(*Pb) && qa)
{
a = GetCurElem(qa);
b = GetCurElem(qb);
switch(cmp(a, b))
{
case -1:
ha = qa;
qa = NextPos(qa);
break;
case 0:
qa->data.coef -= qb->data.coef;
if(qa->data.coef)
{
ha = qa;
}
else
{
DelFirst(Pa, ha, &qa);
FreeNode(&qa);
}
DelFirst(Pb, hb, &qb);
FreeNode(&qb);
qb = NextPos(hb);
qa = NextPos(ha);
break;
case 1:
qb->data.coef *= -1;
DelFirst(Pb, hb, &qb);
InsFirst(Pa, ha, qb);
qb = NextPos(hb);
ha = ha->next;
break;
}
}
if(!ListEmpty(*Pb))
{
qqb = qb;
while(qqb)
{
qqb->data.coef *= -1;
qqb = qqb->next;
}
Pb->tail = hb;
Append(Pa, qb);
}
DestroyPolyn(Pb);
}
void SubtractPolyn_N(polynomial *Pa, polynomial *Pb)
{
Oppsite(Pb);
AddPolyn(Pa, Pb);;
}
void MultipyPolyn(polynomial *Pa, polynomial *Pb)
{
polynomial Pc;
Position qa, qb;
term a, b, c;
InitList(&Pc);
qa = GetHead(Pa);
qa = qa->next;
while(qa)
{
a = GetCurElem(qa);
qb = GetHead(Pb);
qb = qb->next;
while(qb)
{
b = GetCurElem(qb);
c.coef = a.coef * b.coef;
c.expn = a.expn + b.expn;
OrderInsert(&Pc, c, cmp);
qb = qb->next;
}
qa = qa->next;
}
DestroyPolyn(Pb);
ClearList(Pa);
Pa->head = Pc.head;
Pa->tail = Pc.tail;
Pa->len = Pc.len;
}
void main()
{
polynomial p, q;
int m;
printf("请输入一个一元多项式的非零项的个数:");
scanf("%d", &m);
CreatPolyn(&p, m);
PrintPolyn(p);
printf("请输入一个一元多项式的非零项的个数:");
scanf("%d", &m);
CreatPolyn(&q, m);
PrintPolyn(q);
MultipyPolyn(&p, &q);
PrintPolyn(p);
DestroyPolyn(&p);
}