一元多项式求和问题没报错却运行不了(我是萌新)
#include<stdio.h>#include<malloc.h>
typedef struct Polynode
{int coef;
int exe;
struct Polynode *next;
}Polynode,*Linklist;
void CreatePoly(Linklist *p);
void PrintPoly(Linklist p);
Linklist AddPoly(Linklist pa,Linklist pb);
int main()
{Linklist p1,p2;
printf("请输入第一个多项式的系数和指数\n");
p1=CreatePoly(&p1);
PrintPoly(p1);
printf("请输入第二个多项式的系数和指数\n");
p2=CreatePoly(&p2);
PrintPoly(p2);
PrintPoly(AddPoly(p1,p2));
return 0;
}
void CreatePoly(Linklist *p)
{Linklist r,s;
int c,e;
int flag=1;
*p=(Linklist)malloc(sizeof(Polynode));
r=*p;
while(flag)
{ scanf("%d,%d",&c,&e);
if(c!=0)
{s=(Linklist)malloc(sizeof(Polynode));
s->coef=c;
s->exe=e;
r->next=s;
r=s;
}
else
flag=0;
}
r->next=NULL;
return p;
}
void PrintPoly(Linklist p)
{Linklist s;
s=p->next;
while(s!=NULL)
{printf("%d x^%d",s->coef,s->exe);
s=s->next;
}
printf("\n");
getchar();
}
Linklist AddPoly(Linklist pa,Linklist pb)
{Linklist newp,p,q,s,pc;
int sum;
p=pa->next;
q=pb->next;
newp=(Linklist)malloc(sizeof(Polynode));
pc=newp;
while(p&&q)
{
if(p->exe<q->exe)
{s=(Linklist)malloc(sizeof(Polynode));
s->coef=p->coef;
s->exe=p->exe;
pc->next=s;
pc=s;
p=p->next;
}
else if(p->exe==q->exe)
{sum=p->exe+q->exe;
if(sum!=0)
{s->coef=sum;
s->exe=p->exe;
pc->next=s;
pc=s;
}
p=p->next;
q=q->next;
}
else
{s=(Linklist)malloc(sizeof(Polynode));
s->coef=q->coef;
s->exe=q->exe;
pc->next=s;
pc=s;
q=q->next;
}
}
while(p)
{s=(Linklist)malloc(sizeof(Polynode));
s->coef=p->coef;
s->exe=p->exe;
pc->next=s;
pc=s;
p=p->next;
}
while(q)
{s=(Linklist)malloc(sizeof(Polynode));
s->coef=q->coef;
s->exe=q->exe;
pc->next=s;
pc=s;
q=q->next;
}
pc->next=NULL;
return newp;
}
页:
[1]