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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct PolyNode *Polynomial;
struct PolyNode
{
    int coef;
    int expon;
    Polynomial next;
};
Polynomial ReadPoly();
void Attch(int c,int e,Polynomial *Prear);
Polynomial Mult(Polynomial p1,Polynomial p2);
Polynomial Add(Polynomial P1,Polynomial p2);
void PrintPoly(Polynomial p);
int main()
{
    Polynomial p1,p2,pp,ps;
    p1=ReadPoly();
    p2=ReadPoly();
    pp=Mult(p1,p2);
    PrintPoly(pp);
    ps=Add(p1,p2);
    PrintPoly(ps);
    return 0;
} 
Polynomial ReadPoly()
{
    int N;
    int c,e;
    Polynomial p,rear,t;
    p=(Polynomial)malloc(sizeof(struct PolyNode));
    p->next=NULL;
    rear=p;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d%d",&c,&e);
        Attch(c,e,&rear);
    }
    t=p;
    p=p->next;
    free(t);
    return p;
}
void Attch(int c,int e,Polynomial *Prear)
{
    Polynomial p;
    p=(Polynomial)malloc(sizeof(struct PolyNode));
    p->coef=c;
    p->expon=e;
    p->next=NULL;
    (*Prear)->next=p;
    *Prear=p;
}
Polynomial Mult(Polynomial p1,Polynomial p2)
{
    Polynomial t1,t2,p,rear,t;
    t1=p1;t2=p2;
    int c,e;
    if(p1==NULL||p2==NULL)
    return NULL;
    p=(Polynomial)malloc(sizeof(struct PolyNode));
    p->next =NULL;
    rear = p;
    while(t2)
    {
        Attch(t1->coef*t2->coef,t1->expon+t2->expon,&rear);
        t2=t2->next;
    }
    t1=t1->next;
    while(t1)
    {
        t2=p2;rear=p;
        while(t2)
        {
            e=t1->expon+t2->expon;                      //指数; 
            c=t1->coef*t2->coef;                        //系数; 
            while(rear->next&&rear->next->expon>e)          //判断rear->next是否存在,并且是否大于e; 
            {
                rear=rear->next;
            }
            if(rear->next&&rear->next->expon==e)        //指数相等 
            {
                if(rear->next->coef+c)                  //判断系数相加是否为零 
                {
                    rear->next->coef+=c;
                }
                else
                {
                    t=rear->next;
                    rear->next=t->next;
                    free(t);
                }
            }
            else
            {
                t=(Polynomial)malloc(sizeof(struct PolyNode));
                t->coef=c;
                t->expon=e;
                t->next=rear->next;
                rear->next =t;
                rear=rear->next;
            }
            t2=t2->next;
        }
        t1=t1->next;
    }
    t2=p;p=p->next;free(t2);
    return p;
}
Polynomial Add(Polynomial p1,Polynomial p2)
{
    Polynomial t1,t2,p,rear,t;
    p=(Polynomial)malloc(sizeof(struct PolyNode));
    p->next=NULL;
    rear=p;
    t1=p1;
    t2=p2;
    while(t1&&t2)
    {
        if(t1->expon>t2->expon)
        {
            Attch(t1->coef,t1->expon,&rear);
            t1=t1->next;
        }
        else if(t1->expon==t2->expon)
        {
            if(t1->coef+t2->coef==0)
            {
                t1=t1->next;
                t2=t2->next;
            }
            else
            {
                Attch(t1->coef+t2->coef,t1->expon,&rear);
                t1=t1->next;
                t2=t2->next;
            }
        }
        else
        {
            Attch(t2->coef,t2->expon,&rear);
            t2=t2->next; 
        }

    }
    if(t1==NULL)
    {
        rear->next=t2;
    }
    else
    {
        rear->next=t1;
    }
    t=p;
    p=p->next;
    free(t);
    return p;
}
void PrintPoly(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->expon);
        p=p->next;
    }
    printf("\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值