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

``````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

``````

``````#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode *PolyNomial;
struct PolyNode{
int coef;
int expon;
};
void Attach(int c,int e,PolyNomial* prear){
PolyNomial P;
P=(PolyNomial)malloc(sizeof(struct PolyNode));
P->coef=c;
P->expon=e;
*prear=P;
}
PolyNomial t1,t2,rear,p,front;
p=(PolyNomial)malloc(sizeof(struct PolyNode));
rear=p;
front=rear;
if(!L1||!L2)return NULL;
t1=L1;
t2=L2;
while(t1!=NULL&&t2!=NULL){
if(t1->expon==t2->expon){
int sum=t1->coef+t2->coef;
Attach(sum,t1->expon,&rear);
}
else if(t1->expon<t2->expon){
Attach(t1->coef,t1->expon,&rear);
}
else{
Attach(t2->coef,t2->expon,&rear);
}
}
while(t1){
Attach(t1->coef,t1->expon,&rear);
}
while(t2){
Attach(t2->coef,t2->expon,&rear);
}
return front;
}
PolyNomial Multi(PolyNomial L1,PolyNomial L2){
PolyNomial t1,t2,rear,p,front,t;
int c,e;
p=(PolyNomial)malloc(sizeof(struct PolyNode));
rear=p;
front=rear;
t1=L1;
t2=L2;
while(t2){
Attach(t1->coef*t2->coef,t1->expon+t2->expon,&rear);
}
while(t1){
t2=L2;
rear=p;
while(t2){
c=t1->coef*t2->coef;
e=t1->expon+t2->expon;
}
}
else{
free(t);
}
}
else{
t=(PolyNomial)malloc(sizeof(struct PolyNode));
t->coef=c;
t->expon=e;
}
}
}
return front;
}
PolyNomial P,rear,t;
P=(PolyNomial)malloc(sizeof(struct PolyNode));
rear=P;
int n,c,e;
scanf("%d",&n);
while(n--){
scanf("%d %d",&c,&e);
Attach(c,e,&rear);
}
t=P;
free(t);
return P;
}
void PrintPoly(PolyNomial P){
printf("%d %d",P->coef,P->expon);
while(P){
printf(" %d %d",P->coef,P->expon);
}
}
int main(){
PolyNomial L1,L2,m1,m2;
m1=Multi(L1,L2);
PrintPoly(m1);
PrintPoly(m2);
}
``````

``````a.cpp: In function ‘PolyNode* ReadPoly()’:
a.cpp:105:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
a.cpp:107:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&c,&e);
``````

1个回答

ychen4_13 具体如何实现，加在什么位置
3 个月之前 回复