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

  • 时间:
  • 来源:互联网

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

输入格式:
输入分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 <stdio.h>
#include <stdlib.h>
typedef struct Node *Polynomial;
struct Node{
    int Coef;
    int Exp;
    Polynomial Link;

};
Polynomial ReadPoly();
void Attach(int Coef,int Exp,Polynomial * p);
void Pprint(Polynomial p);
Polynomial Plus(Polynomial p1,Polynomial p2);
Polynomial Muliti(Polynomial p1,Polynomial p2);
void InsertP(Polynomial *p,int Coef,int Exp);
int main(){
    Polynomial p1=NULL,p2=NULL,p3,p4;
    p1 = ReadPoly();
    p2 = ReadPoly();
    p3=Plus(p1,p2);
    p4=Muliti(p1,p2);
    Pprint(p4);
    Pprint(p3);
    return 0;
}
Polynomial ReadPoly(){
    int a,Coef,exp;
    Polynomial p,temp;
    temp = (Polynomial)malloc(sizeof(struct Node));
    temp ->Link = NULL;
    p=temp;
    scanf("%d",&a);
    for(int i=0;i<a;i++){
        scanf("%d%d",&Coef,&exp);
        if(Coef!=0)
            Attach(Coef,exp,&temp);
    }
    temp = p;
    p =p->Link;
    free(temp);
    return p;
}
void Attach(int Coef,int Exp,Polynomial* p){
    Polynomial tmp = (Polynomial)malloc(sizeof(struct Node));
    tmp->Coef =Coef;
    tmp->Exp = Exp;
    tmp->Link =NULL;
    (*p)->Link = tmp;
    *p = tmp;
}
void Pprint(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->Exp);
        p=p->Link;
    }
    printf("\n");
}
Polynomial Plus(Polynomial p1,Polynomial p2){
    Polynomial tmp,p,t3;
    if(!p1&&!p2) return NULL;
    tmp = (Polynomial)malloc(sizeof(struct Node));
    tmp->Link = NULL; // 导致测试用例二不通过。
    p=tmp; //后面不用Attach导致返回的链条直接是原始链条 。
    while(p1 && p2){
        if(p1->Exp == p2->Exp){
            if((p1->Coef+p2->Coef)!=0){
                Attach(p1->Coef+p2->Coef,p1->Exp,&p);
            }
            p1=p1->Link;
            p2=p2->Link;
        }else if(p1->Exp > p2->Exp){
            Attach(p1->Coef,p1->Exp,&p);
             p1=p1->Link;
        }else{
            Attach(p2->Coef,p2->Exp,&p);
            p2=p2->Link;
        }
    }
    while(p1){
         Attach(p1->Coef,p1->Exp,&p);
          p1=p1->Link;
    }
    while(p2){
         Attach(p2->Coef,p2->Exp,&p);
         p2=p2->Link;
    }
    t3 = tmp;
    tmp=tmp->Link;
    free(t3);
    return tmp;
}
Polynomial Muliti(Polynomial p1,Polynomial p2){
    Polynomial t1,t2,t3,p3,t;
    int e,c;
    if(!p1 || !p2){
        return NULL;
    }
    p3 = (Polynomial)malloc(sizeof(struct Node));
    p3->Link = NULL;
    t1=p1;
    t2= p2;
    t3=p3;
    while(t2){
        Attach(t1->Coef*t2->Coef,t1->Exp+t2->Exp,&t3);
        t2 = t2->Link;
    }
    t1=t1->Link;
    while(t1){
        t2=p2;
        t3=p3;
        while(t2){
            c=t1->Coef*t2->Coef;
            e = t1->Exp+t2->Exp;
            while(t3->Link && t3->Link->Exp > e){
                t3=t3->Link;
            }
            if(t3->Link && t3->Link->Exp == e){
                if(t3->Link->Coef+c){
                    t3->Link->Coef +=c;
                }else{
                    t = t3->Link;
                    t3->Link=t3->Link->Link;
                    free(t);
                }
            }else{
                t=(Polynomial)malloc(sizeof(struct Node));
                t->Coef=c;
                t->Exp = e;
                t->Link = t3->Link;
                t3->Link = t;
                t3=t;
            }
            t2 = t2 ->Link;
        }
        t1=t1->Link;
    }
    t3= p3;
    p3= p3->Link;
    free(t3);
    return p3;
}


xingxing12323
发布了39 篇原创文章 · 获赞 0 · 访问量 1192
私信 关注

本文链接http://element-ui.cn/news/show-1210.html