02-线性结构2 一元多项式的乘法与加法运算

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

 

#include <cstdio>
#include <cstdlib>
using namespace std;

typedef struct PolyNode *Polynomial; //
struct PolyNode{    //系数指数
    int coef;
    int expon;
    Polynomial link;
};
void Add(int c,int e,Polynomial * rear)
{
    Polynomial p= new PolyNode;
    p->coef=c;  p->expon=e;
    p->link= NULL;
    (*rear)->link = p;
    *rear= p;
}
Polynomial Mult(Polynomial p1 ,Polynomial p2)
{
    Polynomial p = new PolyNode,t1=p1,t2=p2,t;    p->link=NULL;
    Polynomial rear = p; int e,c;
    if(!p1||!p2) return NULL;
    while(t2)
    {
        if(t2->coef*t1->coef!=0)
            Add(t2->coef*t1->coef,t1->expon+t2->expon,&rear);
        t2=t2->link;
    }
    t1=t1->link;
    while(t1){
        rear=p;t2=p2;
        while(t2){
            e=t1->expon+t2->expon;
            c=t1->coef*t2->coef;
            while(rear->link&&rear->link->expon>e)
                rear=rear->link;
            if(rear->link&&rear->link->expon==e){
                if(rear->link->coef+c)
                    rear->link->coef+=c;
                else
                {
                    t=rear->link;
                    rear->link=t->link;
                    delete t;
                }
            }
            else{
                t=new PolyNode;
                t->coef=c;t->expon=e;
                t->link = rear->link;
                rear->link=t;  rear=rear->link;
            }
            t2=t2->link;
        }
        t1=t1->link;
    }
    t2=p;p=p->link;delete t2;
    return p;

}
Polynomial ReadPoly()
{
    Polynomial P,rear,t;
    P= new PolyNode;
    P->link=NULL;
    rear=P;
    int n,c,e;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&c,&e);
        Add(c,e,&rear);
    }
    t=P;
    P=P->link;
    delete t;
    return P;
}
void print(Polynomial p)
{
    int flag=1;
    if(!p)
        printf("0 0");
    while(p){
        if(flag){
            printf("%d %d",p->coef,p->expon);
            flag=0;
        }
        else{
            printf(" %d %d",p->coef,p->expon);
        }
        p=p->link;
    }
    printf("\n");
}
Polynomial ADD(Polynomial p1,Polynomial p2)
{
    Polynomial p,t1=p1,t2=p2,rear;
    p = new PolyNode;
    p->link =NULL;
    rear = p;
    while(t1&&t2)
    {
        if(t1->expon==t2->expon)
        {
            int s = t1->coef+t2->coef;
            if(s!=0)
            {
                Add(s,t1->expon,&rear);
            }
            t1=t1->link;
            t2=t2->link;
        }
        else
        {
            if(t1->expon>t2->expon)
            {
                Add(t1->coef,t1->expon,&rear);
                t1=t1->link;
            }
            else
            {
                Add(t2->coef,t2->expon,&rear);
                t2=t2->link;
            }
        }
    }
    while(t2)
    {
        Add(t2->coef,t2->expon,&rear);
        t2=t2->link;
    }
    while(t1)
    {
        Add(t1->coef,t1->expon,&rear);
        t1=t1->link;
    }
    t1=p;
    p=p->link;
    delete t1;
    return p;
}
int main(){
    //freopen("in.txt","r",stdin);
    Polynomial p1,p2,pp,ps;
    p1=ReadPoly();
    p2=ReadPoly();
    pp=Mult(p1,p2);
    print(pp);
    ps=ADD(p1,p2);
    print(ps);


    return 0;
}

 

posted on 2016-10-15 22:21  acmtime  阅读(439)  评论(0编辑  收藏  举报

导航