user-0 提问: 一元稀疏多项式计算器   输出多项式  只能输出两项   求解!!!1!!!!1

回复:
user: user-0
content: 明白了，谢谢！！

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: 另外，释放链表空间不是     free(List1);     free(List2); 这么简单。而是遍历链表一个个的释放。

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<math.h>  typedef struct LNode {     float coef;            //系数     int expn;               //指数     struct  LNode *next; } LNode,*Linklist;  Linklist List1=NULL; Linklist List2=NULL;  Linklist in(Linklist L) {   //多项式的建立     Linklist p;     float a;     int b;     Linklist head=(Linklist)malloc(sizeof(LNode));     L=head;     head->expn=0;//头结点放的是多项式的项数     p=head;     p->next=NULL;     printf("请输入一个多项式：\n");     //while(scanf("%f%d",&a,&b) && (a||b)){     while (scanf("%f%d", &a,&b) == 2 && (fabs(a) > 1e-6 || b != 0)) {         //printf("+++++%f %d\n", a, b);         (head->expn)++;         //printf("%3d\n",L->expn);         Linklist q=(Linklist)malloc(sizeof(LNode));          q->coef=a;         q->expn=b;         q->next = NULL;          if(head->next == NULL){             head->next = q;             //q->next=NULL;             //p=head;         } else {             p = head;             while (p->next) {                 if (p->next->expn <= q->expn) {                     q->next = p->next;                     p->next = q;                     break;                 }                 p = p->next;             }             if (p->next == NULL)                 p->next = q; #if 0             if(p->next->expn <= q->expn){                 q->next=p->next;                 p->next=q;                 p=head;             }             else{                 p=p->next;             } #endif         }         printf("end\n");     }     printf("您已成功输入一个多项式！！\n");     return L; } void Input(){           //输入多项式     List1=in(List1);     List2=in(List2); } void Out(Linklist L){     //输出多项式     Linklist p;     p=L->next;     printf("共有%3d项  ",L->expn);     while(p!=NULL)//按指数由大到小输出     {         printf("%0.1f%3d  ",p->coef,p->expn);         p=p->next;}     printf("\n"); } void Output(){     printf("多项式a为：\n");     Out(List1);     printf("多项式b为：\n");     Out(List2); } void Add(){     Linklist p1,p2;     p1=List1->next;     p2=List2->next;     printf("相加后的多项式为：\n");     while(p1&&p2){         if(p1->expn>p2->expn){             printf("%0.1f%3d  ",p1->coef,p1->expn);             p1=p1->next;         }         else if(p1->expn<p2->expn){             printf("%0.1f%3d  ",p1->coef,p2->expn);             p2=p2->next;         }         else{             printf("%0.1f%3d  ",p1->coef+p2->coef,p1->expn);             p1=p1->next;             p2=p2->next;         }     }     if(p1==NULL){         while(p2){             printf("%0.1f%3d  ",p2->coef,p2->expn);             p2=p2->next;         }     }     else{         while(p1){             printf("%0.1f%3d  ",p1->coef,p1->expn);             p1=p1->next;         }     }     printf("\n"); } void Sub(){     Linklist p1,p2;     p1=List1->next;     p2=List2->next;     printf("相减后的多项式为：\n");     while(p1&&p2){         if(p1->expn>p2->expn){             printf("%0.1f%3d  ",p1->coef,p1->expn);             p1=p1->next;         }         else if(p1->expn<p2->expn){             printf("%0.1f%3d  ",p1->coef,p2->expn);             p2=p2->next;         }         else{             if(p1->coef-p2->coef!=0)                 printf("%0.1f%3d  ",p1->coef-p2->coef,p1->expn);             p1=p1->next;             p2=p2->next;         }     }     if(p1==NULL){         while(p2){             printf("%0.1f%3d  ",p2->coef,p2->expn);             p2=p2->next;         }     }     else{         while(p1){             printf("%0.1f%3d  ",p1->coef,p1->expn);             p1=p1->next;         }     }     printf("\n"); } void Calc(){     int x;     double sum=0;     Linklist p;     printf("请输入x的值：");     scanf("%d",&x);     p=List1->next;     while(p){         sum+=p->coef*pow(x,p->expn);         p=p->next;     }     printf("多项式a在x处的值为：%0.1lf",sum);     printf("\n"); } int main(){       int num;     while(1){          printf("------------------------------------------\n");         printf("1---------输入并建立多项式(格式为：系数 指数,以0 0结束)\n");         printf("2---------输出多项式\n");         printf("3---------多项式相加\n");         printf("4---------多项式相减\n");         printf("5---------计算多项式在x处的值\n");         printf("0---------退出\n");         printf("-------------------------------------------\n");         printf("请输入您要选择的功能：");         scanf("%d",&num);         switch(num){             case 1:Input();break;             case 2:Output();break;             case 3:Add();break;             case 4:Sub();break;             case 5:Calc();break;             case 0:{                        printf(" 谢谢使用!!!\n");                        exit(1);                    }             default:printf("输入错误，请重新输入");         }     }     free(List1);     free(List2);     return 0; }  参考一下吧  in函数内的逻辑有问题。尤其是在新节点加入时。建议对比代码看一下自己的问题。