我添加多项式c代码有什么问题?

时间:2015-08-30 22:09:51

标签: 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);
}

2 个答案:

答案 0 :(得分:0)

这是一个有效的版本:

#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;
}

答案 1 :(得分:0)

最后......我找到了解决问题的方法。我只需要通过引用来调用头部和最后一个指针。这解决了我的问题。之前我使用的是按值调用方法,它没有对我的原始值进行更改。我甚至对我的代码做了一些小改动。以下是我的工作代码 -

#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(int ,int ,poly **,poly **);
void input(poly **,poly **);
void display(poly *);

void insert(int c,int e,poly **head,poly **last)
{
    poly *p,*q,*new;

    new=(poly *)malloc(sizeof(poly));
    if(new==NULL)
    {
        printf("Allocation error\n");
        return;
    }
    new->coeff=c;
    new->exp=e;
    new->next=NULL;


    if(*head==NULL&&*last==NULL)
    {
        *head=*last=new;
    }
    //insertion at right place
    else
    {
        p=*head;
        q=NULL;
        while(e<p->exp)
        {
            q=p;
            p=p->next;
        }
        if(p->exp==e)
            p->coeff=p->coeff+c;
        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;
                new->next=NULL;
                *last=new;
            }
        }
    }
}

void input(poly **head,poly **last)
{
    int i,n,c,e;
    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",&c);
        printf("Enter exponent for term %d: ",i);
        scanf("%d",&e);
        insert(c,e,head,last);
        insert(c,e,&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");
    printf(" ");
}

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=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);
    printf("\n");
}

输出 -

Polynomial 1

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

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

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

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

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

Enter coefficient for term 5: 9
Enter exponent for term 5: 10

Polynomial 2

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

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

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

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

Enter coefficient for term 4: 0
Enter exponent for term 4: 12

Enter coefficient for term 5: 5
Enter exponent for term 5: 10


Polynomial 1=(9x^10)+(7x^8)+(5x^6)+(3x^4)+(1x^2) 
Polynomial 2=(0x^12)+(5x^10)+(8x^3)+(5x^2) 
Sum of the two polynomials is=(0x^12)+(14x^10)+(7x^8)+(5x^6)+(3x^4)+(8x^3)+(6x^2)