#include <stdlib.h>
typedef struct PNode
{
float coe;
int exp;
struct PNode *next;
}PNode,*PolyList;
void InitPL(PolyList PL)
{
if(PL==NULL)
{
PL=(PolyList)malloc(sizeof(PNode));
}
PL->coe=0;
PL->exp=0;
PL->next=NULL;
}
void InsertAfter(PolyList PL,PolyList PD)
{
PD->next=PL->next;
PL->next=PD;
}
void InsertElem(PolyList PL,int ex,float co)
{
PolyList q,p;
if(co==0)
return;
p=PL->next;
q=PL;
while(p!=NULL&&p->exp<ex)
{
q=p;
p=p->next;
}
if(p->exp!=ex)
{
p=(PolyList)malloc(sizeof(PNode));
p->coe=co;
p->exp=ex;
p->next=q->next;
q->next=p;
}
else
{
p->coe=p->coe+co;
if(p->coe==0)
{
q->next=p->next;
}
}
}
void Print(PolyList PL)
{
PolyList p;
p=PL->next;
while(p)
{
printf("%f,%d/t",p->coe,p->exp);
p=p->next;
}
printf("/n");
}
void DeleteAfter(PolyList PL)
{
PL->next=PL->next->next;
}
void DeleteElem(PolyList PL,int ex)
{
PolyList p,q;
p=PL->next;
q=PL;
while(p!=NULL&&p->exp<ex)
{
q=p;
p=p->next;
}
if(p->exp==ex)
{
q->next=p->next;
}
else
return;
}
void AddPoly(PolyList PL,PolyList PN)
{
PolyList pn;
pn=PN->next;
while(pn)
{
InsertElem(PL,pn->exp,pn->coe);
pn=pn->next;
}
}
void SubPoly(PolyList PL,PolyList PN)
{
PolyList pn;
pn=PN->next;
while(pn)
{
InsertElem(PL,pn->exp,pn->coe*(-1));
pn=pn->next;
}
}
int main()
{
PolyList PM,QN;
int i,expo;
float coef;
PM=(PolyList)malloc(sizeof(PNode));
QN=(PolyList)malloc(sizeof(PNode));
InitPL(PM);
InitPL(QN);
for(i=1;i<=2;i++)
{
printf("Please input coefficient and exponent:");
scanf("%f,%d",&coef,&expo);
InsertElem(PM,expo,coef);
}
for(i=1;i<=3;i++)
{
printf("Please input coefficient and exponent:");
scanf("%f,%d",&coef,&expo);
InsertElem(QN,expo,coef);
}
Print(PM);
Print(QN);
AddPoly(PM,QN);
Print(PM);
SubPoly(PM,QN);
SubPoly(PM,QN);
Print(PM);
printf("Hello, world/n");
getch();
return 0;
}