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<stdio.h>
#include<stdlib.h>
typedef struct node{
	int c;
	int e;
	struct node *next;
}PolyNode;
typedef PolyNode *Polynomial;

void Attach(int c,int e,Polynomial *ptail);
Polynomial Read();
Polynomial Add(Polynomial L1,Polynomial L2);
Polynomial Mul(Polynomial L1,Polynomial L2);
void Print(Polynomial L);

int main()
{
	Polynomial L1,L2,L3,L4;
	L1=Read();
	L2=Read();
	L3=Mul(L1,L2);
	Print(L3);
	L4=Add(L1,L2);
	Print(L4);
	return 0;
 } 
 void Attach(int c,int e,Polynomial *ptail){
 	Polynomial p=(Polynomial)malloc(sizeof(PolyNode));
 	p->c=c;
 	p->e=e;
 	p->next=NULL;
 	(*ptail)->next=p;
 	(*ptail)=p;
 }	
 Polynomial Read(){
 	int n,i,c,e;
 	Polynomial head,tail,p;
 	head=tail=(Polynomial)malloc(sizeof(PolyNode));
 	tail->next=NULL;
 	scanf("%d",&n);
 	for(i=0;i<n;i++){
 		scanf("%d %d",&c,&e);
		Attach(c,e,&tail);	
	}
	//free头结点 
 	p=head;
 	head=head->next;
 	free(p);
 	return head;
 }
 Polynomial Add(Polynomial L1,Polynomial L2){
 	Polynomial head,tail,head1,head2,p;
 	head1=L1;head2=L2;
 	
 	head=tail=(Polynomial)malloc(sizeof(PolyNode));
 	tail->next=NULL;
 	
 	while(head1&&head2){
 		if(head1->e>head2->e){
 			Attach(head1->c,head1->e,&tail);
 			head1=head1->next; 
		 }else if(head1->e<head2->e){
		 	Attach(head2->c,head2->e,&tail);
		 	head2=head2->next;
		 }else {
		 	if(head1->c+head2->c) Attach(head1->c+head2->c,head1->e,&tail);
			head1=head1->next;
		 	head2=head2->next;
		 }
	}
	while(head1){
	 	Attach(head1->c,head1->e,&tail);
	 	head1=head1->next;
	}
	while(head2){
	 	Attach(head2->c,head2->e,&tail);
	 	head2=head2->next;
	}
 	p=head;
 	head=head->next;
 	free(p);
 	return head;
 } 
 
 
 
Polynomial Mul(Polynomial L1,Polynomial L2){
 	Polynomial head,tail,head1,head2,p;
 	int c,e;
 	
 	if(L1==NULL||L2==NULL) return NULL;
 	
 	head1=L1;head2=L2;
 	
 	head=tail=(Polynomial)malloc(sizeof(PolyNode));
 	tail->next=NULL;	
 	
 	while(head2){//初始多项式由L1的第一项乘以L2的每一项得到 
 		Attach(head1->c*head2->c,head1->e+head2->e,&tail);
 		head2=head2->next;
	}
	
	head1=head1->next;
	
	while(head1){
		//每次循环都要让head2指针返回第二个多项式的第一项 
		head2=L2;
		
		//然后让尾指针返回结果多项式链表的头结点,以便将新的结点插入结果多项式链表 
		tail=head;
		while(head2){
			c=head1->c*head2->c;
			e=head1->e+head2->e;
			
			while(tail->next&&tail->next->e>e){
				tail=tail->next;
			} 
			if(tail->next&&tail->next->e==e){
				if(tail->next->c+c!=0){
					tail->next->c+=c;
				}else{
					p=tail->next;
					tail->next=p->next;
					free(p);
				}
			}else{
				p=(Polynomial)malloc(sizeof(PolyNode));
				p->c=c;
				p->e=e;
				p->next=tail->next;
				tail->next=p;
				tail=tail->next;
			} 
			head2=head2->next;
		}
		head1=head1->next;
	}
	
	p=head;
	head=head->next;
	free(p);
	return head;
 }
 void Print(Polynomial L){
 	Polynomial p=L;
 	if(!p) printf("0 0");
 	while(p){
 		printf("%d %d",p->c,p->e);
 		if(p->next) printf(" ");
 		p=p->next;
	 }
	 printf("\n");
 }
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值