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

#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode * Polynomial;

struct PolyNode{
    int coef;   // 系数
    int expon;  //幂
    Polynomial link;
};  //声明结构体需要分号结尾
void Attach(int c,int e,Polynomial *pRear)  //C语言值传递涉及修改实际参数使用指向指针的指针
{
    Polynomial P;
    P = (Polynomial)malloc(sizeof(struct PolyNode));//要添加struct
    P->coef = c;    //对新节点赋值
    P->expon = e;
    P->link = NULL;
    (*pRear)->link = P;
    (*pRear) = P;   //修改pRear的值,此时rear指向链表新的尾部
}


Polynomial ReadPoly(){
    Polynomial P,Rear,t;
    int c,e,N;
    scanf("%d",&N);

    P = (Polynomial)malloc(sizeof(struct PolyNode)); //链表头空节点
    P->link = NULL;
    Rear = P;
    while(N--){
        scanf("%d %d",&c,&e);
        Attach(c,e,&Rear);   //about function(a,b,c) need to konw;
    }
    t = P; P=P->link;free(t);
    return P;
}






void PrintPoly(Polynomial P){ //need to declare P
    int flag = 0;
    if(!P){printf("0 0\n");return ;} // return  is impotant,it could make the program break.
    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,t2,Rear,temp,P;
    t1 = P1;t2 = P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));P->link = NULL;
    Rear = P;
    while(t1&&t2){
        if(t1->expon==t2->expon){
            int sum;
            sum =t1->coef+t2->coef;
            if(sum)
                Attach(sum,t1->expon,&Rear);
            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;
        }
    }
    if(t1)Rear->link=t1;
    if(t2)Rear->link=t2;

    temp = P;
    P = P->link;
    free(temp);
    return P;
}


Polynomial Mult(Polynomial P1,Polynomial P2){
    Polynomial t1,t2,P,Rear,t;
    int c,e;
    if(!P1||!P2)
        return NULL;
    t1 = P1;t2 = P2;
    //先将t1的第一项与t2的每一项相乘,组成链表。
    P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL;
    Rear = P;
    while(t2){
        Attach(t1->coef * t2->coef,t1->expon+t2->expon,&Rear);
        t2 = t2->link;
    }
    //之后将t1的第二项直至最后一项与t2相乘,将每一项插入链表P
    t1 = t1->link;
    while(t1){
        t2 = P2;Rear = P;   //将t2与Rare复原
        while(t2){
            c = t1->coef * t2->coef;
            e = t1->expon + t2->expon;
            //找到插入的位置
            while(Rear->link && Rear->link->expon > e)
                Rear =Rear->link;
            //如果指数相等
            if(Rear->link &&Rear->link->expon == e){
                //如果相加不为0,改变系数
                if(Rear->link->coef+c)
                    Rear->link->coef +=c;
                //or delete the node
                else{
                    t = Rear->link;
                    Rear->link =t->link;
                    free(t);
                }
            }
            //如果不相等,则要插入该项
            else{
                t = (Polynomial)malloc(sizeof(struct PolyNode));
                t->coef = c; t->expon =e;
                t->link = Rear->link;
                Rear->link = t;
            }
            
            t2=t2->link;
        }
        t1 = t1->link;
    }
    t =P;P = P->link;free(t);
    return P;
}




int main(){
	Polynomial P1, P2, PP, PS;
	P1 = ReadPoly();
	P2 = ReadPoly();
	PP = Mult( P1, P2 );
	PrintPoly( PP );
	PS = Add ( P1, P2 );
	PrintPoly( PS );
	//system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值