数据结构实验——多项式

 

. Experimental purpose:

  • Understand the basic logical structure of linear list, and complete the implementation of linked list and circular linked list
  • Through experiments, we can further understand the logical structure and storage structure of linear list, improve the ability to use theoretical knowledge to guide and solve practical problems, and master the practical application of linked list.

二. Experimental content:

Title: univariate polynomial operation

Problem description:

The algorithm is designed to realize the simple operation of univariate polynomial.

Basic requirements:

(1) Input and establish polynomials;

(2) Output polynomial;

(3) Polynomial addition

(4) Polynomial subtraction.

Test data:    

(1)(2x+5x8-3.1x11)+(7-5x8+11x9)=(-3.1x11+11x9+2x+7)

(2)(6x-3-x+4.4x2-1.2x9)-(―6x―3-+5.4x2-x2+7.8x15) =(―7.8x15―

1.2x9+12x―3―x)

    (3)(1+x+x2+x3+x4+x5)+(―x3―x4)=(1+x+x2+x5)

    (4)(x+x3)+(―x―x3)=0

    (5)(x+x100)+(x100+x200)=(x+2x100+x200)

    (6)(x+x2+x3)+0=x+x2+x3

 

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

typedef struct LNode {
    int pow;
    double cof;
    struct LNode* next;
}LNode, * LinkList;


LinkList CreatLinkList() {
    LinkList head = (LNode*)malloc(sizeof(LNode));
    LNode* L, * rear;
    rear = head;
    head->next = NULL;
    int i = 1;
    while (1) {
        L = (LNode*)malloc(sizeof(LNode));
        printf("Typing element%d, please enter the power:", i);
        scanf("%d", &(L->pow));
        printf("Please enter a coefficient:");
        scanf("%lf", &(L->cof));
        if (L->cof == 0 && L->pow == 0) break;
        rear->next = L;
        rear = rear->next;
        i++;
    }
    rear->next = NULL;
    return head;
}

LinkList Link(LinkList L1, LinkList L2) {
    LinkList tmp = L1;
    while (tmp->next != NULL) {
        tmp = tmp->next;
    }
    tmp->next = L2->next;
    return L1;
}

void pr(LinkList result) {
    if (result->next == NULL)return;
    result = result->next;
    printf("%.2lf", result->cof);
    if (result->pow!=0) {
        printf("x^%d", result->pow);
    }
    result = result->next;
    while (1) {
        if (result->pow != 0) {
            if (result->cof > 0) printf("+");
            printf("%.2lf", result->cof);
            if (result->pow!=0) {
                printf("x^%d", result->pow);
            }
        }
        else {
            if (result->cof > 0) printf("+");
            printf("%.2lf", result->cof);
        }
        result = result->next;
        if (result == NULL) {
            break;
        }
    }
    printf("\n");
}

LinkList Sort(LinkList L) {
    LNode* p, * rear;
    rear = L;
    int a;
    double b;
    p = L->next;
    while (rear->next != NULL) {
        p = L->next;
        while (p->next != NULL) {
            if ((p->next->pow) < (p->pow)) {
                a = p->pow;
                p->pow = p->next->pow;
                p->next->pow = a;
                b = p->cof;
                p->cof = p->next->cof;
                p->next->cof = b;
            }
            p = p->next;
        }
        rear = rear->next;
    }
    return L;
}

LinkList Cmp(LinkList L) {
    LinkList rear;
    rear = L;
    while (rear->next != NULL) {
        if (rear->next->next == NULL) {
            break;
        }
        if (rear->next->pow == rear->next->next->pow && rear->next->cof + rear->next->next->cof == 0) {
            rear->next = rear->next->next->next;
        }
        else if (rear->next->pow == rear->next->next->pow) {
            rear->next->cof = rear->next->cof + rear->next->next->cof;
            rear->next->next = rear->next->next->next;
        }
        rear = rear->next;
    }
    return L;
}

LinkList Reverse(LinkList L) {
    LinkList rear;
    rear = L;
    while (rear!=NULL) {
        rear->cof = -(rear->cof);
        //printf("%d",rear->cof);
        rear = rear->next;
    }

    return L;
}

int main() {
    LinkList L1, L2;
    LinkList result;
    int flag;

    L1 = CreatLinkList();
    L1 = Sort(L1);
    L1 = Cmp(L1);
    L2 = CreatLinkList();
    L2 = Sort(L2);
    L2 = Cmp(L2);
    printf("如果想计算加法,请输入1,想计算减法,请输入2");
    scanf("%d",&flag);



    printf("原第一个多项式是:");
    pr(L1);
    printf("原第二个多项式是:");
    pr(L2);

    if (flag==2) {
        L2 = Reverse(L2);
    }

    printf("原第二个多项式变化后是:");
    pr(L2);

    result = Link(L1, L2);
    result = Sort(result);
    result = Cmp(result);
    printf("结果是:");
    pr(result);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值