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
结尾无空行
//写的太乱了/(ㄒoㄒ)/~~
#include<stdio.h>
#include<stdlib.h>
//链表实现
struct node{
int cofe;
int expn;
struct node *next;
};
typedef struct node* Node;
Node Creatlist()//带头结点
{
Node head;
head=(Node)malloc(sizeof(struct node));
head->next=NULL;
return head;
}
void AddNode(Node rear,int cofe,int expn)
{
Node Newp;
Newp=(Node)malloc(sizeof(struct node));
Newp->cofe=cofe;
Newp->expn=expn;
Newp->next=NULL;
rear->next=Newp;
rear=Newp;
}
Node Findrear(Node head)
{
while(head->next)
{
head=head->next;
}
return head;
}
void print(Node head)
{
head=head->next;
if(head==NULL)printf("0 0\n");
else
{
while(head)
{
printf("%d %d",head->cofe,head->expn);
head=head->next;
if(head)printf(" ");
}
printf("\n");
}
}
Node Mix(Node head1,Node head2)
{
Node List,rear,Newp;
List=(Node)malloc(sizeof(struct node));
List->next=NULL;
head1=head1->next;
head2=head2->next;
while(head1&&head2)
{
Newp=(Node)malloc(sizeof(struct node));
if(head1->expn>head2->expn)
{
Newp->cofe=head1->cofe;
Newp->expn=head1->expn;
head1=head1->next;
}
else if(head1->expn<head2->expn)
{
Newp->cofe=head2->cofe;
Newp->expn=head2->expn;
head2=head2->next;
}
else
{
Newp->cofe=head1->cofe+head2->cofe;
Newp->expn=head1->expn;
head1=head1->next;
head2=head2->next;
}
if(Newp->cofe){
Newp->next=NULL;
rear=Findrear(List);
rear->next=Newp;
rear=Newp;
}
else free(Newp);//系数和为0;
}
while(head1)
{
Newp=(Node)malloc(sizeof(struct node));
Newp->cofe=head1->cofe;
Newp->expn=head1->expn;
Newp->next=NULL;
rear=Findrear(List);
rear->next=Newp;
rear=Newp;
head1=head1->next;
}
while(head2)
{
Newp=(Node)malloc(sizeof(struct node));
Newp->cofe=head2->cofe;
Newp->expn=head2->expn;
Newp->next=NULL;
rear=Findrear(List);
rear->next=Newp;
rear=Newp;
head2=head2->next;
}
return List;
}
int insert(Node List,Node Newp,int cofe,int expn)
{
Node rear;
Newp->cofe= cofe;
Newp->expn=expn;
while(List->next)
{
if(expn==List->next->expn){
if(cofe+(List->next->cofe)==0){
List->next=List->next->next;
}
else {
List->next->cofe=cofe+List->next->cofe;
}
return 1;
}
else if(expn>List->next->expn){//比它大就插在它前面。
Newp->next=List->next;
List->next=Newp;
return 1;
}
List=List->next;
}
rear=Findrear(List);
rear->next=Newp;
rear=Newp;
return 1;
}
Node Mult(Node head1,Node head2)//比指针指向的()大的就插入,小的就指针后移。
{
Node List,Newp,rear,p1=head1,p2=head2;
List=(Node)malloc(sizeof(struct node));
List->next=NULL; ///新链表
head1=head1->next; ///指针往后移动一位
head2=head2->next;
int cofe,expn;
while(head1)
{
head2=p2->next;
while(head2)
{
Newp=(Node)malloc(sizeof(struct node));//现建一个新结点,
Newp->next=NULL;
cofe=(head2->cofe)*(head1->cofe);
expn=head2->expn+head1->expn;
if(List->next==NULL)
{
Newp->cofe= cofe;
Newp->expn=expn;
List->next=Newp;
Newp->next=NULL;
}
else {
insert(List,Newp,cofe,expn);
}
head2=head2->next;
}
head1=head1->next;
}
return List;
}
int main()
{
Node head1,rear1,head2,rear2,MixList,MultList;
head1=Creatlist();
rear1=Findrear(head1);
head2=Creatlist();
rear2=Findrear(head2);
int n,m,i,cofe,expn;//n,m分别是项的个数
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&cofe,&expn);
rear1=Findrear(head1);
AddNode(rear1,cofe,expn);
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d %d",&cofe,&expn);
rear2=Findrear(head2);
AddNode(rear2,cofe,expn);
}
MixList=Mix(head1,head2);
MultList=Mult(head1,head2);
print(MultList);
print(MixList);
}