user-0 提问: 数据结构，多项式加法，改错，实在不知道咋改了！！！

回复:
user: user-1
content: 仅供参考：//链表实现一元多项式的加法减法乘法 #include <stdio.h> #include <stdlib.h> typedef struct node {     float coef;   //系数     int expn;     //指数     struct node *next; } PolyNode;      //多项式节点 polynomial node typedef PolyNode* Polynomial; Polynomial createPolynomial() {  //创建多项式     PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点     head->next = NULL;     float coef;     int expn;     printf("输入该多项式每一项的系数和指数，每项一行，输入0 0结束！\n");     while (scanf("%f %d", &coef, &expn) && coef) {   // 默认，按指数递减排列         if (head->next) {             p = head;             while (p->next && expn < p->next->expn)                 p = p->next;             if (p->next) {                 if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式                     p->next->coef += coef;                     if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系数为0，则舍弃该节点                         q = p->next;                         p->next = q->next;                         free(q);                     }                 } else {                     q = (PolyNode*)malloc(sizeof(PolyNode));                     q->coef = coef;                     q->expn = expn;                     q->next = p->next;                     p->next = q;                 }             } else {                 p->next = (PolyNode*)malloc(sizeof(PolyNode));                 p = p->next;                 p->coef = coef;                 p->expn = expn;                 p->next = NULL;             }         } else {             head->next = (PolyNode*)malloc(sizeof(PolyNode));             head->next->coef = coef;             head->next->expn = expn;             head->next->next = NULL;         }     }     return head; } Polynomial multiply(Polynomial poly, float coef, int expn) {  //多项式与指定单项式相乘，该单项式为 coefx^expn     PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));     p = Poly;     q = poly->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = (q->coef*coef);         p->expn = (q->expn + expn);         q = q->next;     }     p->next = NULL;     return Poly; } void add(Polynomial poly1, Polynomial poly2) {  //把 poly2 加到 poly1 上     PolyNode *p, *q, *r;     r = poly1;     p = poly1->next;  //指向第一个节点     q = poly2->next;     poly2->next = NULL;     while (p && q) {         if (p->expn > q->expn) {             r->next = p;             p = p->next;             r = r->next;         } else if (p->expn < q->expn) {             r->next = q;             q = q->next;             r = r->next;         } else {             PolyNode *t;             p->coef += q->coef;             if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0             {                 r->next = p;                 r = r->next;                 p = p->next;             } else {                 t = p;                 p = p->next;                 free(t);             }             t = q;             q = q->next;             free(t);         }     }     if (p)         r->next = p;     if (q)         r->next = q; } Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式     //把poly2的系数取相反数，形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点     PolyNode *p, *q;     p = poly;     q = poly2->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = -(q->coef);  //系数取反         p->expn = q->expn;         q = q->next;     }     p->next = NULL;     add(poly, poly1);  //利用加法     return poly; } Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点     poly->next = NULL;     PolyNode *p, *q, *r;     r = poly;     p = poly1->next;     q = poly2->next;     while (p&&q) {         if (p->expn > q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = p->coef;             r->expn = p->expn;             p = p->next;         } else if (p->expn < q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = q->coef;             r->expn = q->expn;             q = q->next;         } else {             float m = p->coef + q->coef;             if (!(m > -0.000001 && m < 0.000001)) {                 r->next = (PolyNode*)malloc(sizeof(PolyNode));                 r = r->next;                 r->coef = m;                 r->expn = p->expn;             }             q = q->next;             p = p->next;         }     }     while (p) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = p->coef;         r->expn = p->expn;         p = p->next;     }     while (q) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = q->coef;         r->expn = q->expn;         q = q->next;     }     r->next = NULL;     return poly; } Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) {  //多项式相乘     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点     poly->next = NULL;     PolyNode *p;     p = poly2->next;     while (p) {         add(poly, multiply(poly1, p->coef, p->expn));         p = p->next;     }     return poly; } void printPoly(Polynomial poly) {  //打印多项式     if (poly && poly->next) {         PolyNode *p = poly->next;  //p指向第一个节点         while (p->next) {             printf("%gx^%d", p->coef, p->expn);             p = p->next;             if (p && (p->coef > 0))                 printf("+");         }         if (p->expn == 0)             printf("%g", p->coef);   //打印常数项         else             printf("%gx^%d", p->coef, p->expn);         printf("\n");     } } void freePoly(Polynomial poly) {  //释放内存     if (poly && poly->next) {         PolyNode *p, *q;         p = poly;         while (p) {             q = p->next;             free(p);             p = q;         }     }     poly = NULL; } int main() {     printf("用链表实现多项式的加减法\n");     Polynomial poly1, poly2, poly3;     printf("创建多项式一\n");     poly1 = createPolynomial();     printf("多项式一：\n");     printPoly(poly1);     printf("创建多项式二\n");     poly2 = createPolynomial();     printf("多项式二：\n");     printPoly(poly2);     printf("两多项式相加，和为：\n");     poly3 = polyAdd(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两个多项式相乘，积为：\n");     poly3 = polyMultiply(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两多项式相减，差为：\n");     poly3 = polySubtract(poly1, poly2);     printPoly(poly3);     freePoly(poly1);     freePoly(poly2);     freePoly(poly3);     system("pause");     return 0; }

user: user-2
content: #include<stdio.h>  typedef struct{     int x;     int m; }List;  void fun_1(List *t,int n) {     int i,j,h,max;     List temp;      for(i = 0; i < n; i++)     {         h = i;         max = t[i].m;         for(j = i + 1; j < n; j++)         {             if(t[j].m > max)             {                 max = t[j].m;                 h = j;             }         }         if(h != i)         {             temp=t[i];             t[i]=t[h];             t[h]=temp;         }     } }   void print_list(List *l, int len) {     int i;      for (i = 0; i < len; i++)         printf("[%d, %d]\t", l[i].x, l[i].m);     putchar(10);  } int  fun(List *a,List *b,List *c,int k,int z) {      int i = 0,j = 0, q = 0;      while(i<k && j<z &&(a[i].m>=0 && b[j].m>=0))     {         if(a[i].m>b[j].m) {c[q++]=a[i]; i++;}         if(a[i].m<b[j].m) {c[q++]=b[j]; j++;}         if(a[i].m==b[j].m)         {             if(a[i].x + b[j].x == 0) {                 i++; j++;             } else {                 c[q].x=a[i].x+b[j].x;                 c[q++].m=a[i].m;                 i++;                 j++;             }          }     }     while(i<k && a[i].m>=0) {         c[q++] = a[i];         i++;     }     while(j<z && b[j].m>=0) {         c[q++]=b[j];         j++;     }      return q; } int main() {     int n, i, j, d, k=0,z=0;     List a[300], b[300], c[600];      scanf("%d", &n);      while(n--)     {         for(i = 0; i < 300; i++)         {             scanf("%d%d", &a[i].x, &a[i].m);             if(a[i].m<0)                 break;             k++;         }         for(j = 0; j < 300; j++)         {             scanf("%d%d",&b[j].x, &b[j].m);             if(b[j].m<0)                 break;             z++;         }         fun_1(a, k);         //print_list(a, k);         fun_1(b, z);         //print_list(b, z);         d = fun(a, b, c, k, z);         /*         for(i = 0; i < d; i++) {             printf("[%d %d] ",c[i].x,c[i].m);         }         printf("\n");         */         print_list(c, d);         /*Initize*/         k = 0;         z = 0;     }     return 0; }  两个问题， 1. a和b的计数，需要放在判断m的值是否为0之后，这样就不计数最后一个输入了。 2. k, z需要重新初始化（若n >= 2)时。

user: user-2
content: #include<stdio.h>  typedef struct{     int x;     int m; }List;  void select_sort(List *t, int n) {     int i, j, h, max;     List temp;      for(i = 0; i < n; i++)     {         h = i;         max = t[i].m;         for(j = i + 1; j < n; j++) {             if(t[j].m > max) {                 max = t[j].m;                 h = j;             }         }         if(h != i)         {             temp = t[i];             t[i] = t[h];             t[h] = temp;         }     } }   void print_list(List *l, int len) {     int i;      for (i = 0; i < len; i++)         printf("[%3d, %3d]\t", l[i].x, l[i].m);     putchar(10);  } int merge(List *a, List *b, List *c, int alen, int blen) {      int i = 0, j = 0, k, cnt = 0;      while(i < alen && j < blen &&(a[i].m >= 0 && b[j].m >= 0))     {         if(a[i].m > b[j].m) {             for (k = 0; k < cnt; k++)                 if (c[k].m == a[i].m) {                     c[k].x += a[i].x;                     i++;                     break;                 }              if (k >= cnt) {                 c[cnt] = a[i];                 i++;                 cnt++;             }         }         if(a[i].m < b[j].m) {             for (k = 0; k < cnt; k++)                 if (c[k].m == b[j].m) {                     c[k].x += b[j].x;                     j++;                     break;                 }             if (k >= cnt) {                 c[cnt] = b[j];                 j++;                 cnt++;             }         }         if(a[i].m == b[j].m) {             if(a[i].x + b[j].x == 0) {                 i++;                 j++;             } else {                 c[cnt].x = a[i].x + b[j].x;                 c[cnt++].m = a[i].m;                 i++;                 j++;             }          }     }     while(i < alen && a[i].m >= 0) {         c[cnt++] = a[i];         i++;     }     while(j < blen && b[j].m>=0) {         c[cnt++]=b[j];         j++;     }      return cnt; } int main() {     int n, i, j, d, k = 0, z = 0;     List a[300], b[300], c[600];      scanf("%d", &n);      while(n--)     {         for(i = 0; i < 300; i++)         {             scanf("%d%d", &a[i].x, &a[i].m);             if(a[i].m<0)                 break;             k++;         }         for(j = 0; j < 300; j++)         {             scanf("%d%d",&b[j].x, &b[j].m);             if(b[j].m<0)                 break;             z++;         }         select_sort(a, k);         printf("List one: ");         print_list(a, k);         select_sort(b, z);         printf("List two: ");         print_list(b, z);         d = merge(a, b, c, k, z);         for(i = 0; i < d; i++) {             printf("[%d %d] ",c[i].x,c[i].m);         }         printf("\n");         //print_list(c, d);         /*Initize*/         k = 0;         z = 0;     }     return 0; } 这个版本是可以的，主要是输入的第二组数据有重复的（相同的幂）的一组数据。 这个版本对这种情况做了判断。测试一下吧

user: user-0
content: 引用 1 楼 cfjtaishan 的回复:#include<stdio.h>  typedef struct{     int x;     int m; }List;  void fun_1(List *t,int n) {     int i,j,h,max;     List temp;      for(i = 0; i < n; i++)     {         h = i;         max = t[i].m;         for(j = i + 1; j < n; j++)         {             if(t[j].m > max)             {                 max = t[j].m;                 h = j;             }         }         if(h != i)         {             temp=t[i];             t[i]=t[h];             t[h]=temp;         }     } }   void print_list(List *l, int len) {     int i;      for (i = 0; i < len; i++)         printf("[%d, %d]\t", l[i].x, l[i].m);     putchar(10);  } int  fun(List *a,List *b,List *c,int k,int z) {      int i = 0,j = 0, q = 0;      while(i<k && j<z &&(a[i].m>=0 && b[j].m>=0))     {         if(a[i].m>b[j].m) {c[q++]=a[i]; i++;}         if(a[i].m<b[j].m) {c[q++]=b[j]; j++;}         if(a[i].m==b[j].m)         {             if(a[i].x + b[j].x == 0) {                 i++; j++;             } else {                 c[q].x=a[i].x+b[j].x;                 c[q++].m=a[i].m;                 i++;                 j++;             }          }     }     while(i<k && a[i].m>=0) {         c[q++] = a[i];         i++;     }     while(j<z && b[j].m>=0) {         c[q++]=b[j];         j++;     }      return q; } int main() {     int n, i, j, d, k=0,z=0;     List a[300], b[300], c[600];      scanf("%d", &n);      while(n--)     {         for(i = 0; i < 300; i++)         {             scanf("%d%d", &a[i].x, &a[i].m);             if(a[i].m<0)                 break;             k++;         }         for(j = 0; j < 300; j++)         {             scanf("%d%d",&b[j].x, &b[j].m);             if(b[j].m<0)                 break;             z++;         }         fun_1(a, k);         //print_list(a, k);         fun_1(b, z);         //print_list(b, z);         d = fun(a, b, c, k, z);         /*         for(i = 0; i < d; i++) {             printf("[%d %d] ",c[i].x,c[i].m);         }         printf("\n");         */         print_list(c, d);         /*Initize*/         k = 0;         z = 0;     }     return 0; }  两个问题， 1. a和b的计数，需要放在判断m的值是否为0之后，这样就不计数最后一个输入了。 2. k, z需要重新初始化（若n >= 2)时。  我跟着你的改了，但是我的输出还和原来一样，就是每组两个多项式，但是第二组的第一个多项式，有一个幂数是5的，和第二个多项式里的第一个幂数为5的加上了，但是之后的就加不上了 这是第二组的的输入： -1 17 2 23 22 2 6 8 -4 7 -18 0 1 5 21 4 0 -1 12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2 19 9 -7 这是标准输出： [ 2 23 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 6 8 ] [ 8 7 ] [ -3 5 ] [ 44 4 ] [ 22 2 ] [ -18 0 ] 下面是我的输出： [2 23] [2 19] [2 17] [15 10] [6 8] [8 7] [-6 5] [-10 5] [13 5] [44 4] [22 2] [-18 0]