求对该程序进行修改

taka8rie 2017-10-19 04:53:05
写了一个程序,用来进行一元多项式的相加。现在运行出了问题了,不知道哪里错了,可以帮我看看吗?
以下是代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node {
int coef;
int exp;
struct node*next;
};

struct node *creatlist()
{
struct node *head;
struct node *p = NULL;
struct node *q = NULL;
int x1, x2;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
q = head;

printf("input coef:");
scanf("%d", &x1);
printf("input exp:");
scanf("%d", &x2);
while (x1 != 0)
{
p = (struct node *)malloc(sizeof(struct node));
p->coef = x1;
p->exp = x2;
p->next = NULL;
q->next = p;
q = q->next;
printf("input coef:");
scanf("%d", &x1);
printf("input exp:");
scanf("%d", &x2);
}
return head;
}

void printlist(struct node *list) {
struct node *p = list->next;
while (p != NULL)
{
printf("coef=%d exp=%d", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
struct node *add(struct node *list1, struct node *list2)
{
struct node *p = list1->next;
struct node *q = list1->next;
int sum; struct node *u;
struct node *pre = list1;
struct node *t;
while (p != NULL&&q != NULL)
{
if (p->exp < q->exp)
p = p->next;
else if (p->exp == q->exp)
{
sum = p->coef + q->coef;
if (sum != 0)
{
p->coef = sum;
u = p; p = p->next;
free(u);
}
else if (sum == 0)
{
u = q, t = p;
p = p->next;
q = q->next;
free(u); free(t);
}
}
else //p的结点指数比q的小,将q的结点加入p;
{
u = q->next;
q->next = p;
pre->next = q;
pre = q;
q = u;
}if (q);
{
pre->next = q;
}free(list2);
}




}

void main()
{
struct node *list1 = NULL;
struct node *list2 = NULL;
struct node *resultlist = NULL;

list1 = creatlist();
list2 = creatlist();
resultlist = creatlist();

resultlist = add(list1, list2);
printlist(resultlist);
}


...全文
168 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
taka8rie 2017-10-22
  • 打赏
  • 举报
回复
谢谢大家的帮助,我还不知道怎么样来单独回复你们,我已经找到哪里错了。再次感谢。
kugeniha 2017-10-20
  • 打赏
  • 举报
回复
add 函数 p q 指向同一地址
赵4老师 2017-10-20
  • 打赏
  • 举报
回复
仅供参考:
//链表实现一元多项式的加法减法乘法
#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;
}
自信男孩 2017-10-19
  • 打赏
  • 举报
回复
删除节点时,需要知道要删除的节点的前一个节点,然后将要删除的节点的next放到前一个节点的next上; 多项式的创建和输出都没发现问题,求和处理有问题,建议参考一下这个程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OK      1
#define ERROR   0
#define TRUE    1
#define FALSE   0

typedef int Status;
typedef int Elemtype;

typedef struct Node{
    Elemtype cof;
    Elemtype inx;
    struct Node *next;
}Node, *LinkList;

Status visit(Elemtype a, Elemtype b)
{
    printf("%d %d", a, b);

    return OK;
}

Status InitList(LinkList *L)
{
    (*L) = (LinkList)malloc(sizeof(Node));
    if (!(*L))
        return ERROR;

    (*L)->next = NULL;

    return OK;
}

/* count the length */
int ListLength(LinkList L)
{
    int i = 0;
    LinkList p;

    p = L->next;
    while (p) {
        i++;
        p = p->next;
    }

    return i;
}


/* get the data of no. i nodes, i start from 1 */
Status GetElem(LinkList L, int i, Elemtype *cof, Elemtype *inx)
{
    LinkList p;
    int j;

    p = L;
    j = 1;
    while (p && j < i) {
        p = p->next;
        j++;
    }
    if (!p)
        return ERROR;

    *cof=p->next->cof;
    *inx=p->next->inx;

    return OK;
}

Status ListInsert(LinkList *L, int i, Elemtype cof, Elemtype inx)
{
    if(i < 1 || i > ListLength(*L) + 1)
        return ERROR;

    int j;
    LinkList p, q;


    p = *L;
    j = 1;
    while (p && j < i) {
        p = p->next;
        j++;
    }
    if (!p || j > i)
        return ERROR;

    q = (LinkList)malloc(sizeof(Node));
    if (!q) {
        fprintf(stderr, "malloc error!\n");
        return ERROR;
    }
    q->cof = cof;
    q->inx = inx;

    q->next = p->next;
    p->next = q;

    return OK;
}


/*打印L中系数非零的项*/
Status ListTraverse(LinkList L)
{
    LinkList p;

    p = L->next;
    while (p) {
        if (p->cof != 0) {
            visit(p->cof, p->inx);
            printf(" ");
        }
        p = p->next;
    }
    printf("\n");

    return OK;
}

Status PlusList(LinkList *L3,LinkList L1,LinkList L2)
{
    int i, j, k;
    Elemtype cof1, inx1;
    Elemtype cof2, inx2;

    k = i = j = 1;
    while ((i <= ListLength(L1)) && (j <= ListLength(L2))) {
        GetElem(L1, i, &cof1, &inx1);
        GetElem(L2, j, &cof2, &inx2);
        if (inx1 == inx2) {
            ListInsert(L3, k++, cof1 + cof2, inx1);
            i++;
            j++;
        } else if (inx1 > inx2) {
            ListInsert(L3, k++, cof1, inx1);
            i++;
        } else {
            ListInsert(L3, k++, cof2, inx2);
            j++;
        }
    }

    return OK;
}
int main()
{
    LinkList L1, L2, L3;
    int cofn, inxn;
    int i;

    InitList(&L1);
    InitList(&L2);
    InitList(&L3);

    i = 1;
    while(scanf("%d%d", &cofn, &inxn) == 2 && inxn != -1){
        ListInsert(&L1, i++, cofn, inxn);
    }
    i = 1;
    while(scanf("%d%d", &cofn, &inxn) == 2 && cofn != -1){
        ListInsert(&L2, i++, cofn, inxn);
    }
    PlusList(&L3, L1, L2);
    ListTraverse(L3);

    return 0;
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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