c - 我的多项式加法 c 代码有什么问题?

标签 c polynomials

我编写了 C 程序,其中应该添加两个多项式。我在 Kali Linux 2.0 操作系统中编写了这个程序。当我执行该程序时,我没有得到所需的输出。相反,我得到了这个-

Polynomial 1

How many no. terms do you want to enter? 1

Enter coefficient for term 1: 2
Enter exponent for term 1: 3

Polynomial 2

How many no. terms do you want to enter? 1

Enter coefficient for term 1: 2
Enter exponent for term 1: 3


Polynomial 1=
Polynomial 2=
Sum of the two polynomials is

程序代码如下-

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int exp,coeff;
    struct node *next;
}poly;

poly *headA,*headB,*headC;
poly *lastA,*lastB,*lastC;

void insert(poly*,poly*,poly *);
void input(poly *,poly *);
void display(poly *);

void insert(poly *new,poly *head,poly *last)
{
    poly *p,*q;
    if(head==NULL&&last==NULL)  //setting the start
    {
        head=last=new;
        return;
    }
    p=head;
    q=NULL;
    while(new->exp<p->exp)  
    {
        q=p;
        p=p->next;
    }
    if(p->exp==new->exp)    //if exponents are equal
        p->coeff=p->coeff+new->coeff;
    else
    {
        if(q!=NULL) //insertion in middle
        {
            q->next=new;
            new->next=p;
        }
        else if(q==NULL)    //insertion at beginning
        {
            new->next=head;
            head=new;
        }
        else if(p==NULL)    //insertion at the end
        {
            last->next=new;
            last=new;
        }
    }
}

void input(poly *head,poly *last)
{
    int i,n,c,e;
    poly *new;
    new=(poly *)malloc(sizeof(poly));
    printf("How many no. terms do you want to enter? ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\nEnter coefficient for term %d: ",i);
        scanf("%d",&new->coeff);
        printf("Enter exponent for term %d: ",i);
        scanf("%d",&new->exp);
        new->next=NULL;
        insert(new,head,last);
        insert(new,headC,lastC);
    }
}

void display(poly *start)
{
    poly *p;
    p=start;
    while(p!=NULL)
    {
        printf("(%dx^%d)+",p->coeff,p->exp);
        p=p->next;
    }
    printf("\b");
}

void main()
{
    system("clear");

    headA=(poly *)malloc(sizeof(poly));
    headB=(poly *)malloc(sizeof(poly));
    headC=(poly *)malloc(sizeof(poly));
    lastA=(poly *)malloc(sizeof(poly));
    lastB=(poly *)malloc(sizeof(poly));
    lastC=(poly *)malloc(sizeof(poly));

    headA=headB=headC=NULL;
    lastA=lastB=lastC=NULL;

    printf("Polynomial 1\n\n");
    input(headA,lastA);
    printf("\nPolynomial 2\n\n");
    input(headB,lastB);
    printf("\n\nPolynomial 1=");
    display(headA);
    printf("\nPolynomial 2=");
    display(headB);
    printf("\nSum of the two polynomials is=");
    display(headC);
}

编辑: 阅读评论后,我对代码进行了一些更改。现在输出是-

Polynomial 1

How many no. terms do you want to enter? 1

Enter coefficient for term 1: 5
Enter exponent for term 1: 6

Polynomial 2

How many no. terms do you want to enter? 2

Enter coefficient for term 1: 6
Enter exponent for term 1: 5

Enter coefficient for term 2: 7
Enter exponent for term 2: 8


Polynomial 1=(0x^0)+
Polynomial 2=(0x^0)+
Sum of the two polynomials is=(0x^0)

现在的程序代码是-

poly *insert(poly*,poly*,poly *);
poly *input(poly *,poly *);
void display(poly *);

poly *insert(poly *new,poly *head,poly *last)
{
    poly *p,*q;
    if(head==NULL&&last==NULL)
    {
        head=last=new;
        return;
    }
    p=head;
    q=NULL;
    while(new->exp<p->exp)
    {
        q=p;
        p=p->next;
    }
    if(p->exp==new->exp)
        p->coeff=p->coeff+new->coeff;
    else
    {
        if(q!=NULL)
        {
            q->next=new;
            new->next=p;
        }
        else if(q==NULL)
        {
            new->next=head;
            head=new;
        }
        else if(p==NULL)
        {
            last->next=new;
            last=new;
        }
    }
    return head;
}

poly *input(poly *head,poly *last)
{
    int i,n,c,e;
    poly *new;
    printf("How many no. terms do you want to enter? ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        new=(poly *)malloc(sizeof(poly));
        if(new==NULL)
        {
            printf("Allocation Error!!");
            break;
        }
        printf("\nEnter coefficient for term %d: ",i);
        scanf("%d",&new->coeff);
        printf("Enter exponent for term %d: ",i);
        scanf("%d",&new->exp);
        new->next=NULL;
        head=insert(new,head,last);
        headC=insert(new,headC,lastC);
        free(new);
    }
    return head;
}

void display(poly *start)
{
    poly *p;
    p=start;
    while(p!=NULL)
    {
        printf("(%dx^%d)+",p->coeff,p->exp);
        p=p->next;
    }
    printf("\b");
}

void main()
{
    system("clear");

    headA=(poly *)malloc(sizeof(poly));
    headB=(poly *)malloc(sizeof(poly));
    headC=(poly *)malloc(sizeof(poly));
    lastA=(poly *)malloc(sizeof(poly));
    lastB=(poly *)malloc(sizeof(poly));
    lastC=(poly *)malloc(sizeof(poly));

    if(headA==NULL||headB==NULL||headC==NULL||lastA==NULL||lastB==NULL||lastC==NULL)
    {
        printf("Allocation failure!!!");
        return;
    }

    headA=headB=headC=NULL;
    lastA=lastB=lastC=NULL;

    printf("Polynomial 1\n\n");
    headA=input(headA,lastA);
    printf("\nPolynomial 2\n\n");
    headB=input(headB,lastB);
    printf("\n\nPolynomial 1=");
    display(headA);
    printf("\nPolynomial 2=");
    display(headB);
    printf("\nSum of the two polynomials is=");
    display(headC);
}

最佳答案

这是一个工作版本:

#include <stdio.h>
#include <stdlib.h>

term_t 描述多项式中的单个项。

typedef struct term {
    int exp;
    int coeff;
    struct term *next;
} term_t;

poly_t 描述了整个多项式,它只是一个项列表。 last 指针并不是真正必要的,因为我们总是从头开始遍历列表。

typedef struct poly {
    term_t *head;
} poly_t;

我们需要一种通过将项列表设置为空来初始化多项式的方法。我们还需要一种通过释放多项式的所有项来销毁多项式的方法。

void init_poly(poly_t *poly)
{
    poly->head = NULL;
}

void destroy_poly(poly_t *poly)
{
    term_t *term = poly->head;
    while (term != NULL) {
        term_t *nextTerm = term->next;
        free (term);
        term = nextTerm;
    }
    poly->head = NULL;
}

我们还会发现有必要能够克隆术语(创建现有术语的副本)。请注意,在 C 中,无需强制转换 malloc 的返回值。

term_t *clone_term(term_t *term)
{
    term_t *new_term;

    if ((new_term = malloc(sizeof *new_term)) == NULL) {
        printf("Allocation failure!!!\n");
        return NULL;
    }

    new_term->coeff = term->coeff;
    new_term->exp = term->exp;
    new_term->next = NULL;
    return new_term;
}

我们还需要一种将项插入多项式的方法。项按指数排序(最高指数在列表中排在前面),但如果具有匹配指数的项已经在多项式中,我们只需添加到系数即可。我们可以通过维护指向下一个指针的指针来消除所有特殊情况代码,我们将修改该指针以指向新术语。

void insert_term(poly_t *poly, term_t *term)
{
    term_t **nextPtr = &poly->head;
    term_t *nextTerm;

    while ((nextTerm = *nextPtr) != NULL) {
        if (nextTerm->exp == term->exp) {
            /* we found an existing term with a matching exponent */
            nextTerm->coeff += term->coeff;
            free (term); /* we don't need term so it must be free'd */
            return;
        }
        else if (nextTerm->exp < term->exp) {
            /* the next term has a lower exponent, so we stop here */
            break;
        }
        nextPtr = &nextTerm->next;
    }

    term->next = nextTerm;
    *nextPtr = term;
}

input_poly 函数仅负责输入单个多项式。它为插入的每个术语分配一个新的 term_t

int input_poly(poly_t *poly)
{
    int i, n;

    printf("How many terms do you want to enter? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        term_t *term;

        if ((term = malloc(sizeof *term)) == NULL) {
            printf("Allocation failure!!!\n");
            return -1;
        }

        printf("\nEnter coefficient for term %d: ", i+1);
        scanf("%d", &term->coeff);

        printf("Enter exponent for term %d: ", i+1);
        scanf("%d", &term->exp);

        term->next = NULL;

        insert_term(poly, term);
    }

    return 0;
}

add_to_poly 函数将多项式添加到现有多项式中。它通过克隆加数的项并将它们插入累加器来实现这一点。有必要克隆每一项,因为一项不能同时是两个多项式的成员。请注意,由于两个多项式的指数均按排序顺序保存,因此可以更有效地完成此操作。

int add_to_poly(poly_t *accum, const poly_t *addend)
{
    term_t *term;
    for (term = addend->head; term != NULL; term = term->next) {
        term_t *new_term;

        if ((new_term = clone_term(term)) == NULL) {
            return -1;
        }

        insert_term(accum, new_term);
    }

    return 0;
}

使用退格字符删除尾随的 + 可能在终端上可行,但当输出重定向到文件时则不起作用。最好只在需要时才打印分隔符。

void display_poly(poly_t *poly)
{
    term_t *term;
    for (term = poly->head; term != NULL; term = term->next) {
        printf("(%dx^%d)", term->coeff, term->exp);
        if (term->next != NULL) {
            printf("+");
        }
    }
}

main 应该具有以下类型签名。我们只需初始化多项式,输入两个多项式,将它们相加并打印它们。

int main(int argc, char **argv)
{
    poly_t polyA;
    poly_t polyB;
    poly_t polyC;

    init_poly(&polyA);
    init_poly(&polyB);
    init_poly(&polyC);

    printf("Polynomial 1\n\n");
    if (input_poly(&polyA) == -1) {
        goto error;
    }
    printf("\n");

    printf("Polynomial 2\n\n");
    if (input_poly(&polyB) == -1) {
        goto error;
    }
    printf("\n\n");

    if ((add_to_poly(&polyC, &polyA) == -1 ||
         add_to_poly(&polyC, &polyB) == -1)) {
        goto error;
    }

    printf("Polynomial 1=");
    display_poly(&polyA);
    printf("\n");

    printf("\nPolynomial 2=");
    display_poly(&polyB);
    printf("\n");

    printf("\nSum of the two polynomials is=");
    display_poly(&polyC);
    printf("\n");

error:
    destroy_poly(&polyA);
    destroy_poly(&polyB);
    destroy_poly(&polyC);

    return 0;
}

关于c - 我的多项式加法 c 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301640/

相关文章:

c - C中的 "++="是什么?

c - C 中的指针类似于数组,但指针更有用

Python PolynomialFeatures 将数据转换为与原始数据不同的形状

python - 使用 PolynomialFeatures 和 LinearRegression 拟合更高阶函数

检查用户是否输入了 int - C

c - 如何使数组中的每一列适合宾果卡的随机数?

c - 在 ANSI C 中打印字符

c++ - 用于 Legendre 多项式的 gsl_sf_legendre_sphPlm_array() 替代方案

python - scipy 勒让德多项式中的正交性问题

python - 使用 PolynomialFeatures 将多项式拟合到 3D 点云