ignore__ 2020-02-25 09:43 采纳率: 100%
浏览 450
已采纳

c语言,一元多项式相乘,相乘结果莫名少第一项

图片说明

输入样例:

4 3 4 -5 2 6 1 -2 0

3 5 20 -7 4 3 1

#include<stdio.h>
#include<stdlib.h>
typedef struct Node *Poly;
struct Node{
    int coef;
    int expon;
    Poly link;
};
Poly read(void);
void Attach(int c,int e,Poly *R);
Poly Sum(Poly p1,Poly p2);
Poly Mult(Poly p1,Poly p2);
void print(Poly p);
void freep(Poly p);
int main(void)
{
    Poly p1,p2;
    p1 = read();
    p2 = read();
    print(Mult(p1,p2));
    print(Sum(p1,p2));
    return 0;
}
Poly read(void)
{
    int N,c,e;
    Poly p,Rear,t;
    scanf("%d",&N);
    p = (Poly)malloc(sizeof(struct Node));
    p->link = NULL;
    Rear = p;
    while(N--)
    {
        scanf("%d %d",&c,&e);
        Attach(c,e,&Rear);
    }
    t = p;
    p = p->link;
    free(t);
    return p;
}
void Attach(int c,int e,Poly *R)
{
    Poly p;
    p = (Poly)malloc(sizeof(struct Node));
    p->coef = c;
    p->expon = e;
    p->link=NULL;
    (*R)->link = p;
    *R = p;
}
Poly Sum(Poly p1,Poly p2)
{
    Poly t1,t2,phead,Rear,t;
    t1 = p1;
    t2 = p2;
    phead = (Poly)malloc(sizeof(struct Node));
    phead->link = NULL;
    Rear = phead;
    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 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 = phead;
    phead = phead->link;
    free(t);
    return phead;
}
Poly Mult(Poly p1,Poly p2)
{
    Poly t1,t2,t,Rear,phead;
    int c,e;
    if(!p1||!p2)
    {
        return NULL;
    } 
    phead=(Poly)malloc(sizeof(struct Node));
    phead->link = NULL;
    Rear = phead;
    t1 = p1;
    t2 = p1;
    while(t2)
    {
        Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
        t2 = t2->link; 
    }
    t1 = t1->link;
    while(t1)
    {
        t2 = p2;
        Rear = phead;
        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)
            {
                if(c+Rear->link->coef)
                {
                    Rear->link->coef+=c;
                }
                else 
                {
                    t = Rear->link;
                    Rear->link = t->link;
                    free(t);
                }
            }
            else 
            {
                t=Rear->link;
                Rear->link = NULL;
                Attach(c,e,&Rear);
                Rear->link = t;
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t = phead;
    phead = phead->link;
    free(t);
    return phead;
}
void print(Poly 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->link;
    }
    printf("\n");
}



  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-02-25 10:59
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器