浙大数据结构习题笔记:一元多项式的乘法与加法运算

一元多项式的乘法与加法运算

虽然是mooc例题,但码下来觉得代码量也不小,写完对单链表的插入删除内存处理也有了点了解

最后在PTA上提交后,一开始没有注意到格式,后来微调了一下格式才搞好。

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 <malloc.h>

typedef struct PolyNode *Polynomial;
struct PolyNode{
    int num;
    int exp;
    Polynomial link;
};

void Attach(int num,int exp,Polynomial *pRear)
{
    Polynomial P;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->num = num;
    P->exp = exp;
    P->link = NULL;
    (*pRear)->link = P;
    *pRear = P;
}

Polynomial ReadPoly()
{
    int N,num,exp;
    Polynomial P,rear,temp;
    scanf("%d",&N);
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    rear = P;
    while(N--){
        scanf("%d %d",&num,&exp);
        Attach(num,exp,&rear);
    }
    temp = P;
    P=P->link;
    free(temp);
    return P;
}

Polynomial Add(Polynomial P1,Polynomial P2)
{
    Polynomial P,rear,t1,t2,t;
    int sum;
    t1 = P1;
    t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    rear = P;
    while(t1 && t2){
        if(t1->exp == t2->exp){
            sum = t1->num + t2->num;
            if(sum != 0){
                Attach(sum,t1->exp,&rear);
            }
            t1 = t1->link;
            t2 = t2->link;
        }else if(t1->exp > t2->exp){
            Attach(t1->num,t1->exp,&rear);
            t1 = t1->link;
        }else{
            Attach(t2->num,t2->exp,&rear);
            t2 = t2->link;
        }
    }
    while(t1){
        Attach(t1->num,t1->exp,&rear);
        t1 = t1->link;
    }
    while(t2){
        Attach(t2->num,t2->exp,&rear);
        t2 = t2->link;
    }

    rear->link = NULL;
    t = P;
    P = P->link;
    free(t);
    return P;
}

Polynomial Mult(Polynomial P1,Polynomial P2)
{
    Polynomial P,rear,t1,t2,t;
    int num,exp;

    if(!P1 || !P2){
        return NULL;
    }

    t1 = P1;
    t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    rear = P;

    while(t2){	//先令P1第一项与P2所有相乘,形成第一批P链
        Attach(t1->num*t2->num,t1->exp+t2->exp,&rear);
        t2 = t2->link;
    }
    t1 = t1->link; 

    while(t1){
        t2 = P2;
        rear = P;
        while(t2){
            num = t1->num * t2->num;
            exp = t1->exp + t2->exp;

            while(rear->link && rear->link->exp > exp){
                rear = rear->link;
            }  	//定位rear的位置
            if(rear->link && rear->link->exp == exp){
                if(rear->link->num + num != 0){
                    rear->link->num += num;
                }else{
                    t = rear->link;
                    rear->link = t->link;
                    free(t);
                }
            }else{
                t = (Polynomial)malloc(sizeof(struct PolyNode));
                t->num = num;
                t->exp = exp;
                t->link = rear->link;  
                rear->link = t;
                rear = rear->link;
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }

    t2 = P;
    P = P->link;
    free(t2);

    return P;
}

void PrintPoly(Polynomial P)
{
    int flag = 0;

    if(!P){
        printf("0 0");
        return ;
    }
    while(P){
        if(flag == 0){
            flag = 1;
        }else{
            printf(" ");
        }
        printf("%d %d",P->num,P->exp);
        P = P->link;
    }
}

int main()
{
    Polynomial P1,P2,Plus,Sum;

    P1 = ReadPoly();
    P2 = ReadPoly();
    Plus = Mult(P1,P2);
    PrintPoly(Plus);
    printf("\n");
    Sum = Add(P1,P2);
    PrintPoly(Sum);


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值