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

#include<stdio.h>
#include<stdlib.h>
typedef struct Node *Pet;
typedef Pet List;
struct Node{
    int cef;
    int exp;
    Pet Next;
};
void Print(List p);
List read()
{
    List p1,head,t;
    int i,n,a,b;
    scanf("%d",&n);
    t=(Pet)malloc(sizeof(struct Node));
            head=t;
    for(i=0;i<n;i++){
        scanf("%d %d",&a,&b);
        p1=(Pet)malloc(sizeof(struct Node));
        p1->cef=a;
        p1->exp=b;
        head->Next=p1;
        head=p1;//
    }
    head->Next=NULL;
    return t->Next;
}
List addition(List P1,List P2){
    List temp,head,t,pa,pb;
    pa=P1;
    pb=P2;
    
    head=(Pet)malloc(sizeof(struct Node));
    head->Next=NULL;
    t=head;
    while(pa&&pb){
    temp=(Pet)malloc(sizeof(struct Node));
        if(pa->exp<pb->exp){
            temp->cef=pb->cef;
            temp->exp=pb->exp;
            t->Next=temp;
            t=temp;
            pb=pb->Next;
        }
        else if(pa->exp==pb->exp){
            temp->cef=pb->cef+pa->cef;
            temp->exp=pb->exp;
            t->Next=temp;
            t=temp;
            pb=pb->Next;
            pa=pa->Next;
        }
        else  if(pa->exp>pb->exp){
            temp->cef=pa->cef;
            temp->exp=pa->exp;
            t->Next=temp;
            t=temp;
            pa=pa->Next;
}
           
    }
    if(pa)  
t->Next = pa;
else if(pb) 
t->Next = pb;
return head->Next;
}
void Print(List p){
    List t=p;
    t=p;
    int flag=1;
    for(;t;t=t->Next){
        if(t->cef){
             printf("%d %d",t->cef,t->exp);
        flag=0;}
        if(!flag&&t->cef){
            printf(" ");
        }}
        if(flag)
        printf("0 0");
    printf("\n");
    }
List multiplication(List p1,List p2){
    List a,b,mul,temp,head;
    a=p1;
    b=p2;
    mul=(List)malloc(sizeof(struct Node));
   mul->Next=NULL;
    head=mul;
    for(;a;a=a->Next){
        
 for(b=p2;b;b=b->Next){
        temp=(List)malloc(sizeof(struct Node));
        temp->cef=(a->cef)*(b->cef);
        temp->exp=(a->exp)+(b->exp);
                temp->Next=NULL;
        head=addition(temp,mul);
        mul=head;
    }
    }
    
    return head;//
}
int main(){
    List L1,L2,ad,mul;
    L1 = read();
    L2 = read();
    ad = addition(L1,L2);
    mul = multiplication(L1,L2);
    Print(mul);
    Print(ad);
    return 0;

 

这个输出的结果不知道为什么会有一个很大的数字和0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值