这是一元多项式的程序,求各位大神帮忙调试并修改下错误,感谢!
//一元多项式那个我着急用这个程序,麻烦哪位大神帮忙改下,谢谢哦!#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int coef; //系数
int expn; //指数
struct node *next;
}ploynom;
void main()
{
ploynom *pa,*pb,*pc;
printf("please input tne ploynom of pa:\n");
pa=creatployn();
printf("print the ploynom of pa:\n");
printployn(pa);
printf("please input tne ploynom of pb:\n");
pb=creatployn();
printf("print the ploynom of pb:\n");
printployn(pb); printf("\n\n\n");
pc=(ploynom *)malloc(sizeof(ploynom));
pc->next=NULL;
pc=addployn(pa,pb);
}
ploynom *creatployn()
{
int c,e;
ploynom *head,*r,*p;
head=(ploynom *)malloc(sizeof(ploynom));
r=head;
scanf("%d,%d",&c,&e);
while(c!=0) //以输入系数c为0作为结束标志
{
p=(ploynom *)malooc(sizeof(ploynom));
p->coef=c;
p->expn=e;
r->next=p; //在当前表尾作插入
r=p;
scanf("%d,%d\n",&c,&e);
}
r->next=NULL;
return head;
}
polynom *addployn(ploynom *pa,ploynom *pb)
{
ploynom *qa,*qb,*q1;
qa=pa->next; //qa,qb,初值分别指向pa,pb的第一个结点
qb=pb->next;
ploynom *headc,*pc,*qc;
pc=(ploynom *)malloc(sizeof(ploynom));
pc->next=NULL;
headc=pc;
while(qa&&qb) //当二者均非空时
{
qc=(ploynom *)malloc(sizeof(ploynom));
if(qa->expn==qb->expn)
{
qc->coef=qa->coef+qb->coef;
if((qc->coef)!=0) //系数之和不为0
{
qc->expn=qa->expn;
qa=qa->next;
q1=qb; //将已经把指数和系数都传给qc后没用的qb的第一个结点放到q1中
qb=qb->next;
free(q1); //释放掉第一个和qa系数相等的qb结点
}
else
{
q1=qa; qa=qa->next; free(q1);
q1=qb; qb=qb->next; free(q1);
}
}
else if(qa->expn<qb->expn)
{
qc->coef=qa->coef;
qc->expn=qa->expn;
q1=qa;
qa=qa->next;
free(q1);
}
else(qa->expn>qb->expn)
{
qc->coef=qb->coef;
qc->expn=qb->expn;
q1=qb;
qb=qb->next;
free(q1);
}
if((qc->coef)!=0)
{
qc->next=qc->next;
pc->next=qc;
pc=qc;
}
}
while(qa!=NULL&&qb==NULL)
{
qc=(ploynom *)malloc(sizeof(ploynom));
qc->coef=qa->coef; qc->expn=qa->expn;
qa=qa->next;
qc->next=pc->next;
pc->next=qc; pc=qc;
}
while(qa==NULL&&qb!=NULL)
{
qc=(ploynom *)malloc(sizeof(ploynom));
qc->coef=qb->coef; qc->expn=qb->expn;
qb=qb->next;
qc->next=pc->next;
pc->next=qc; pc=qc;
}
return headc;
}
void printployn(ploynom *p)
{
ploynom *q=p->next;
if(!q)
printf("It's empty!!!");
while(q)
{
if(q->coef>0) putchar('+');
if(q->coef!=1&&q->coef!=-1)
{
printf("%2d",q->coef);
if(q->expn==1) putchar('x');
else if(q->expn) printf("x^%d",q->expn);
}
else
{
if(q->coef==1)
{
if(q->expn==0) putchar('1');
else if(q->expn==1) putchar('x');
else printf("x^%d",q->expn);
}
if(q->coef==-1)
{
if(q->expn==0) putchar('-1');
else if(q->expn==1) putchar('-x');
else printf("-x^%d",q->expn);
}
}
q=q->next;
}
printf("\n");
}