user-0 提问: 一元多项式相加，帮忙修改

回复:
user: user-0
content: 引用 1 楼 自信男孩的回复:#include <stdio.h> #include <stdlib.h>  typedef struct Polynode {     int coef,exp;     struct Polynode *next;  }Polynode,*Polylist;  void polycreate(Polylist &head) {     Polylist rear,s; int c,e;      head=(Polynode *)malloc(sizeof(Polynode));     if (!head)         exit(0);      rear=head;      scanf("%d %d",&c,&e);      while(c!=0){          s=(Polynode *)malloc(sizeof(Polynode));         if (!s)             exit(0);         s->coef=c;         s->exp=e;          rear->next=s;         rear=s;          scanf("%d %d",&c,&e);     }     rear->next=NULL; } //polyadd(Polylist polya,Polylist polyb) Polylist polyadd(Polylist polya,Polylist polyb) {     Polynode *pa,*pb,*pc,*r, *polyc;     int sum;      polyc = (Polylist)malloc(sizeof(Polynode));     if (!polyc)         exit(0);     pa = polya->next;      //pb=polya->next;     pb=polyb->next;     pc = polyc;   //*pre指向和多项式的尾节点      while(pa!=NULL&&pb!=NULL)      {         if(pa->exp<pb->exp)         {             pc->next=pa; pc=pa; pa=pa->next;}          else if(pa->exp==pb->exp)         {             sum=pa->coef+pb->coef;              if(sum!=0)             {                 pa->coef=sum; pc->next=pa;                  pc=pa;pa=pa->next;                  r=pb;pb=pb->next;free(r);             }             else             {                 r=pa;pa=pa->next;free(r);                  r=pb;pb=pb->next;free(r);             }         } else         {     pc->next=pb; pc=pb; pb=pb->next;}     }     if(pa!=NULL) pc->next=pa;      else pc->next=pb;      return polyc; }  void PrintPoly(Polylist p) {     Polylist s;      s=p->next;      while(s)     {         //printf("%0.2f x^%d",s->coef,s->exp);         printf("%d x^%d",s->coef,s->exp);         s=s->next;         if(s!=NULL)             if(s->coef>=0) printf("+");     }     printf("\n"); }  int main() {     Polylist polya,polyb;     polycreate(polya);     polycreate(polyb);     PrintPoly(polyadd(polya,polyb));  } 供参考～ 能编译通过但是不能结果不对

user: user-0
content: 引用 1 楼 自信男孩的回复:#include <stdio.h> #include <stdlib.h>  typedef struct Polynode {     int coef,exp;     struct Polynode *next;  }Polynode,*Polylist;  void polycreate(Polylist &head) {     Polylist rear,s; int c,e;      head=(Polynode *)malloc(sizeof(Polynode));     if (!head)         exit(0);      rear=head;      scanf("%d %d",&c,&e);      while(c!=0){          s=(Polynode *)malloc(sizeof(Polynode));         if (!s)             exit(0);         s->coef=c;         s->exp=e;          rear->next=s;         rear=s;          scanf("%d %d",&c,&e);     }     rear->next=NULL; } //polyadd(Polylist polya,Polylist polyb) Polylist polyadd(Polylist polya,Polylist polyb) {     Polynode *pa,*pb,*pc,*r, *polyc;     int sum;      polyc = (Polylist)malloc(sizeof(Polynode));     if (!polyc)         exit(0);     pa = polya->next;      //pb=polya->next;     pb=polyb->next;     pc = polyc;   //*pre指向和多项式的尾节点      while(pa!=NULL&&pb!=NULL)      {         if(pa->exp<pb->exp)         {             pc->next=pa; pc=pa; pa=pa->next;}          else if(pa->exp==pb->exp)         {             sum=pa->coef+pb->coef;              if(sum!=0)             {                 pa->coef=sum; pc->next=pa;                  pc=pa;pa=pa->next;                  r=pb;pb=pb->next;free(r);             }             else             {                 r=pa;pa=pa->next;free(r);                  r=pb;pb=pb->next;free(r);             }         } else         {     pc->next=pb; pc=pb; pb=pb->next;}     }     if(pa!=NULL) pc->next=pa;      else pc->next=pb;      return polyc; }  void PrintPoly(Polylist p) {     Polylist s;      s=p->next;      while(s)     {         //printf("%0.2f x^%d",s->coef,s->exp);         printf("%d x^%d",s->coef,s->exp);         s=s->next;         if(s!=NULL)             if(s->coef>=0) printf("+");     }     printf("\n"); }  int main() {     Polylist polya,polyb;     polycreate(polya);     polycreate(polyb);     PrintPoly(polyadd(polya,polyb));  } 供参考～ Poly list polyadd(Polylist polya,Polylist polyb)     polya和polyb之前不应该加引用符号&吗？

user: user-1
content: #include <stdio.h> #include <stdlib.h>  typedef struct Polynode {     int coef,exp;     struct Polynode *next;  }Polynode,*Polylist;  void polycreate(Polylist &head) {     Polylist rear,s; int c,e;      head=(Polynode *)malloc(sizeof(Polynode));     if (!head)         exit(0);      rear=head;      scanf("%d %d",&c,&e);      while(c!=0){          s=(Polynode *)malloc(sizeof(Polynode));         if (!s)             exit(0);         s->coef=c;         s->exp=e;          rear->next=s;         rear=s;          scanf("%d %d",&c,&e);     }     rear->next=NULL; } //polyadd(Polylist polya,Polylist polyb) Polylist polyadd(Polylist polya,Polylist polyb) {     Polynode *pa,*pb,*pc,*r, *polyc;     int sum;      polyc = (Polylist)malloc(sizeof(Polynode));     if (!polyc)         exit(0);     pa = polya->next;      //pb=polya->next;     pb=polyb->next;     pc = polyc;   //*pre指向和多项式的尾节点      while(pa!=NULL&&pb!=NULL)      {         if(pa->exp<pb->exp)         {             pc->next=pa; pc=pa; pa=pa->next;}          else if(pa->exp==pb->exp)         {             sum=pa->coef+pb->coef;              if(sum!=0)             {                 pa->coef=sum; pc->next=pa;                  pc=pa;pa=pa->next;                  r=pb;pb=pb->next;free(r);             }             else             {                 r=pa;pa=pa->next;free(r);                  r=pb;pb=pb->next;free(r);             }         } else         {     pc->next=pb; pc=pb; pb=pb->next;}     }     if(pa!=NULL) pc->next=pa;      else pc->next=pb;      return polyc; }  void PrintPoly(Polylist p) {     Polylist s;      s=p->next;      while(s)     {         //printf("%0.2f x^%d",s->coef,s->exp);         printf("%d x^%d",s->coef,s->exp);         s=s->next;         if(s!=NULL)             if(s->coef>=0) printf("+");     }     printf("\n"); }  int main() {     Polylist polya,polyb;     polycreate(polya);     polycreate(polyb);     PrintPoly(polyadd(polya,polyb));  } 供参考～