5-1 一元多项式的乘法与加法运算 (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<stdio.h>
#include<stdlib.h>
//带有头结点
//乘法,加法存入链表
typedef struct Node{
int Coefficient;
int Exponent;
struct Node* Next;
}Node,*Pnode;
typedef Pnode List;
void Insert(int Coefficient,int Exponent,Pnode Position);
Pnode Head();
void Entering(List head);
int Is_Empty(List head);
void MultPolynomial(const List head1,const List head2,List head3);
int Find(int Exponent,List head,Pnode* Positon);
void Input(List head);
void Delete(List head);
void AddPolynomial(const List head1,const List head2,List head3);
void Delete_node(List head);
int main(){
List head1 = NULL;
head1 = Head();
List head2 = NULL;
head2 = Head();
Entering(head1);
Entering(head2);
//检验为空
int t1,t2;
t1=Is_Empty(head1);
t2=Is_Empty(head2);
if(t1==0&&t2==0){
printf("0 0\n0 0\n");
return 0;
}
Delete_node(head1);
Delete_node(head2);
//多项式乘法
List head3 = NULL;
head3 = Head();
MultPolynomial(head1,head2,head3);
Delete_node(head3);
Input(head3);
Delete(head3);
//多项式加法
List head4 = NULL;
head4 = Head();
AddPolynomial(head1,head2,head4);
Delete_node(head4);
Input(head4);
Delete(head1);
Delete(head2);
Delete(head4);
return 0;
}
//尾插
void Insert(int Coefficient,int Exponent,Pnode Position){
Pnode p = (Pnode)malloc(sizeof(Node));
if(p==NULL){
printf("out of space!\n");
exit(0);
}
p->Coefficient = Coefficient;
p->Exponent = Exponent;
p->Next = Position->Next;
Position->Next = p;
}
//头结点
Pnode Head(){
Pnode p = (Pnode)malloc(sizeof(Node));
if(p==NULL){
printf("out of space!\n");
exit(0);
}
p->Coefficient = 0;
p->Exponent = 0;
p->Next = NULL;
return p;
}
//录入多项式
void Entering(List head){
int n;
scanf("%d",&n);
while(n--){
int Coefficient;
int Exponent;
scanf("%d%d",&Coefficient,&Exponent);
Insert(Coefficient,Exponent,head);
head = head->Next;
}
}
//是否为空
int Is_Empty(List head){
if(head->Next==NULL){
return 0;
}
else{
return 1;
}
}
//多项式乘法
void MultPolynomial( List head1, List head2,List head3){
int temp1,temp2;
temp1=Is_Empty(head1);
temp2=Is_Empty(head2);
if(temp1==0||temp2==0){
return ;
}
for(List thead1=head1;thead1->Next!=NULL;thead1=thead1->Next){
for(List thead=head2;thead->Next!=NULL;thead=thead->Next){
int Coefficient = thead1->Next->Coefficient*thead->Next->Coefficient;
int Exponent = thead1->Next->Exponent+thead->Next->Exponent;
Pnode Position;
int status;
status = Find(Exponent,head3,&Position);
if(status==1){
Position->Coefficient+=Coefficient;
}
else{
Insert(Coefficient,Exponent,Position);
}
}
}
}
//以指数来查找(若存在则返回1,不存在返回0,存在Position保存该节点,不存在保存需要插入的节点)
int Find(int Exponent,List head,Pnode* Position){
if(head->Next==NULL){
*Position = head;
return 0;
}
for(;head->Next!=NULL;head=head->Next){
if(Exponent>head->Next->Exponent){
*Position = head->Next;
return 0;
}
if(head->Next->Exponent == Exponent){
*Position = head->Next;
return 1;
}
if(head->Next->Next!=NULL){
if(head->Next->Exponent>Exponent&&head->Next->Next->Exponent<Exponent){
*Position = head->Next;
return 0;
}
}
if(head->Next->Next==NULL){
if(head->Exponent>Exponent&&head->Next->Exponent<Exponent){
*Position = head;
return 0;
}
}
}
*Position = head;
return 0;
}
//输出
void Input(List head){
if(Is_Empty(head)==0){
printf("0 0\n");
return ;
}
else{
for(;head->Next!=NULL;head=head->Next){
if(head->Next->Next!=NULL){
printf("%d %d ",head->Next->Coefficient,head->Next->Exponent);
}
else{
printf("%d %d\n",head->Next->Coefficient,head->Next->Exponent);
}
}
}
}
//释放链表
void Delete(List head){
List temp = head->Next;
for(;temp!=NULL;head=head->Next,temp=temp->Next){
free(head);
}
}
//删除系数为0的节点
void Delete_node(List head){
for(;head->Next!=NULL;head=head->Next){
Pnode temp;
if(head->Next->Coefficient==0){
temp=head->Next;
head->Next=head->Next->Next;
free(temp);
}
if(head->Next==NULL){
break;
}
}
}
//加法
void AddPolynomial(const List head1,const List head2,List head3){
List thead1 = head1->Next;
List thead2 = head2->Next;
int Coefficient;
int Exponent;
Pnode Position = head3;
while(thead1!=NULL&&thead2!=NULL){
while(thead1!=NULL&&thead2!=NULL&&thead1->Exponent>thead2->Exponent){
Coefficient = thead1->Coefficient;
Exponent = thead1->Exponent;
Insert(Coefficient,Exponent,Position);
Position = Position->Next;
thead1=thead1->Next;
}
while(thead1!=NULL&&thead2!=NULL&&thead2->Exponent>thead1->Exponent){
Coefficient = thead2->Coefficient;
Exponent = thead2->Exponent;
Insert(Coefficient,Exponent,Position);
Position = Position->Next;
thead2=thead2->Next;
}
if(thead1!=NULL&&thead2!=NULL&&thead1->Exponent==thead2->Exponent){
Coefficient = thead2->Coefficient+thead1->Coefficient;
Exponent = thead1->Exponent;
Insert(Coefficient,Exponent,Position);
Position = Position->Next;
thead1=thead1->Next;
thead2=thead2->Next;
}
}
if(thead1==NULL){
if(thead2!=NULL){
while(thead2->Next!=NULL){
Coefficient = thead2->Coefficient;
Exponent = thead2->Exponent;
Insert(Coefficient,Exponent,Position);
Position = Position->Next;
thead2=thead2->Next;
}
Coefficient = thead2->Coefficient;
Exponent = thead2->Exponent;
Insert(Coefficient,Exponent,Position);
}
}
if(thead2==NULL){
if(thead1!=NULL){
while(thead1->Next!=NULL){
Coefficient = thead1->Coefficient;
Exponent = thead1->Exponent;
Insert(Coefficient,Exponent,Position);
Position = Position->Next;
thead1=thead1->Next;
}
Coefficient = thead1->Coefficient;
Exponent = thead1->Exponent;
Insert(Coefficient,Exponent,Position);
}
}
}