PTA一元多项式的乘法与加法c++版——山东科技大学

一道链表模板题,写下以后复习用 

#include<bits/stdc++.h>
using namespace std;
typedef struct number
{
    int xi;
    int zhi;
    struct number *next;
} number,*linklist;

linklist l1,l2;
linklist l;
linklist ml;//乘法链表
int m,n;
void init()
{
    l1=(number*)malloc(sizeof(number));
    l1->next=NULL;
    cin>>n;
    number *a,*b;
    a=l1;
    for(int i=0; i<n; i++)
    {
        b=(number*)malloc(sizeof(number));
        cin>>b->xi>>b->zhi;
        b->next=NULL;
        a->next=b;
        a=b;
    }

    l2=(number*)malloc(sizeof(number));
    l2->next=NULL;
    cin>>m;
    number *c,*d;
    c=l2;
    for(int i=0; i<m; i++)
    {
        d=(number*)malloc(sizeof(number));
        cin>>d->xi>>d->zhi;
        d->next=NULL;
        c->next=d;
        c=d;
    }
}
void print()
{
    number *a;
    a=ml->next;
    int flag=0,x=0;
    while(a!=NULL)
    {
        if(a->xi!=0)
        {
            if(x)
                cout<<' ';
            cout<<(a->xi)<<' '<<(a->zhi);
            flag=1;
            x++;
        }
        a=a->next;
    }
    if(flag==0)
        cout<<0<<' '<<0;
    cout<<endl;
    flag=0;
    x=0;
    a=l->next;
    while(a!=NULL)
    {
        if(a->xi!=0)
        {
            if(x)
                cout<<' ';
            cout<<(a->xi)<<' '<<(a->zhi);
            flag=1;
            x++;
        }

        a=a->next;
    }
    if(flag==0)
        cout<<0<<' '<<0;
}
void goadd()
{
    number *a1,*a2,*a,*b;
    a1=l1->next;
    a2=l2->next;
    l=(number*)malloc(sizeof(number));
    l->next=NULL;
    a=l;
    while(a1!=NULL&&a2!=NULL)
    {
        if(a1->zhi==a2->zhi)
        {
            b=(number*)malloc(sizeof(number));
            b->xi=a1->xi+a2->xi;
            b->zhi=a1->zhi;
            b->next=NULL;
            a->next=b;
            a=b;
            a1=a1->next;
            a2=a2->next;
        }
        else if(a1->zhi < a2->zhi)
        {
            b=(number*)malloc(sizeof(number));
            b->xi=a2->xi;
            b->zhi=a2->zhi;
            b->next=NULL;
            a->next=b;
            a=b;
            a2=a2->next;
        }
        else
        {
            b=(number*)malloc(sizeof(number));
            b->xi=a1->xi;
            b->zhi=a1->zhi;
            b->next=NULL;
            a->next=b;
            a=b;
            a1=a1->next;
        }
    }
    if(a1==NULL)
    {
        while(a2!=NULL)
        {
            b=(number*)malloc(sizeof(number));
            b->xi=a2->xi;
            b->zhi=a2->zhi;
            b->next=NULL;
            a->next=b;
            a=b;
            a2=a2->next;
        }
    }
    if(a2==NULL)
    {
        while(a1!=NULL)
        {
            b=(number*)malloc(sizeof(number));
            b->xi=a1->xi;
            b->zhi=a1->zhi;
            b->next=NULL;
            a->next=b;
            a=b;
            a1=a1->next;
        }
    }
}
void gomul()
{
    number *a1,*a2,*a,*b;
    a1=l1->next;
    a2=l2->next;
    ml=(number*)malloc(sizeof(number));
    ml->next=NULL;
    a=ml;
    while(a2!=NULL)
    {
        while(a1!=NULL)
        {
            b=(number*)malloc(sizeof(number));
            b->xi=(a1->xi)*(a2->xi);
            b->zhi=a1->zhi+a2->zhi;
            b->next=NULL;
            a->next=b;
            a=b;
            a1=a1->next;
        }
        a1=l1->next;
        a2=a2->next;
    }
    a=ml->next;
    while(a!=NULL)
    {
        b=a;
        while(b->next!=NULL)
        {
            if(b->next->zhi==a->zhi)
            {
                a->xi+=b->next->xi;
                b->next=b->next->next;
            }
            else
            {
                b=b->next;
            }
        }
        a=a->next;
    }
}
int main()
{
    init();
    goadd();
    gomul();
    print();
    return 0;
}

ps:代码仅供参考,请勿抄袭

猜你喜欢

转载自blog.csdn.net/scorpion_legend/article/details/109131291