• 0x00数据结构——C语言实现(多项式)


    0x00数据结构——C语言实现(多项式)

    /*filename:polynomial*/
    #ifndef POLYNOMIAL
    #define POLYNOMIAL
    
    //一元多项式的表示
    
    //Pn(x)=p0 + p1*x + p2*x^2 + ... + pn*x^n
    /*
    基本操作:
        创建一个有m项系数和指数的一元多项式p
        销毁一元多项式p
        打印输出
        返回项数
        完成一元多项式的相加
        完成一元多项式的相减
        完成一元多项式的相乘
    
    */
    
    
    struct term;
    typedef struct term term;
    typedef term *polyn;
    
    typedef enum {
        false = 0,
        true
    } BOOL;
    
        //初始化一个一元多项式p
    polyn init_polynomial(void);
    
        //在多项式p中插入一个系数为c,指数为e的项
    polyn insert_term(int c, int e, polyn p);
    
    /*
        //创建一个有m项系数和指数的一元多项式p
    polyn creat_polynomial(int m);
    */
    
        //销毁一元多项式p
    BOOL destroy_polyn(polyn p);
    
        //打印输出
    void print_polyn(polyn p);
    
        //返回项数
    int polyn_length(polyn p);
    
        //完成一元多项式的相加
    polyn add_polyn(polyn p1, polyn p2);
    
        //完成一元多项式的相减
    polyn sub_polyn(polyn p1, polyn p2);
    
        //完成一元多项式的相乘
    polyn mutiply_polyn(polyn p1, polyn p2);
    
    
    #endif
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "polynomial.h"
    
    /*
    typedef struct term{
        float coef; //系数
        int expn;   //指数
        struct term *next;
    } term, *polyn;
    */
    
    struct term {
        float coef; //系数
        int expn;   //指数
        struct term *next;
    };
    
    //初始化一个一元多项式p
    polyn init_polynomial(void)
    {
        term *head = NULL;
        head = (term*)malloc(sizeof(term));
        if(head!=NULL){
            head->coef = 0;
            head->expn = -1;
            head->next = NULL;
        }
        return head;
    }
    
        //在多项式p中加入一个系数为c,指数为e的项
    polyn insert_term(int c, int e, polyn p)
    {
        if(c==0) {
            return p;
        } else {
            term *tmp = p->next, *pre = p;
            while(tmp != NULL && tmp->expn != e && tmp->expn < e) {
                pre = tmp;
                tmp = tmp->next;
            }
            if(tmp == NULL) {
                pre->next = (term*)malloc(sizeof(term));
                if(pre->next != NULL) {
                    pre->next->coef = c;
                    pre->next->expn = e;
                    pre->next->next = NULL;
                }
            } else if(tmp->expn == e) {
                tmp->coef += c;
                if(tmp->coef == 0) {
                    pre->next = tmp->next;
                    free(tmp);
                }
            } else if(tmp->expn > e) {
                pre->next = (term*)malloc(sizeof(term));
                if(pre->next != NULL) {
                    pre->next->coef = c;
                    pre->next->expn = e;
                    pre->next->next = tmp;
                }
            }
        }
        return p;
    }
    
    /*
        //创建一个有m项系数和指数的一元多项式p,带头节点
    polyn creat_polynomial(int m)
    {
        term *head = NULL, *tmp = NULL, *tmp_p = NULL;
        tmp = (term*)malloc(sizeof(term));
        tmp->coef = 0;
        tmp->expn = -1;
        tmp->next = NULL;
        head = tmp;
        while(m){
            tmp_p = (term *)malloc(sizeof(term));
            tmp_p->next = NULL;
            tmp->next = tmp_p;
            tmp = tmp_p;
            m--;
        }
        return head;
    }
    */
    
        //销毁一元多项式p
    BOOL destroy_polyn(polyn p)
    {
        term *tmp;
        p = p->next;
        while(p != NULL){
            tmp = p;
            p = p->next;
            free(tmp);
            tmp = NULL;
        }
        return true;
    }
    
        //打印输出
    void print_polyn(polyn p)
    {
        p = p->next;
        if(p!=NULL) {
            if(p->expn == 0){//考虑第一项
                printf("%g", p->coef);
                p = p->next;
                if(p!=NULL){
                    if(p->expn == 1){//考虑第二项
                        p->coef>0? printf("+%gX", p->coef):printf("%gX", p->coef);
                    } else {
                         p->coef>0?printf("+%gX^%d", p->coef, p->expn):printf("%gX^%d", p->coef, p->expn);
                    }
                    p = p->next;
                }
            } else if(p->expn == 1){//考虑第一项
                if(p->coef != 1) {
                    printf("%gX", p->coef);
                } else {
                    printf("X");
                }
                p = p->next;
            } else if(p->expn > 1){
    
                if(p->coef != 1) {
                    printf("%gX^%d", p->coef, p->expn);
                } else {
                    printf("X^%d", p->expn);
                }
                p = p->next;
            }
            while(p!=NULL){
                p->coef>0?printf("+%gX^%d", p->coef, p->expn):printf("%gX^%d", p->coef, p->expn);
                p = p->next;
            }
        }
        printf("
    ");
    }
    
        //返回项数
    int polyn_length(polyn p)
    {
        int n = 0;
        while(p->next != NULL){
            n++;
            p = p->next;
        }
        return n;
    }
    
        //完成一元多项式的相加
    polyn add_polyn(polyn p1, polyn p2)
    {
        term *tmp = p1, *tmp1 = p1->next, *tmp2 = p2->next;
        while(tmp1!=NULL && tmp2!=NULL){
            //指数相等的情况
            if(tmp1->expn == tmp2->expn){
                tmp1->coef += tmp2->coef;
                if(tmp1->coef == 0){
                    p1->next = tmp1->next;
                    p2->next = tmp2->next;
                    free(tmp1);
                    free(tmp2);
                    //相加为0则物理删除该项;
                } else {
                    p1 = p1->next;
                    p2 = p2->next;
                }
                tmp1 = p1->next;
                tmp2 = p2->next;
            } else if(tmp1->expn < tmp2->expn){
                //指数1小于指数2,p1向后比较
                p1 = p1->next;
                tmp1 = p1->next;
            } else if(tmp2->expn < tmp1->expn){
                //指数1大于指数2,将指数2项插到指数1前面
                //逻辑删除p2中指数2对应项
                p2->next = tmp2->next;
    
                //插出入到p1中指数1对应项前面
                tmp2->next = tmp1;
                p1->next = tmp2;
    
                //指针移动
                p1 = p1->next;
                tmp2 = p2->next;
            }
        }
        if(tmp1 == NULL){
            p1->next = tmp2;
        }
    
        free(p2);
        p2 = NULL;
        return tmp;
    }
    
        //完成一元多项式的相减
    polyn sub_polyn(polyn p1, polyn p2)
    {
        term *tmp = p1, *tmp1 = p1->next, *tmp2 = p2->next;
        while(tmp1!=NULL && tmp2!=NULL){
            //指数相等的情况
            if(tmp1->expn == tmp2->expn){
                tmp1->coef -= tmp2->coef;
                if(tmp1->coef == 0){
                    p1->next = tmp1->next;
                    p2->next = tmp2->next;
                    free(tmp1);
                    free(tmp2);
                    //相减为0则物理删除该项;
                } else {
                    p1 = p1->next;
                    p2 = p2->next;
                }
                tmp1 = p1->next;
                tmp2 = p2->next;
            } else if(tmp1->expn < tmp2->expn){
                //指数1小于指数2,p1向后比较
                p1 = p1->next;
                tmp1 = p1->next;
            } else if(tmp2->expn < tmp1->expn){
                //指数1大于指数2,将指数2项插到指数1前面
                //逻辑删除p2中指数2对应项
                tmp2->coef *= -1;
                p2->next = tmp2->next;
    
                //插出入到p1中指数1对应项前面
                tmp2->next = tmp1;
                p1->next = tmp2;
    
                //指针移动
                p1 = p1->next;
                tmp2 = p2->next;
            }
        }
        if(tmp1 == NULL){
            p1->next = tmp2;
        }
    
        free(p2);
        p2 = NULL;
        return tmp;
    }
    
        //完成一元多项式的相乘
    polyn mutiply_polyn(polyn p1, polyn p2)
    {
        polyn tmp_res = (polyn)malloc(sizeof(term));
        tmp_res->next = NULL;
        term *cur1 = p1->next, *cur2 = p2->next;
        while(cur1!=NULL){
            polyn tmp_p2 = (polyn)malloc(sizeof(term));
            term *cur_p2 = tmp_p2;
            cur2 = p2->next;
            while(cur2!=NULL){
                cur_p2->next = (term *)malloc(sizeof(term));
                cur_p2 = cur_p2->next;
                cur_p2->coef = cur1->coef * cur2->coef;
                cur_p2->expn = cur1->expn + cur2->expn;
    
                cur2 = cur2->next;
            }
            cur_p2->next = NULL;
            //print_polyn(tmp_p2);
            if(tmp_res->next == NULL){
                free(tmp_res);
                tmp_res = tmp_p2;
            } else{
                add_polyn(tmp_res, tmp_p2);
            }
            //print_polyn(tmp_res);
            cur1 = cur1->next;
            //free(p1->next);
            //p1->next = cur1;
        }
        destroy_polyn(p1);
        destroy_polyn(p2);
        p1->next = tmp_res->next;
        return tmp_res;
    }
  • 相关阅读:
    Html
    git和github简易教程
    Java基础
    如何学习一门语言
    leetcode题解(持续更新)
    浅谈安全威胁+引子
    内网渗透基础
    Java运算符
    Java修饰符
    Java变量类型
  • 原文地址:https://www.cnblogs.com/born2run/p/9581343.html
Copyright © 2020-2023  润新知