Data structure - addition of polynomials of one variable (C language version)

The task of this level: Design a singly linked list storage structure. Each node stores the coefficient and exponent of one item. The types are all integers. Complete the functions that generate polynomials, add polynomials, and output polynomials.

related information

In order to complete this mission, you need to master:

  1. How to store a polynomial of one variable;

  2. How to add polynomials of one variable.

Store a polynomial of one variable

Mathematically, a polynomial of one variable has the form:

pn​(x)=p0​+p1​x1+p2​x2+...+pn​xn

It can be represented by a linear table (p0​, p1​,...pn​). Under normal circumstances, a one-variable polynomial only represents non- 0coefficient items and is stored in a chain. The corresponding linked list node data structure can be: (assuming that the coefficients and exponents of the polynomial are both integers)

struct node
{
    int exp; //表示指数
    int coef; //表示系数
    struct node *next; //指向下一个结点的指针
};

Polynomial addition operation rules

All terms with the same exponent in the two polynomials are added to the coefficients. If the sum is not 0, then they form a term in the result polynomial. All terms with different exponents in the two polynomials are copied to the result polynomial respectively.

Example:

p(x)=5+2x+3x5−2x7

Q(x)=12x+2x7+13x15

Then the resulting polynomial is:

R(x)=5+14x+3x5+13x15

____________________________________________________________________________

Input and output univariate polynomial

When inputting, input the coefficients and exponents of the unary polynomial item by item and in sequence. 0When indicates the end of the input.

For example:

p(x)=5+2x+3x5−2x7

enter:5 0 2 1 3 5 -2 7 0 0

The output takes the following format. If the expected output polynomial is:

p(x)=5+2x+3x5−2x7

Then the output is:5x^0 + 2x^1 + 3x^5 -2x^7

____________________________________________________________________________

Programming requirements

Add code in the editor on the right to implement the corresponding function. Creating a polynomial function requires the ability to input the coefficients and exponents of the one-variable polynomial one by one and in sequence when inputting each item of the polynomial in the function. The input end of the input is expressed 0when

Test instruction

The platform will test the code you write:

_____________________________________________________________________________

Test input:

5 0 2 1 3 5 -2 7 0 0 12 1 2 7 13 15 0 0

Expected output:

5x^0 + 14x^1 + 3x^5 + 13x^15

____________________________________________________________________________

Test input:

6 -1 5 0 7 9 0 0 -15 -2 9 9 18 12 0 0

Expected output:

-15x^-2 + 6x^-1 + 16x^9 + 18x^12


Start your mission and wish you success!

Header file LAB1.H

#ifndef _LAB1_H_
#define  _LAB1_H_
#include <stdlib.h>
#include <stdio.h>


//存放多项式某项的结点结构
struct node
{
    int coef ; //表示系数
    int exp ;  // 表示指数
    struct node *next;  //指向下一个结点的指针
};

typedef  struct node * PNODE ;

/*
  函数功能:生成多项式
  函数名:createPoly
  函数参数:无
  返回值:指向多项式的头指针
*/
PNODE createPoly(void)
{
    //在此处填写代码,能实现创建一个多项式并返回多项式头指针的函数
    //注意:头指针不存放多项式的项。
    /**********  Begin **********/
    PNODE head = (PNODE)malloc(sizeof(struct node));
    PNODE tail = head;
    int exp,coef;
    scanf("%d %d ",&coef,&exp);
    while(coef!=0){
        PNODE newnode = (PNODE)malloc(sizeof(struct node));
        if(newnode!=NULL){
            newnode->exp = exp;
            newnode->coef = coef;
        }
        newnode->next = NULL;
        tail->next = newnode;
        tail = newnode;
        scanf("%d %d",&coef,&exp);
    }
    return head;
    /**********  End  **********/
}

/*
  函数功能:进行多项式相加
  函数名:addPoly
  函数参数:polyAddLeft :加法左边多项式头指针, polyAddRight:加法右边多项式头指针
  返回值:指向结果多项式的头指针
*/
PNODE addPoly(PNODE polyAddLeft , PNODE polyAddRight)
{
    //在此处填写代码,能实现创两个多项式相加并返回结果多项式头指针的函数
    /**********  Begin **********/
    //为了方便操作,定义新的指针时刻表示链表的尾指针
    PNODE head1 = polyAddLeft->next;
    PNODE head2 = polyAddRight->next;

    //这是新链表的尾指针
    PNODE tail = polyAddLeft; //tail = 左链表的头指针。表示当前新链表的尾指针就
    while(head1!=NULL&&head2!=NULL){
        if(head1->exp < head2->exp){ //如果左链表结点次幂 小于 右链表,说明右链表中不存在可以和它相运算的项,它直接作为运算结果其中一向!
            //直接把左链表的结点连接到  新链表上
            tail->next = head1;

            //更新新链表的尾指针
            tail = tail->next;

            //此时相当于把左节点的结点剪下来,然后放到新链表上,所以左链表需要向右移动一格。
            head1 = head1->next;
        }
        else if(head1->exp > head2->exp){//如果左链表结点次幂 大于 右链表,说明左链表中不存在可以和它相运算的项,它直接作为运算结果其中一向!
            tail->next = head2; //直接把右链表结点连接到新链表中
            tail = tail->next;  //更新新链表的尾指针
            head2 = head2->next;//右链表移动一格(相当于删除去次幂小的那个结点)
        }
        else{ //假如次幂相等。
            head1->coef += head2->coef;  //则系数相加然后放在左链表中
            tail->next = head1;

            //更新新链表的尾指针
            tail = tail->next;

            //左右两条链表同时向右移动一个结点
            head1 = head1->next;
            head2 = head2->next;
        }
    }

    //判断哪边链表到最后不为空,如果不为空,直接连接到新链表后边即可。
    if(head1!=NULL)
    {
        tail->next = head1;
    }
    else tail->next = head2;

    //返回新链表的头指针
    return polyAddLeft;

    /**********  End **********/
}

/*while
  函数功能:输出多项式
  函数名:printPoly
  函数参数:待输出多项式的头指针poly
  返回值:无
*/
void printPoly(PNODE poly)
{
    //在此处填写代码,能实现按格式输出多项式的功能,输出格式样例见说明
    /**********  Begin **********/
    PNODE print_node = poly->next;
    printf("%dx^%d",print_node->coef,print_node->exp);
    print_node = print_node->next;

    while(print_node!=NULL){
        if(print_node->coef == 0);
        else{
            printf("+%dx^%d",print_node->coef,print_node->exp);
        }
        print_node = print_node->next;
    }
    /**********  End **********/
}

void destroyPoly(PNODE poly)
{
    //释放存储多项式的链表空间
    PNODE now = poly->next;
    while(now!=NULL) {
        free(poly);
        poly = now;
        now = now->next;
    }
}
#endif

Main function:main.h 

#include "../linklist.h"

int main(void)
{
	PNODE  polyAddLeft, polyAddRight ,polyAddResult ;
	
	polyAddLeft = createPoly();
	
 	
 	polyAddRight = createPoly();
 	
 	polyAddResult = addPoly(polyAddLeft,polyAddRight);
 	printPoly(polyAddResult);
    destroyPoly(polyAddLeft);
    destroyPoly(polyAddRight);
    destroyPoly(polyAddResult);
	
}

Guess you like

Origin blog.csdn.net/qq_29992017/article/details/127035365