用C设计一个简单的一元稀疏多项式加减计算器,想知道代码问题出现在哪里

qq2207099072 2016-09-24 08:43:55
编译器经常提醒我poly1是个空指针,而且就算能成功运行结果也不对,总是少几项,求帮忙解决一下
代码如下:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct NODE
{
float cofe;
int exp;
struct NODE*link;
}Node;
//初始化链表,默认输入指数的顺序为升序
Node*Creat_node(void)
{
int i;
int j;
int exp;
float cofe;
Node*head;
Node*current;
Node*next;
printf("请输入多项式的项数:");
scanf("%d",&i);
if (i == 0)
return NULL;
head = (Node*)malloc(sizeof(Node));
if (head == NULL)
return NULL;
current = head;
for (j = 1; j <= i; j++)
{
printf("请输入第%d项的系数:",j);
scanf("%f", &cofe);
printf("请输入第%d项的指数:",j);
scanf("%d", &exp);
next= (Node*)malloc(sizeof(Node));
current->link=next;
current = next;
current->exp = exp;
current->cofe = cofe;
}
current->link = NULL;
return head;
}
//多项式相加,按照升序排列
Node*add(Node*head1, Node*head2)
{
Node*poly1;
Node*poly2;
Node*head;
Node*last;
Node*save;
poly1 = head1->link;
poly2 = head2->link;
head = head1;
last = head;
while (poly1!= NULL && poly2!= NULL)
{
if ((poly1->exp)< (poly2->exp))
{
last->link = poly1;
last = poly1;
poly1 = poly1->link;
}
if ((poly1->exp) > (poly2->exp))
{
last->link = poly2;
last = poly2;
poly2 = poly2->link;
}
if ((poly1->exp) == (poly2->exp))
{
poly1->cofe = (poly1->cofe) + (poly2->cofe);
if (poly1->cofe == 0)
{
save = poly1;
poly1 = poly1->link;
free(save);
save = poly2;
poly2 = poly2->link;
free(save);
continue;
}
else
{
last->link = poly1;
last = poly1;
poly1 = poly1->link;
save = poly2;
poly2 = poly2->link;
free(save);
}
}
}
if (poly1 != NULL)
last->link = poly1;
else
last->link = poly2;
return head;
}
//多项式相减,只需要在原来的多项式相加的基础上改动一下
Node*sub(Node*head1, Node*head2)
{
Node*current;
Node*head;
current = head2->link;
while (current != NULL)
{
current->cofe = -(current->cofe);
current = current->link;
}
head = add(head1, head2);
return head;
}
//输出函数部分
void output(Node *head)
{
Node *p;
int temp = 1;
p = head->link;
while (p != NULL)
{
if (temp == 1)
{
if (p->exp == 0)
printf("%.2f", p->cofe);
else
{
if (p->cofe == 1) printf("x^%d", p->exp);
else if (p->cofe== -1)
printf("-x^%d", p->exp);
else
printf("%.2fx^%d", p->cofe, p->exp);
}
temp = 0;
}
else if (temp == 0)
{
if (p->exp == 0)
{
if (p->cofe>0)
printf("+%.2f", p->cofe);
else if (p->cofe<0)
printf("%.2f", p->cofe);
}
else
{
if (p->cofe == 1)
printf("+x^%d", p->exp);
else if (p->cofe == -1)
printf("-x^%d", p->exp);
else if (p->cofe>0)
printf("+%.2fx^%d", p->cofe, p->exp);
else if (p->cofe<0)
printf("%.2fx^%d", p->cofe, p->exp);
}
}
p = p->link;
}
printf("\n");
}



//主函数部分
int main(void)
{
Node*head1;
Node*head2;
Node*result;
int i;
int choice;
printf("输入第一个多项式\n");
head1 = Creat_node();
printf("输入第二个多项式\n");
head2 = Creat_node();
if (head1 == NULL || head2 == NULL)
return EXIT_FAILURE;
printf("两个多项式相加为:");
result = add(head1, head2);
output(result);
getch();
return EXIT_SUCCESS;
}
...全文
951 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2016-09-27
  • 打赏
  • 举报
回复
#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef struct NODE { float cofe; int exp; struct NODE*link; }Node; //初始化链表,默认输入指数的顺序为升序 Node*Creat_node(void) { int i; int j; int exp; float cofe; Node*head; Node*current; Node*next; printf("请输入多项式的项数:"); scanf("%d",&i); if (i == 0) return NULL; head = (Node*)malloc(sizeof (Node)); if (head == NULL) return  NULL; current = head; for (j = 1; j <= i; j++) { printf("请输入第%d项的系数:",j); scanf("%f", &cofe); getchar(); printf("请输入第%d项的指数:",j); scanf("%d", &exp); getchar(); next= (Node*)malloc(sizeof (Node)); if (next == NULL) return NULL; current->link=next; current = next; current->exp = exp; current->cofe = cofe; } current->link = NULL; return head; } //多项式相加,按照升序排列 Node*add(Node*head1, Node*head2) { Node*poly1; Node*poly2; Node*last; Node*save; float x; poly1=head1->link; poly2=head2->link; last = head1; while (poly1!= NULL && poly2!= NULL) { printf("poly1->exp=%d,poly2->exp=%d\n",poly1->exp,poly2->exp); if ((poly1->exp)< (poly2->exp)) { last->link = poly1; last=poly1; poly1=poly1->link; continue; } if ((poly1->exp) > (poly2->exp)) { last->link = poly2; last=poly2; poly2=poly2->link; continue; } if ((poly1->exp) == (poly2->exp)) { x = (poly1->cofe) + (poly2->cofe); if (x == 0) { save = poly1; poly1=poly1->link; free(save); save = poly2; poly2=poly2->link; continue; } else { poly1->cofe = x; last->link = poly1; last = poly1; poly1=poly1->link; save = poly2; poly2=poly2->link; continue; } } } while (poly1 != NULL) { last->link = poly1; last=poly1; poly1=poly1->link; } while (poly2!=NULL) { last->link=poly2; last=poly2; poly2=poly2->link; } return head1; } //多项式相减,只需要在原来的多项式相加的基础上改动一下 Node*sub(Node*head1, Node*head2) { Node*current; Node*head; current = head2->link; while (current != NULL) { current->cofe = -(current->cofe); current = current->link; } head = add(head1, head2); return head; } //输出函数部分 void output(Node *head) { Node *p; int temp = 1; p = head->link; while (p != NULL) { if (temp == 1) { if (p->exp == 0) printf("%.2f", p->cofe); else { if (p->cofe == 1)  printf("x^%d", p->exp); else if (p->cofe== -1) printf("-x^%d", p->exp); else printf("%.2fx^%d", p->cofe, p->exp); } temp = 0; } else if (temp == 0) { if (p->exp == 0) { if (p->cofe>0) printf("+%.2f", p->cofe); else if (p->cofe<0) printf("%.2f", p->cofe); } else { if (p->cofe == 1) printf("+x^%d", p->exp); else if (p->cofe == -1) printf("-x^%d", p->exp); else if (p->cofe>0) printf("+%.2fx^%d", p->cofe, p->exp); else if (p->cofe<0) printf("%.2fx^%d", p->cofe, p->exp); } } p = p->link; } printf("\n"); } //主函数部分 int main() { Node*head1; Node*head2; Node*result; printf("输入第一个多项式\n"); head1 = Creat_node(); //output(head1); printf("输入第二个多项式\n"); head2 = Creat_node(); if (head1 == NULL || head2 == NULL) return 0; result = add(head1, head2); printf("两个多项式相加为:"); output(result); return 1; } /*注意在每个if后面加一句continue;就可以了*/
小灸舞 2016-09-26
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
赵4老师 2016-09-25
  • 打赏
  • 举报
回复
仅供参考:
//链表实现一元多项式的加法减法乘法
#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;
}
paschen 2016-09-24
  • 打赏
  • 举报
回复
单步跟踪程序运行,观察分析每一步执行结果与你预期是否一致

33,319

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧