[Data structure] Application of linear table: sparse unary polynomial operator

 ***************************

The author of this article is Noah Lazy Yangyang, a classmate of MF22, HKUST, to provide you with a homework idea, please do not copy directly! ! ! Let's learn together~

*****************************

Table of contents

1. Description of the problem

1.1 Basic functions

1.2 Robustness

1.3 Normative

2. Description of the algorithm

2.1 Description of data structure

2.2 Description of program structure

3. Debugging analysis

4. Spatio-temporal analysis of the algorithm

5. Test results and analysis

6. Experimental experience and harvest

the code

Noah_exp1.h

Main.cpp


1. Description of the problem

1.1 Basic functions

  1. Use the learned knowledge of linear tables to construct a univariate polynomial data structure, and realize functions such as creation, display, summation, differentiation, and indefinite integral.
  2. Regarding the creation and display functions, the input format is not required, and the output format is as follows: X^3–X^2+2X-2.
  3. Regarding the summation function, it is required to use the creation function to create two polynomials first, then sum and output (screen printing), for example, input X3+3X2+2X+6 and X3-X2+2X-2, the output is 2X3+2X2 +4X+4.
  4. Regarding the differential function, it is required to input a polynomial first, and then calculate the N-order differential. For example, when N=1: X3+3X2+2X+6→3X2+6X+2, when N=2: X3+3X2+2X+6 →6X+6.
  5. Regarding the function of calculating the indefinite integral, it is required to input a polynomial first, and then calculate its indefinite integral, for example
  6. In addition, the entire program is required to build a good human-computer interaction mode, with a menu interface and various functional interfaces, and the interface switching logic must be correct.

1.2 Robustness

  1. When inputting X3-X2+2X-2, whether the results displayed are the same when inputting in the order of X3, -X2, +2X, -2 and in the order of -2, X3, -X2, +2X.
  2. Summation: The coefficient of 0 will be eliminated: (X3+3X2+2X+6) + (X3-3X2+2X+6)=2X3+4X+12.
  3. Differentiation N=4: X3+3X2+2X+6→0.
  4. The switching logic of the menu interface and each function interface is correct.

1.3 Normative

  1. code comment
  2. program modularization
  3. Friendly human-computer interaction

2. Description of the algorithm

2.1 Description of data structure

The single linked list structure is used for storage, and the node uses the structure to define each item of the polynomial. Each node contains three elements, which are coefficient, index and next node pointer. The logical structure of the data is shown in the figure below.

In memory, nodes are not stored in adjacent storage spaces, but the next node is accessed through the *next pointer in each node, and all nodes can be found through the head node.

Explanation of main variables:

variable

type

illustrate

polynomial_Node

structure

One-term node structures in polynomials

*polylist

body pointer

Used to access the structure and store the memory location of the structure

coef

float

One of the elements in polynomial_Node, the coefficient in the monomial

exp

int

One of the elements in polynomial_Node, the index in the monomial

*next

structure pointer

One of the elements in polynomial_Node, used to access the structure and store the memory location of the structure

pre_node

structure pointer

Appears in several important functions, and is used to temporarily store the pointer of the previous node when building a polynomial data structure

result /  result_return

structure pointer

It appears as a parameter in multiple important functions, generally as the exit of the function, and stores the output result of the function operation

2.2 Description of program structure

        The program mainly includes the noah_exp1.h header file and the main.cpp main file. The noah_exp1.h file is a collection of codes to realize the polynomial data structure. The main.cpp mainly realizes the interaction between the menu and the function interface and the function call in the header file. Its specific structure is as follows,

The specific description of the module in noah_exp1.h:

(1)typedef struct polynomial_Node{

    /***********************

    Name: polynomial_Node; *polylist

    Description: Structure definition for polynomial nodes

    Input Parameters: -

    Output Parameters: -

    Return: -

    Supplement: -

***********************/

(2)void Creatpolyn(polylist &poly, int listsize){

    /***********************

    Name: Creatpolyn()

    Description: Enter the coefficient and exponent of the listsize+1 item to create an ordered linked list representing a polynomial

    Input Parameters: -poly, the first node of the linked list to be initialized (headless node in this data structure)

                      -listsize, the length of the linked list to be initialized

    Output Parameters: -poly, reference parameters, save the head node of the initialized linked list as an exit

    Return: -

    Supplement: -The linked list has no head node

***********************/

(3)int get_polylen(polylist p){

    /***********************

    Name: get_polylen()

    Description: Returns the length of the linked list (the highest power of the polynomial + 1)

    Input Parameters: -p, the first node of the linked list of the required length

    Output Parameters: -

    Return: -len, int data type, is the length of the linked list

    Supplement: -The linked list has no head node

***********************/

(4)void Copy_polylist(polylist original,polylist &result){

    /***********************

    Name: Copy_polylist()

    Description: Clone the linked list, copy the p linked list to the result linked list, the memory addresses of the two tables are different

    Input Parameters: -original, the first node of the linked list to be copied

                      -result, the head node of the cloned linked list

    Output Parameters: -result, reference parameter, saves the first node of the cloned linked list as the exit of the function

    Return: -

    Supplement: -The linked list has no head node

***********************/

(5)polylist revers_Llist(polylist L){

    /***********************

    Name: revers_Llist()

    Description: The linked list is reversed, and the first node after the reverse is returned

    Input Parameters: -L, the first node of the linked list that needs to be reversed

    Output Parameters: -

    Return: - the first node after inversion

    Supplement: -The linked list has no head node

***********************/

(6)void Displaypolynomial(polylist poly){

    /***********************

    Name: Displaypolynomial()

    Description: Print polynomials in a common format

    Input Parameters: -poly, the first node of the linked list to be printed

    Output Parameters: -

    Return: -

    Supplement: -The linked list has no head node, so the first node also needs to be printed,

                 When outputting the first item, if the coefficient is positive, no plus sign is required, if it is a constant item, X is not output, and if the power is 1, the power is not displayed.

                 The coefficient is not equal to 0, X^n is not displayed when the exponent is equal to 0,

                 The coefficient is not equal to 0, and ^n is not displayed when the exponent is equal to 1,

                 When the coefficient is equal to 0, this item is omitted.

***********************/

(7)void addpoly(polylist a, polylist b, polylist &result){

    /***********************

    Name: addpoly()

    Description: Add two polynomials and store the result in the linked list of result

    Input Parameters: -a, the first node of one of the linked lists that needs to be added

                      -b, the first node of the second linked list to be added

                      -result, empty node for input

    Output Parameters: -result, as a function exit, the linked list with result as the first node saves the added polynomial

    Return: -

    Supplement: -

***********************/

(8)void differentialpoly(polylist a,int N, polylist &result_return){

    /***********************

    Name: differentialpoly()

    Description: Differentiate the polynomial to order N and store the result in the linked list of result

    Input Parameters: -a, the first node pointer of the polynomial that needs to be differentiated

                      -result_return, empty node for input

    Output Parameters: -result_return, as a function exit, the linked list with result_return as the first node saves the polynomial after N-order differentiation

    Return: -

    Supplement: -Refer to the recursive idea, each time you enter the function N-1, when N=0, turn to the function exit

***********************/

(9)void indefiniteintegralpoly(polylist a, polylist &result){

    /***********************

    Name: indefiniteintegralpoly()

    Description: Calculate the polynomial indefinite integral and store the result in the linked list of result

    Input Parameters: -a, the first node pointer of the polynomial that needs to be indefinite integral

                      -result, empty node for input

    Output Parameters: -result, as a function exit, the linked list with result as the first node saves the polynomial after calculating the indefinite integral

    Return: -

    Supplement: -

***********************/

3. Debugging analysis

Debugging function 1: Create a polynomial and print it to screen

When initializing, first input the highest degree of the polynomial, and then input the coefficients from low order to high order.

Test data selection:

  1. 3- 1.2, 2, 2.2 ,22
  2. 1-1,2
  3. 0-2
  4. 5-0, 1, 1, 0, 1, 1

Problems and solutions:

(1) Float precision problem, because the size of the judgment coefficient (especially == judgment) needs to be compared, but because of the precision of the float data type, use fabs(p->coef-0.000) > FLOAT_PRECISON instead.

(2) Other printing details, such as omitting when the coefficient is equal to 0, and not displaying the number of times when the index is 1, use different judgment statements to handle different situations.

Debugging function 2: Add 2 polynomial and print result to screen

Test data selection:

  1. 3- 1.2, 2, 2.2 ,22;1-1,2
  2. 0-2;5-0, 1, 1, 0, 1, 1

Problems and solutions:

none

Debugging function three: Get differential of a polynomial and print result to screen

Test data selection:

  1. 3- 1.2, 2, 2.2 ,22;1
  2. 3- 1.2, 2, 2.2 ,22;2
  3. 1-1,2;1
  4. 1-1,2;2
  5. 0-2;1
  6. 0-2;2
  7. 5-0, 1, 1, 0, 1, 1;1
  8. 5-0, 1, 1, 0, 1, 1;2

Problems and solutions:

none

Debugging function four: Get indefinite integral of a polynomial and print result to screen

Test data selection:

  1. 3- 1.2, 2, 2.2 ,22
  2. 1-1,2
  3. 0-2
  4. 5-0, 1, 1, 0, 1, 1

Problems and solutions:

none

4. Spatio-temporal analysis of the algorithm

(1)void Creatpolyn(polylist &poly, int listsize)

Time complexity: O(n)

Space complexity: O(n)

(2)int get_polylen(polylist p)

Time complexity: O(n)

Space complexity: O(n)

(3)void Copy_polylist(polylist original,polylist &result)

Time complexity: O(n)

Space complexity: O(n)

(4)polylist revers_Llist(polylist L)

Time complexity: O(n)

Space complexity: O(n)

(5)void Displaypolynomial(polylist poly)

Time complexity: O(n)

Space complexity: O(n)

(6)void addpoly(polylist a, polylist b, polylist &result)

Time complexity: O(n)

Space complexity: O(n)

(6)void differentialpoly(polylist a,int N, polylist &result_return)

Time complexity: O(n)

Space complexity: O(n)

(6)void indefiniteintegralpoly(polylist a, polylist &result)

Time complexity: O(n)

Space complexity: O(n)

5. Test results and analysis

Test function one: Create a polynomial and print it to screen

test case

result

analyze

3- 1.2, 2, 2.2 ,22

√, the primary item does not output the power only

1-0,2

√, the constant item is 0 to omit the output

0-2

√, only one normal output

5-0, 1, 1, 0, 1, 1

√, the sparse polynomial output is correct

Debugging function 2: Add 2 polynomial and print result to screen

test case

result

analyze

3- 1.2, 2, 2.2 ,22;1-0,2

√, the corresponding power addition is correct

0-2;5-0, 1, 1, 0, 1, 1

Problems and solutions:

none

Test function three: Get differential of a polynomial and print result to screen

test case

result

analyze

3- 1.2, 2, 2.2 ,22;1

√, first order differential normal output

3- 1.2, 2, 2.2 ,22;2

√, second order differential normal output

1-1,2;2

√, the differential order is greater than the highest power output is 0

0-2;3

√, the differential order is greater than the highest power output is 0

Debugging function four: Get indefinite integral of a polynomial and print result to scree

test case

result

analyze

3- 1.2, 2, 2.2 ,22

1-1,2

5-0, 1, 1, 0, 1, 1

6.实验体会和收获

  1. 学会了灵活运用链表这种数据结构;
  2. 对指针、引用参数、函数返回值的认识更加深刻;
  3. 对递归的思想理解更加深刻,并且能够用于实战;
  4. 学会了如何更快地定位程序BUG并进行调试;
  5. 学会了如何更科学地设计测试用例来对程序进行功能测试。

代码

Noah_exp1.h

/**************************************************************************

Copyright Copyright 2022 Noah Zhan.* File Name: noah_exp1.h* Description: 实现一元稀疏多项式数据机构及相关功能** Version: V1.0* Author:   Noah Zhan* Create Time: 2022-10-20

**************************************************************************/
#ifndef NOAH_EXP1_H_INCLUDED
#define NOAH_EXP1_H_INCLUDED
#define FLOAT_PRECISON 0.0000001//用于浮点数的大小比较
#include<cmath>

//定义多项式结点结构体
typedef struct polynomial_Node{
    /***********************
    Name: polynomial_Node; *polylist
    Description: 多项式结点的结构体定义
    Input Parameters: -
    Output Parameters: -
    Return: -
    Supplement: -
    ***********************/
    float coef;
    int expn;
    struct polynomial_Node *next;
}polynomial_Node,*polylist;

//初始化多项式
void Creatpolyn(polylist &poly, int listsize){
    /***********************
    Name: Creatpolyn()
    Description: 输入listsize+1项的系数和指数,建立表示多项式的有序链表
    Input Parameters: -poly, 所需初始化的链表的第一个结点(该数据结构中无头结点)
                      -listsize,所需初始化的链表的长度
    Output Parameters: -poly,引用参数,作为出口保存了初始化后的链表的头结点
    Return: -
    Supplement: -该链表没有头结点
    ***********************/
    poly = (polylist)malloc(sizeof(polynomial_Node));
    poly->next = nullptr;
    printf("Input constant coefficient:(float)");
    scanf("%f",&poly->coef);
    poly->expn = 0;
    //初始化剩下的listsize-1个结点
    polylist pre_node = poly;
    for(int i = 1; i<listsize;i++){
        polylist p = (polylist)malloc(sizeof(polynomial_Node));
        printf("Input X^%d coefficient:(float)",i);
        scanf("%f",&p->coef);
        p->next = nullptr;
        p->expn = i;
        pre_node->next = p;
        pre_node = p;
    }
}

//返回链表的长度(多项式的最高次幂+1)
int get_polylen(polylist p){
    /***********************
    Name: get_polylen()
    Description: 返回链表的长度(多项式的最高次幂+1)
    Input Parameters: -p,所需求长度的链表的第一个结点
    Output Parameters: -
    Return: -len,int数据类型,为链表的长度
    Supplement: -该链表没有头结点
    ***********************/
    int len = 1;
    while(p->next!=nullptr){
        len++;
        p = p->next;
    }
    return len;
}


//链表的克隆,将p链表复制给result链表,两表的内存地址不同
void Copy_polylist(polylist original,polylist &result){
    /***********************
    Name: Copy_polylist()
    Description: 链表的克隆,将p链表复制给result链表,两表的内存地址不同
    Input Parameters: -original,被复制的链表的第一个结点
                      -result,克隆后的链表的头结点
    Output Parameters: -result,引用参数,作为函数的出口保存了克隆后的链表的第一个结点
    Return: -
    Supplement: -该链表没有头结点
    ***********************/
    //克隆头结点
    result = (polylist)malloc(sizeof(polynomial_Node));
    result->coef = original->coef;
    result->expn = original->expn;
    result->next = nullptr;
    polylist pre_node = result;
    int len = get_polylen(original);
    //克隆剩下的结点
    for(int i = 1; i<len;i++){
        original = original->next;
        //printf("%f %d %f %d",original->coef,original->expn,original->next->coef,original->next->expn);
        polylist p = (polylist)malloc(sizeof(polynomial_Node));
        p->coef = original->coef;
        p->expn = original->expn;
        p->next = nullptr;
        pre_node->next = p;
        pre_node = p;
    }
}

//链表逆置,并返回逆置后的第一个结点
polylist revers_Llist(polylist L){
    /***********************
    Name: revers_Llist()
    Description: 链表逆置,并返回逆置后的第一个结点
    Input Parameters: -L,需要逆置的链表的第一个结点
    Output Parameters: -
    Return: -逆置后的第一个结点
    Supplement: -该链表没有头结点
    ***********************/
    if(L->next==nullptr)//如果长度为1,则不必逆置
        return L;
    polylist p = L->next;
    L->next = nullptr;
    while(p){
        polylist temp = p->next;
        p->next = L;
        L = p;
        p = temp;
    }
    return L;
}

//打印多项式
void Displaypolynomial(polylist poly){
    /***********************
    Name: Displaypolynomial()
    Description: 以常见的格式打印多项式
    Input Parameters: -poly,需要打印的链表的第一个结点
    Output Parameters: -
    Return: -
    Supplement: -该链表没有头结点,因此第一个结点也需要打印,
                 输出第一项时如果系数为正则不需要输出加号,如果是常数项则不输出X,如果幂为1则不显示次幂,
                 系数不等于0,指数等于0时不显示X^n,
                 系数不等不等于0,指数等于1时不显示^n,
                 系数等于0时,省略该项。
    ***********************/
    polylist p = poly;
    //输出第一项(如果系数为正则不需要输出加号,如果是常数项则不输出X,如果幂为1则不显示次幂)
    if(fabs(p->coef-0.000) > FLOAT_PRECISON && p->expn==0)//系数不等于零,指数等于零时
        printf("%.2f",p->coef);
    else if(fabs(p->coef-0.000) > FLOAT_PRECISON && p->expn==1)//系数不等于零,指数等于1时
        printf("%.2fX",p->coef);
    else{
        while(fabs(p->coef-0.000) < FLOAT_PRECISON){//系数等于零时,p = p->next
            if(p->next==nullptr){//如果多项式只有一项且系数为0
                printf("0"); return;}
            p = p->next;
        }
        if(fabs(p->coef-1.000) < FLOAT_PRECISON && p->expn!=0)
            printf("X^%d",p->expn);
        else if(fabs(p->coef-(-1.000)) < FLOAT_PRECISON && p->expn!=0)
            printf("-X^%d",p->expn);
        else
        printf("%.2fX^%d",p->coef,p->expn);
    }
    p = p->next;
    //输出剩下的项
    while(p!=nullptr){
        if(fabs(p->coef-0.000) < FLOAT_PRECISON){//如果系数为0则该项不打印
            p = p->next;
            continue;
        }
        else if(p->coef<0.000 && p->expn!=0 && p->expn !=1){//系数小于零直接打印,自带减号
            if(fabs(p->coef-(-1.000)) < FLOAT_PRECISON)//系数为-1
                printf("-X^%d",p->expn);
            else//系数不为-1
                printf("%.2fX^%d",p->coef,p->expn);
        }
        else if(p->coef>0.000 && p->expn!=0 && p->expn !=1){ //系数大于零打印时要加“+”
            if(fabs(p->coef-1.000) < FLOAT_PRECISON)//系数为1
                printf("+X^%d",p->expn);
            else//系数不为1
                printf("+%.2fX^%d",p->coef,p->expn);
        }
        else if(p->expn==0){//指数为零的时候不打印X和次幂
            if(p->coef>0)
                printf("+%.2f",p->coef);
            else if(p->coef<0)
                printf("%.2f",p->coef);
        }
        else if(p->expn==1){//指数为1的时候不打印次幂
            if(p->coef>0){
                if(fabs(p->coef-1.000) < FLOAT_PRECISON)//系数为1
                    printf("+X");
                else
                    printf("+%.2fX",p->coef);}
            else if(p->coef<0){
                if(fabs(p->coef-(-1.000)) < FLOAT_PRECISON)//系数为-1
                    printf("-X");
                else
                    printf("%.2fX",p->coef);}
        }
        p = p->next;
    }
}

//两个多项式相加
void addpoly(polylist a, polylist b, polylist &result){
    /***********************
    Name: addpoly()
    Description: 将两个多项式相加并将结果存储在result的链表中
    Input Parameters: -a,需要相加的其中一个链表的第一个结点
                      -b,需要相加的第二个链表的第一个结点
                      -result,输入的空结点
    Output Parameters: -result,作为函数出口,以result为第一个结点的链表保存了相加后的的多项式
    Return: -
    Supplement: -
    ***********************/
    result = (polylist)malloc(sizeof(polynomial_Node));
    polylist pre_node;
    int head_state = 1;//用于在生成第一个结点时的控制

    if(a->expn!=0){//当a头结点次幂不为0时进行逆置操作,方便后续相加运算
        a = revers_Llist(a);
    }
    if(b->expn!=0){//当b头结点次幂不为0时进行逆置操作,方便后续相加运算
        b = revers_Llist(b);
    }

    while(a!=nullptr || b!=nullptr){
        float coef_a,coef_b;//暂存
        int expn_a,expn_b;//暂存
        //计算新的coef和expt
        if(a==nullptr && b != nullptr){
            coef_a = 0.000;
            expn_a = 0;
        }
        else{
            coef_a = a->coef;
            expn_a = a->expn;
        }
        if(b==nullptr && a != nullptr){
            coef_b = 0.000;
            expn_b = 0;
        }
        else{
            coef_b = b->coef;
            expn_b = b->expn;
        }
        float coef = coef_a + coef_b;
        int expn = expn_a|expn_b;

        //如果是头结点
        if(head_state){
            result->coef = coef;
            result->expn = expn;
            result->next = nullptr;
            pre_node = result;
            //printf("%f  %d",result->coef,result->expn);
            if(a!=nullptr)
                a = a->next;
            if(b!=nullptr)
                b = b->next;
            head_state = 0;
            continue;
        }
        //如果不是头结点
        polylist temp = (polylist)malloc(sizeof(polynomial_Node));
        temp->coef = coef;
        temp->expn = expn;
        temp->next = nullptr;
        pre_node->next = temp;
        pre_node = temp;
        if(a!=nullptr)
            a = a->next;
        if(b!=nullptr)
            b = b->next;
    }
    result = revers_Llist(result);
}

//多项式求N阶微分
void differentialpoly(polylist a,int N, polylist &result_return){
    /***********************
    Name: differentialpoly()
    Description: 将多项式微分N阶并将结果存储在result的链表中
    Input Parameters: -a,需要进行微分的多项式的第一个结点指针
                      -result_return,输入的空结点
    Output Parameters: -result_return,作为函数出口,以result_return为第一个结点的链表保存了N阶微分后的的多项式
    Return: -
    Supplement: -参考了递归思路,每次进入函数N-1,当N=0时转向函数出口
    ***********************/
    if(get_polylen(a)<=N){//微分阶数过高时进行控制
        result_return = (polylist)malloc(sizeof(polynomial_Node));
        result_return->coef = 0.000;
        result_return->expn = 0;
        result_return->next = nullptr;
        return ;
    }
    else if(N>0){//此时还要继续微分
        polylist result;
        result = (polylist)malloc(sizeof(polynomial_Node));
        polylist pre_node;
        int head_state = 1;//用于在生成第一个结点时的控制
        while(a!=nullptr){
            //如果expn为0则这项微分后不存在了,直接跳过这个node
            if(a->expn==0){
                a = a->next;
                continue;
            }
            //计算微分后新的coef和expt
            float coef = a->coef * a->expn;
            int expn = a->expn - 1;

            //如果是头结点
            if(head_state){
                result->coef = coef;
                result->expn = expn;
                result->next = nullptr;
                pre_node = result;
                a = a->next;
                head_state = 0;
                continue;
            }
            //如果不是头结点
            polylist temp = (polylist)malloc(sizeof(polynomial_Node));
            temp->coef = coef;
            temp->expn = expn;
            temp->next = nullptr;
            pre_node->next = temp;
            pre_node = temp;
            a = a->next;
        }
        differentialpoly(result,N-1,result_return);//进入下一阶微分
        free(result);//释放内存
    }
    else if(N==0)//出口,将最终结果复制给result_return
        Copy_polylist(a,result_return);
}

//多项式不定积分
void indefiniteintegralpoly(polylist a, polylist &result){
    /***********************
    Name: indefiniteintegralpoly()
    Description: 求多项式不定积分并将结果存储在result的链表中
    Input Parameters: -a,需要求不定积分的多项式的第一个结点指针
                      -result,输入的空结点
    Output Parameters: -result,作为函数出口,以result为第一个结点的链表保存了求不定积分后的的多项式
    Return: -
    Supplement: -
    ***********************/
    result = (polylist)malloc(sizeof(polynomial_Node));
    result->coef=0.000;result ->expn=0;result->next=nullptr;
    polylist pre_node = result;
    while(a!=nullptr){
        polylist temp = (polylist)malloc(sizeof(polynomial_Node));
        temp->coef = float(a->coef / (float(a->expn) + 1.00));
        temp->expn = a->expn + 1;
        temp->next = nullptr;
        pre_node->next = temp;
        pre_node = temp;
        a = a->next;
    }
}
#endif // NOAH_EXP1_H_INCLUDED

Main.cpp

#include <iostream>
#include "noah_exp1.h"
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void Menue_gui();
void createandprintpoly();
void addandprint();
void differentialandprint();
void integralandprint();

int main()
{
    while(1){
        Menue_gui();
        int func;
        scanf("%d",&func);
        switch(func){
            case 0:
                exit(0);
            case 1:
                createandprintpoly();break;
            case 2:
                addandprint();break;
            case 3:
                differentialandprint();break;
            case 4:
                integralandprint();break;
            default:
                printf("Input error! Please try again!");
        }
        printf("\n");
        system("pause");
    }
    return 0;
}

//菜单界面
void Menue_gui(){
    system("cls");//清屏
    printf("********************This is Sparse unary polynomial arithmetic unit**************************\n");
    printf("*********************************************************************************************\n");
    printf("Menue:\n");
    printf("\nExit this program------------------------------------------------------0.\n");
    printf("\nCreate a polynomial and print it to screen-----------------------------1.\n");
    printf("\nAdd 2 polynomial and print result to screen----------------------------2.\n");
    printf("\nGet differential of a polynomial and print result to screen------------3.\n");
    printf("\nGet indefinite integral of a polynomial and print result to screen-----4.\n");
    printf("\n**********************************************************************************************\n");
    printf("Choose the function you want to use(input number):\n");
}

//功能1界面
void createandprintpoly(){
    system("cls");//清屏
    printf("ENTER FUNCTION : Create a polynomial and print it to screen--1.\n");
    polylist poly_1;
    printf("Please input highest exponential(integer) and a number series of coefficient to initialize the polynomial.\n");
    printf("highest exponential:\n");
    int Initsize;
    scanf("%d",&Initsize);
    Initsize=Initsize+1;
    //printf("The number series of coefficient:(input)\n");
    Creatpolyn(poly_1,Initsize);
    printf("Now the polynomial is:\n");
    //Displaypolynomial(poly_1);//这是从低次幂到高次幂进行打印
    //将正向链表逆置,得到从高次降到低次的链表
    polylist poly_1_reverse;
    Copy_polylist(poly_1,poly_1_reverse);//复制一个原链表到别的内存空间
    poly_1_reverse = revers_Llist(poly_1_reverse);//逆置
    Displaypolynomial(poly_1_reverse);
}

//功能2界面
void addandprint(){
    system("cls");//清屏
    printf("ENTER FUNCTION : Add 2 polynomial and print result to screen----------------------------2.\n");
    //初始化多项式A
    printf("Initialize polynomial A.\n");
    polylist poly_2;
    printf("Please input highest exponential(integer) and a number series of coefficient to initialize the polynomial.\n");
    printf("highest exponential:\n");
    int Initsize;
    scanf("%d",&Initsize);
    Initsize=Initsize+1;
    //printf("The number series of coefficient:(input)\n");
    Creatpolyn(poly_2,Initsize);

    //初始化多项式B
    printf("Initialize polynomial B.\n");
    polylist poly_3;
    printf("Please input highest exponential(integer) and a number series of coefficient to initialize the polynomial.\n");
    printf("highest exponential:\n");
    //int Initsize;
    scanf("%d",&Initsize);
    Initsize=Initsize+1;
    //printf("The number series of coefficient:(input)\n");
    Creatpolyn(poly_3,Initsize);

    //展示多项式A
    printf("\nNow the polynomial A is:\n");
    polylist poly_2_reverse;
    Copy_polylist(poly_2,poly_2_reverse);//复制一个原链表到别的内存空间
    poly_2_reverse = revers_Llist(poly_2_reverse);//逆置
    Displaypolynomial(poly_2_reverse);

    //展示多项式B
    printf("\nNow the polynomial B is:\n");
    polylist poly_3_reverse;
    Copy_polylist(poly_3,poly_3_reverse);//复制一个原链表到别的内存空间
    poly_3_reverse = revers_Llist(poly_3_reverse);//逆置
    Displaypolynomial(poly_3_reverse);

    //多项式A\B相加并打印
    printf("\nAdd A and B, the result is:\n");
    polylist add_result;
    addpoly(poly_2_reverse,poly_3_reverse,add_result);
    Displaypolynomial(add_result);
}

//功能3界面
void differentialandprint(){
    system("cls");//清屏
    printf("ENTER FUNCTION: Get differential of a polynomial and print result to screen------------3.\n");
    //初始化多项式C
    printf("Initialize polynomial.\n");
    polylist poly_5;
    printf("Please input highest exponential(integer) and a number series of coefficient to initialize the polynomial.\n");
    printf("highest exponential:\n");
    int Initsize;
    scanf("%d",&Initsize);
    Initsize=Initsize+1;
    //printf("The number series of coefficient:(input)\n");
    Creatpolyn(poly_5,Initsize);

    //展示多项式C
    printf("\nNow the polynomial is:\n");
    polylist poly_5_reverse;
    Copy_polylist(poly_5,poly_5_reverse);//复制一个原链表到别的内存空间
    poly_5_reverse = revers_Llist(poly_5_reverse);//逆置
    Displaypolynomial(poly_5_reverse);

    printf("\nInput the  differential order:\n");
    int N;
    scanf("%d",&N);
    //一阶微分阶数
    printf("\n %d order differential of polynomial is:\n",N);
    polylist differential_result_1st;
    differentialpoly(poly_5_reverse,N,differential_result_1st);
    Displaypolynomial(differential_result_1st);
}

//功能4界面
void integralandprint(){
    system("cls");//清屏
    //初始化多项式D
    printf("Initialize polynomial.\n");
    polylist poly_6;
    printf("Please input highest exponential(integer) and a number series of coefficient to initialize the polynomial.\n");
    printf("highest exponential:\n");
    int Initsize;
    scanf("%d",&Initsize);
    Initsize=Initsize+1;
    //printf("The number series of coefficient:(input)\n");
    Creatpolyn(poly_6,Initsize);

    //展示多项式D
    printf("\nNow the polynomial is:\n");
    polylist poly_6_reverse;
    Copy_polylist(poly_6,poly_6_reverse);//复制一个原链表到别的内存空间
    poly_6_reverse = revers_Llist(poly_6_reverse);//逆置
    Displaypolynomial(poly_6_reverse);

    //不定积分
    printf("\n Indefinite integral of polynomial is:\n");
    polylist integeral_result;
    indefiniteintegralpoly(poly_6_reverse,integeral_result);
    Displaypolynomial(integeral_result);
}

Guess you like

Origin blog.csdn.net/standingflower/article/details/127596094