#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<windows.h>
typedef struct node
{
int coef,exp;
struct node *next;
}JD,*LinkList;
void add_poly(JDpa,JDpb) /多项式相加函数 ,通过判断指数大小来推动程序进行/
{
JD *p,*q,*u,*pre;
int x;
p=pa->next;
q=pb->next;
pre=pa;
while((p!=NULL)&&(q!=NULL))
{
if(p->expexp)
{
pre=p;
p=p->next;
}
else if(p->exp==q->exp)
{
x=p->coef+q->coef;
if(x!=0)
{
p->coef=x;
pre=p;
}
else
{
pre->next=p->next;
free§;
}
p=pre->next;
u=q;
q=q->next;
free(u);
}
else
{
u=q->next;
q->next=p;
pre->next=q;
pre=q;
q=u;
}
}
if(q!=NULL)
pre->next=q;
free(pb);
}
void PrintList(LinkList head)
{
JD *p;
printf("\t\t\t");
for(p=head->next;p->next!=NULL;p=p->next)
printf("%dx^%d + “,p->coef,p->exp);
printf(”%dx^%d",p->coef,p->exp);
printf("\n");
}
LinkList CreateList(void) /多项式创建函数/
{
int coef,exp;
JD *head=(JD *)malloc(sizeof(JD));
JD *s,*r;
r=head;
printf("\t\tplease input the coef and exp:\n");
while(1)
{
printf("\t\tthe coef:");
scanf("%d",&coef);
if(coef0)break;
printf("\n\t\tthe exp:");
scanf("%d",&exp);
printf("\t\t==========================\n");
s=(JD *)malloc(sizeof(JD));
s->coef=coef;
s->exp=exp;
r->next=s;
r=s;
}
r->next=NULL;
return head;
getchar();
}
void main()
{
LinkList pa,pb;
pa=CreateList(); /生成pa指向的多项式/
system(“cls”);
pb=CreateList(); /生成pb指向的多项式/
system(“cls”);
printf("\n\n\n\t\t=The Addition Of One-Letter-Polynomial=\n");
printf("\n\t\tThe first one-letter- polynomial:\n");
PrintList(pa);
printf("\n\t\tThe second one-letter- polynomial:\n");
PrintList(pb);
printf("\n\t\t========The final one-letter- polynomial:\n");
add_poly(pa,pb);
PrintList(pa);
getchar();
getch();
}