首页 > 其他分享 >PTA一元多项式的乘法与加法运算

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

时间:2022-11-15 13:55:23浏览次数:58  
标签:多项式 t2 PTA t1 link 加法 Polynomial expon

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

输入格式:

输入分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

注意: 这一题如果零多项式(多项式项数为0 区别于项数为1 系数为0指数为0的多项式)和另外一个多项式 相乘为零多项式 相加为另一个多项式

#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode{
    int coef;
    int expon;
    Polynomial link;
};

Polynomial ReadPoly(void); // 采用带头节点插入的方法建立多项式链表 先建立带头节点的链表读入多项式数据 最后删除头节点
Polynomial Mult(Polynomial P1,Polynomial P2);
Polynomial Add(Polynomial P1,Polynomial P2);
void PrintPoly(Polynomial P);
void Attach(int coef,int expon, Polynomial *pRear); //增加新节点到多项式链表后 pRear 指向链表最后节点地址的指针
void DeletePoly(Polynomial p); // 删除一个多项式链表

int main(){
    Polynomial P1,P2,PP,PS;
    
    P1 = ReadPoly();
 //   PrintPoly(P1);
    P2 = ReadPoly();
    PP = Mult(P1,P2);
    PrintPoly(PP);
    PS = Add(P1,P2);
    PrintPoly(PS);

    return 0;
}

void Attach(int coef,int expon, Polynomial *pRear){
    Polynomial p;
    p = (Polynomial)malloc(sizeof(struct PolyNode));
    p->coef = coef;
    p->expon = expon;
    p->link = NULL;
    (*pRear)->link = p;
    *pRear = p;
}

Polynomial ReadPoly(void){
    Polynomial P,Rear,t;
    int n;
    int coef,expon;
    scanf("%d",&n);
    P = (Polynomial)malloc(sizeof(struct PolyNode)); // 建立链表空的头节点
    P->link = NULL;
    Rear = P;
    while (n--) {
        scanf("%d %d",&coef,&expon);
        Attach(coef,expon,&Rear);
    }
    t = P;
    P = P->link;
    free(t);
    return P;
}

void PrintPoly(Polynomial P){
    int flag = 0;
    if(P == NULL){
        printf("0 0\n");
        return;
    }
// 最后一个没有空格 相当于除了开始,后面%d %d输出中间都有空格
    while (P) {
        if(!flag)
            flag = 1;
        else
            printf(" ");
        printf("%d %d",P->coef,P->expon);
        P = P->link;
    }
    printf("\n");
}

Polynomial Add(Polynomial P1,Polynomial P2){
    Polynomial t1 = P1,t2 = P2,t;
    Polynomial p,rear;
    p = (Polynomial)malloc(sizeof(struct PolyNode));
    rear = p;
    while (t1 && t2) {
        if(t1->expon == t2->expon){
            if(t1->coef + t2->coef){
                Attach(t1->coef+t2->coef, t1->expon, &rear);
                t1 = t1->link;
                t2 = t2->link;
            }else{
                t1 = t1->link;
                t2 = t2->link;
            }
        }else if (t1->expon > t2->expon){
            Attach(t1->coef, t1->expon, &rear);
            t1 = t1->link;
        }else{
            Attach(t2->coef, t2->expon, &rear);
            t2 = t2->link;
        }
    }
    while (t1) {
        Attach(t1->coef, t1->expon, &rear);
        t1 = t1->link;
    }
    while (t2) {
        Attach(t2->coef, t2->expon, &rear);
        t2 = t2->link;
    }
    t = p;
    p = p->link;
    free(t);
    return p;
}

// 多项式相乘算法:
// 第一个多项式每一项乘以第二个多项式累加求法
// 第一个多项式第一项和第二个多项式相乘得到结果多项式p
// 第一个多项式从第二项开始和第二个多项式相乘得到q作为中间多项式逐步和结果多项式p累加
Polynomial Mult(Polynomial P1,Polynomial P2){
    Polynomial p = (Polynomial)malloc(sizeof(struct PolyNode)); // 定义结果多项式
    p->link = NULL;
    Polynomial t1 = P1,t2 = P2;
    Polynomial rear = p;
    Polynomial q; // 中间多项式
    Polynomial t; // 用于释放空的头节点
    if(t1 == NULL || t2 == NULL){
        p->expon = 0;
        p->coef = 0;
        return p;
    }
    while (t2) {
        Attach(t1->coef*t2->coef, t1->expon+t2->expon, &rear);
        t2 = t2->link;
    }
    t = p;
    p = p->link;
    free(t);
    t1 = t1->link;
    while (t1) {
        q = (Polynomial)malloc(sizeof(struct PolyNode));
        q->link = NULL;
        Polynomial qRear = q;
        t2 = P2;
        while (t2) {
            Attach(t1->coef*t2->coef, t1->expon+t2->expon, &qRear);
            t2 = t2->link;
        }
        t = q;
        q = q->link;
        free(t);
        p = Add(p, q);
        DeletePoly(q);
        t1 = t1->link;
    }
    return p;
}

void DeletePoly(Polynomial p){
    Polynomial t = p;
    while (p) {
        p = p->link;
        free(t);
        t = p;
    }
}

 

标签:多项式,t2,PTA,t1,link,加法,Polynomial,expon
From: https://www.cnblogs.com/xinrenbool/p/16892199.html

相关文章