3-2-3 链表 一元多项式的乘法与加法运算 (20 分)

#include<stdio.h>
#include<stdlib.h>

typedef struct polynode* polynomial;

struct polynode
{
    int coef;
    int expon;
    polynomial next;
};

polynomial readpoly();
void attach(int, int, polynomial*);
polynomial multpoly(polynomial ,polynomial );
polynomial addpoly(polynomial, polynomial);
void print(polynomial);

int main(void)
{
    polynomial p1, p2, ps, pp;

    p1 = readpoly();
    p2 = readpoly();

    pp = multpoly(p1, p2);
    print(pp);

    ps = addpoly(p1,p2);
    print(ps);

    return 0;
}

polynomial readpoly()
{
    int n, i, coef, expon;
    polynomial p, tail;
    p = (polynomial)malloc(sizeof(struct polynode));
    p->next = NULL;
    tail = p;
    scanf("%d", &n);
    for (i = 0; i < n; ++i)
    {
        scanf("%d %d", &coef, &expon);
        attach(coef, expon, &tail);
    }
    tail = p;
    p = p->next;//如果链表为空,此时的tail->next为空,则返回空值
    free(tail);
    return p;
}

void attach(int coef, int expon, polynomial* pp)
{
    polynomial p, tail;
    p = (polynomial)malloc(sizeof(struct polynode));
    p->next = NULL;
    p->coef = coef;
    p->expon = expon;
    (*pp)->next = p;
    (*pp) = p;
    return;
}

polynomial multpoly(polynomial p1, polynomial p2)
{
    if (!p1 || !p2)
        return NULL;
    polynomial t1 = p1, t2 = p2, p, tail, t;
    int coef, expon;
    p = (polynomial)malloc(sizeof(struct polynode));
    p->next = NULL;
    tail = p;
    while (t2)
    {
        coef = t2->coef * t1->coef;
        expon = t2->expon + t1->expon;
        attach(coef, expon, &tail);
        t2 = t2->next;
    }
    t1 = t1->next;
    while (t1)
    {
        t2 = p2;
        tail = p;
        while (t2)
        {
            coef = t2->coef * t1->coef;
            expon = t2->expon + t1->expon;
            while (tail->next && tail->next->expon > expon)
            {
                tail = tail->next;
            }
            if (tail->next == NULL)
            {
                attach(coef, expon, &tail);
            }
            else
            {
                if (tail->next->expon == expon)
                {
                    if(tail->next->coef + coef)
                        tail->next->coef += coef;
                    else
                    {
                        t = tail->next;
                        tail->next = t->next;
                        free(t);
                    }
                }
                else if (tail->next->expon < expon)
                {
                    t = (polynomial)malloc(sizeof(struct polynode));
                    t->coef = coef;
                    t->expon = expon;
                    t->next = tail->next;
                    tail->next = t;
                }
            }
            t2 = t2->next;
        }
        t1 = t1->next;
    }
    t = p;
    p = p->next;
    free(t);
    return p;
}

polynomial addpoly(polynomial p1, polynomial p2)
{
    polynomial t1 = p1, t2 = p2, tail, t, p;
    int coef, expon;
    p = (polynomial)malloc(sizeof(struct polynode));
    p->next = NULL;
    tail = p;
    while (t1 && t2)
    {
        if (t1->expon == t2->expon && t1->coef + t2->coef != 0)
        {
            coef = t1->coef + t2->coef;
            expon = t1->expon;
            attach( coef, expon, &tail);
            t1 = t1->next;
            t2 = t2->next;
        }
        else if (t1->expon == t2->expon && t1->coef + t2->coef == 0)
        {
            t1 = t1->next;
            t2 = t2->next;
        }
        else if(t1->expon > t2->expon)
        {
            coef = t1->coef;
            expon = t1->expon;
            attach(coef, expon, &tail);
            t1 = t1->next;
        }
        else if (t1->expon < t2->expon)
        {
            coef = t2->coef;
            expon = t2->expon;
            attach(coef, expon, &tail);
            t2 = t2->next;
        }
    }
    while(t1)
    {
        attach(t1->coef, t1->expon, &tail);
        t1 = t1->next;
    }
    while (t2)
    {
        attach(t2->coef, t2->expon, &tail);
        t2 = t2->next;
    }
    t = p;
    p = p->next;
    free(t);
    return p;
}

void print(polynomial p)
{
    int flag = 1;
    if (!p)
    {
        printf("0 0\n");
        return;
    }
    else
    {
        while (p)
        {
            if (flag)
                flag = 0;
            else
                printf(" ");
            printf("%d %d", p->coef, p->expon);
            p = p->next;
        }
    }
    printf("\n");
    return;
}

 

posted @ 2022-02-03 18:00  柳碎花殇  阅读(34)  评论(0)    收藏  举报