搜索
查看: 325|回复: 0
打印 上一主题 下一主题

可以帮我看看哪里出了问题吗/

[复制链接]
跳转到指定楼层
楼主
 楼主| 发表于 2020-12-28 12:09:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. typedef struct node
  4. {
  5.     float coef;
  6.     int exp;
  7.     struct node * next;
  8. }PolyNode;
  9. void CreateListR(PolyNode * &L,double a[],int b[],int n)
  10. {
  11.     PolyNode *s,*tc;
  12.     int i;
  13.     L=(PolyNode * )malloc(sizeof(PolyNode));
  14.     tc=L;
  15.     for(i=0;i<n;i++)
  16.     {
  17.         s=(PolyNode * )malloc(sizeof(PolyNode));
  18.         s-> coef=a[i];
  19.         s-> next=b[i];
  20.         tc-> next=s;
  21.         tc=s;
  22.     }
  23.     tc->next=NULL;
  24. }
  25. void DestoryList(PolyNode * &L)
  26. {
  27.     PolyNode * pre=L,*p=pre->next;
  28.     while (p!=NULL)
  29.     {  free(pre);
  30.        pre=p;p=p->next;
  31.     }
  32.     free(pre);
  33. }
  34. void DisPoly(PolyNode *L)
  35. {  PolyNode *p=L->next;
  36.    while(p!=NULL)
  37.    {
  38.        free(pre);
  39.        pre=p;p=p->next;
  40.    }
  41.     free(pre);
  42. }
  43. void DispPoly(PolyNode *L)
  44. { PolyNode *p=L->next;
  45.   while(p!=NULL)
  46.   {
  47.       printf("(%gx^%d)",p->coef,p->exp);
  48.       p=p->next;
  49.   }
  50.    printf("");
  51. }
  52. void Add(PloyNode * ha,PolyNode * hb,PolyNode * &hc)
  53. {    PolyNode * pa=ha-> next,*pb=hb->next,*s,*tc;
  54.      double c;
  55.      hc=(PolyNode * )malloc(sizeof(PloyNode));
  56.      tc=hc;
  57.      while(pa!=NULL&&pb!=NULL)
  58.      {   if (pa->exp>pb->exp)
  59.          {   s=(PolyNode * )malloc(sizeof(PolyNode));
  60.              s->exp=pa->exp;s->coef=pa->coef;
  61.              tc->next=s;tc=s;
  62.              pa=pa->next;
  63.          }
  64.          else if (pa->exp<pb->exp)
  65.          {    s=(PolyNode * )malloc(sizeof(PolyNode));
  66.               s->exp=pb->exp;s->coef=pb->coef;
  67.               tc->next=s; tc=s;
  68.               pb=pb->next;
  69.          }
  70.          else
  71.          {   c=pa->coef+pb->coef;
  72.              if (c!=0)
  73.              {   s=(PolyNode * )malloc(sizeof(PolyNode));
  74.                  s->exp=pa->exp;s->coef=c;
  75.                  tc->next=s;tc=s;
  76.              }
  77.              pa=pa->next;
  78.              pb=pb->next;
  79.          }
  80.      }
  81.      if(pb!=NULL) pa=pb;
  82.      while (pa!=NULL)
  83.      {    s=(PolyNode * )malloc(sizeof(PolyNode));
  84.           s->exp=pa->exp;
  85.           s->coef=pa->coef;
  86.           tc->next=s;tc=s;
  87.           pa=pa->next;
  88.      }
  89.      tc->next=NULL;
  90. }
  91. void main()
  92. {   PolyNode *poly1,*poly2,*poly3;
  93.     double a[MAX];
  94.     int b[MAX];
  95.     //----创建第一个多项式单链表----
  96.     a[0]=3.2;   b[0]=5;
  97.     a[1]=2.0;   b[1]=3;
  98.     a[2]=-6.0;  b[2]=0;
  99.     a[3]=10.0;  b[3]=0;
  100.     n=4;
  101.     CreatListR(Poly1,a,b,n);
  102.     printf("第一个多项式:");DosPoly(Poly1);
  103.     //----创建第二个多项式单链表----
  104.     a[0]=1.8;      b[0]=5;
  105.     a[1]=-2.5;     b[1]=4;
  106.     a[2]=-2.0;     b[2]=3;
  107.     a[3]=1.0;      b[3]=2;
  108.     a[4]=6.0;      b[4]=1;
  109.     a[5]=-5.0;     b[5]=0;
  110.     n=6;
  111.     CreatListR(poly2,a,b,n);
  112.     printf("第二个多项式:");DispPoly(Poly2);
  113.     Add(Poly1,Poly2,Poly3);
  114.     printf("相加h后多项式:");DispPoly(Poly3);
  115.     DestoryList(Poly1);
  116.     DestoryList(Poly2);
  117.     DestoryList(poly3);
  118. }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表