【用链表实现一元多项式的加法和乘法运算】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
//这里看不明白的话可以看看c语言中typedef的用法:https://blog.csdn.net/theonegis/article/details/40049667
typedef struct PNode
{
    int coef;     //系数
    int expn;       //指数
    struct PNode *next;
}PNode, *Polynomial;
void Attach(int c, int e, Polynomial *Rear)     //将新节点插入到链表的尾部
{
    Polynomial P = (Polynomial)malloc(sizeof(PNode));
    P->coef = c;
    P->expn = e;
    P->next = NULL;
    (*Rear)->next = P;
    *Rear = P;
}
Polynomial Read()
{
    Polynomial L = (Polynomial)malloc(sizeof(PNode));
    L->next = NULL;
    int n;
    scanf("%d", &n);
    Polynomial rear = L;
    while(n--)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        Attach(a, b, &rear);     
    }
    Polynomial t = L;
    L = L->next;
    free(t);        //删除临时生成的头节点
    return L;
}
Polynomial Add(Polynomial P1, Polynomial P2)            //两个多项式的加法
{
    Polynomial t1 = P1, t2 = P2;
    Polynomial P = (Polynomial)malloc(sizeof(PNode));          //注意malloc出的内存只能用free删除,不能用别的
    P->next = NULL;
    Polynomial rear = P;
    while(t1 && t2)
    {
        if(t1->expn == t2->expn)
        {
            int sum = t1->coef + t2->coef;
            int expn = t1->expn;
            if(sum)     //若和不为0,直接将它插入到新链表的末端
                Attach(sum, expn, &rear);           
            t1 = t1->next;
            t2 = t2->next;
        }
        else if(t1->expn < t2->expn)        //若指数不相等,则插入指数较大的
        {
            int coef = t2->coef, expn = t2->expn;
            Attach(coef, expn, &rear);
            t2 = t2->next;
        }
        else if(t1->expn > t2->expn)
        {
            int coef = t1->coef, expn = t1->expn;
            Attach(coef, expn, &rear);
            t1 = t1->next;
        }
    }
    while(t1)       //若有剩余的,依次插入即可
    {
        Attach(t1->coef, t1->expn, &rear);
        t1 = t1->next;
    }
    while(t2)
    {
        Attach(t2->coef, t2->expn, &rear);
        t2 = t2->next;
    }
    t2 = P;
    P = P->next;
    free(t2);   //释放头节点
    return P;
}
Polynomial Mul(Polynomial p1, Polynomial p2)
{ 
    if(!p1 || !p2) return NULL;
    Polynomial t1 = p1, t2 = p2;
    Polynomial P = (Polynomial)malloc(sizeof(PNode));
    P->next = NULL;
    Polynomial rear = P;
    while(t2)       //先用p1的第一项乘以p2的每一项,得到P
    {
        Attach(t1->coef * t2->coef, t1->expn + t2->expn, &rear);
        t2 = t2->next;
    }
    t1 = t1->next;      //得到p1的下一项,根据前面得到的P,对两个多项式逐项相乘(前面算过的就不用再算了),将它们插入到合适的位置
    while(t1)
    {
        t2 = p2, rear = P;      //另一个多项式从初始位置开始
        while(t2)
        {
            int e = t1->expn + t2->expn, c = t1->coef * t2->coef;       //两项的*运算
            while(rear->next && rear->next->expn > e)       //找到下一项的指数小于或等于e的
                rear = rear->next;
            if(rear->next && rear->next->expn == e)     //如果找到指数相同的
            {
                if(rear->next->coef + c !=  0)      //系数不为0则加起来
                    rear->next->coef += c;
                else        //系数为零了就把这个系数为零的项删除
                {
                    Polynomial t = rear->next;
                    rear->next = t->next;
                    free(t);
                }
            }
            else //小于e,则把新项插入到它之后,没理解就慢慢想,画图
            {
                Polynomial t = (Polynomial)malloc(sizeof(PNode));
                t->coef = c;
                t->expn = e;
                t->next = rear->next;
                rear->next = t;
                rear = rear->next;
            }
            t2 = t2->next;      //下一项
        }
        t1 = t1->next;      //下一项
    }
    t2 = P;
    P = P->next;
    free(t2);
    return P;
}
void Print(Polynomial P)
{
    int flag = 0;
    if(!P)
    {
        printf("0 0\n");
        return ;
    }
    while(P)
    {
        if(!flag)
            flag = 1;
        else 
            printf(" ");
        printf("%d %d", P->coef, P->expn);
        P = P->next;
    }
    printf("\n");
}
int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    Polynomial L1, L2;
    L1 = Read();
    L2 = Read();
    Polynomial sum = Add(L1, L2);
    Polynomial mul = Mul(L1, L2);
    Print(mul);
    Print(sum);
}
posted @ 2019-09-12 16:39  DIY-Z  阅读(1011)  评论(0编辑  收藏  举报