02-线性结构2 一元多项式的乘法与加法运算 (20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
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 <iostream>
using namespace std;
typedef struct Node *PtrToNode;
struct Node{
int col;
int exp;
PtrToNode next;
};
typedef PtrToNode List;
List read(){
int N,col,exp;
scanf("%d",&N);
List head=(struct Node*)malloc(sizeof(struct Node));//头结点
head->next=NULL;//设置头结点
if(N){//N有可能为0
List p=head;//指针
for(int i=0;i<N;i++){
List cur=(struct Node*)malloc(sizeof(struct Node));
scanf("%d %d",&cur->col,&cur->exp);
p->next=cur;
p=p->next;
}
p->next=NULL;//设置尾指针
}
return head;
}
List Listadd(List L1,List L2);
List Listplus(List L1,List L2){
List L=(struct Node*)malloc(sizeof(struct Node));//L也有头指针
L->next=NULL;
List p1=L1->next;//跨过头指针
List p2=L2->next;
while(p1){
List tp=(List)malloc(sizeof(struct Node));
tp->next=NULL;
List t=tp;
p2=L2->next;
while(p2){
List temp=(struct Node*)malloc(sizeof(struct Node));
temp->col=(p1->col)*(p2->col);
temp->exp=(p1->exp)+(p2->exp);
temp->next=NULL;
t->next=temp;
t=t->next;
p2=p2->next;
}
t->next=NULL;
L=Listadd(L,tp);
p1=p1->next;
}
return L;
}
List Listadd(List L1,List L2){
List L=(struct Node*)malloc(sizeof(struct Node));
L->next=NULL;
List pL=L;
List p1=L1->next;//跨过头指针
List p2=L2->next;
while(p1&&p2){
if(p1->exp==p2->exp){
if((p1->col+p2->col)!=0){
List temp=(struct Node*)malloc(sizeof(struct Node));
temp->col=p1->col+p2->col;
temp->exp=p1->exp;
temp->next=NULL;
pL->next=temp;
pL=pL->next;
}
p1=p1->next;
p2=p2->next;
}else if(p1->exp>p2->exp){
List temp=(struct Node*)malloc(sizeof(struct Node));
temp->col=p1->col;
temp->exp=p1->exp;
temp->next=NULL;
pL->next=temp;
p1=p1->next;
pL=pL->next;
}else{
List temp=(struct Node*)malloc(sizeof(struct Node));
temp->col=p2->col;
temp->exp=p2->exp;
temp->next=NULL;
pL->next=temp;
p2=p2->next;
pL=pL->next;
}
}
//还有没被加入的余链
if(p1){
while(p1){
List temp=(struct Node*)malloc(sizeof(struct Node));
temp->col=p1->col;
temp->exp=p1->exp;
temp->next=NULL;
pL->next=temp;
pL=pL->next;
p1=p1->next;
}
}
if(p2){
while(p2){
List temp=(struct Node*)malloc(sizeof(struct Node));
temp->col=p2->col;
temp->exp=p2->exp;
temp->next=NULL;
pL->next=temp;
pL=pL->next;
p2=p2->next;
}
}
pL->next=NULL;
return L;
}
void print(List L){
List t=L;
L=L->next;
free(t);
if(L==NULL){
printf("0 0");
}else{
int flag=1;
while(L){
if(flag){
flag=0;
}else{
printf(" ");
}
printf("%d %d",L->col,L->exp);
L=L->next;
}
}
printf("\n");
}
int main(){
List L1,L2,LP,LA;
L1=read();
L2=read();
LP=Listplus(L1,L2);
print(LP);
LA=Listadd(L1,L2);
print(LA);
return 0;
}
主要思路
1.先编写主函数
int main(){
List L1,L2,LP,LA;
L1=read();
L2=read();
LP=Listplus(L1,L2);
print(LP);
LA=Listadd(L1,L2);
print(LA);
return 0;
}
2.编写4个函数
read()
Listplus(L1,L2);
Listadd(L1,L2);
print();
3.注意事项(踩坑/跃坑)
typedef struct Node *PtrToNode;
struct Node{
int col;
int exp;
PtrToNode next;
};
typedef PtrToNode List;
将struct Node定义为指针*PtrToNode
typedef struct Node *PtrToNode;
typedef PtrToNode List;
结构体中的next
和以Node为元素的List
都是Node的指针
!!!