2021-08-10

一元多项式的乘法与加法运算 (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

本题解题思路:
1:由于本题输入是以大小递减的方式输入,且输出也是按照递减的方式输出,所以相当于是按照顺序来的,所以我们可以用单链表的方式进行解决。
2:无论是加法还是乘法都涉及合并同类项,所以在设计函数的时候要把这些情况考虑进去。

函数


#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    long int exponent;//指数 
	long int coefficient;//系数 
	struct node *next;
}linklist,*arr;
//带头节点的链表 
//尾插法 
arr read(arr p,int n){
	arr h1,h2; 
	int i,a,b;
	p=(arr)malloc(sizeof(linklist));
	p->next=NULL;
	h1=p;
	if(n==0){
		h2=(arr)malloc(sizeof(linklist));
		h2->next=NULL;
		h2->coefficient=0;
		h2->exponent=0;
		h1->next=h2;
	}
	else{
		for(i=0;i<n;i++){
			h2=(arr)malloc(sizeof(linklist));
			h2->next=NULL;
			h1->next=h2;
			h1=h2; 
			scanf("%d%d",&a,&b);
			h1->coefficient=a;
			h1->exponent=b;
		}
	}
    return p;
}
//第一种乘法运算
arr muld(arr p1,arr p2,int n,int m ){
	arr t1,t2,h1,h2,t,key;
	int a,b;
	//
	h1=p1->next;
	h2=p2->next;
	//
	t=(arr)malloc(sizeof(linklist));//新的链表
	t->next=NULL;
	t1=t;
	int i,j;
	for(i=0;i<n;i++){
		h2=p2->next;
		for(j=0;j<m;j++){
			a=h1->coefficient*h2->coefficient;
			b=h1->exponent+h2->exponent;
			t2=(arr)malloc(sizeof(linklist));
			t2->next=NULL;
			t1->next=t2;
			t2->coefficient=a;
			t2->exponent=b;
			t1=t1->next;
			h2=h2->next;
		}
		h1=h1->next;
	}
	//对于指数项相同的项,系数相加
	//同类项合并
	for(t1=t;t1!=NULL;t1=t1->next){
		for(t2=t1->next,key=t1;t2!=NULL;){
			if(t1->exponent==t2->exponent){
				t1->coefficient=t2->coefficient+t1->coefficient;
				key->next=t2->next;
				free(t2);
				t2=key->next;
			}
			else{
				key=t2;
				t2=key->next;
			}
			
		}
	}
	return t;
}
 //加法运算 
 arr add(arr L1,arr L2) {
 	int temp;
 	arr ptr1,ptr2,tail,ptr,key,head=NULL;
 	//首先是链表的合并 
 	for(ptr=L1->next;ptr!=NULL;ptr=ptr->next){
 		ptr1=(arr)malloc(sizeof(linklist));
 		ptr1->next=NULL;
 		ptr1->coefficient=ptr->coefficient;
 		ptr1->exponent=ptr->exponent;
 		if(head==NULL)
 		head=ptr1;
 		else
 		tail->next=ptr1;
 		tail=ptr1;
	 }
	 for(ptr=L2->next;ptr!=NULL;ptr=ptr->next){
	 	ptr2=(arr)malloc(sizeof(linklist));
	 	ptr2->next=NULL;
	 	ptr2->coefficient=ptr->coefficient;
	 	ptr2->exponent=ptr->exponent;
	 	if(head==NULL)
	 	head=ptr2;
	 	else
	 	tail->next=ptr2;
	 	tail=ptr2;
	 	
	 }
	 //合并同类项  head是头
	 for(ptr1=head;ptr1!=NULL;ptr1=ptr1->next){
	 	for(ptr2=ptr1->next,key=ptr1;ptr2!=NULL;){
	 		if(ptr1->exponent==ptr2->exponent){
	 			ptr1->coefficient=ptr1->coefficient+ptr2->coefficient;
	 			key->next=ptr2->next;
	 			free(ptr2);
	 			ptr2=key->next;//不要忘了 
			 }
			 else{
			 	key=ptr2;
			 	ptr2=key->next;
			 }
		 }
	 } 
	 return head;
	  
 }
//选择排序
arr paixu(arr head){
	int temp;
	arr ptr1=NULL,ptr2=NULL,ptr=NULL;
	 for(ptr1=head;ptr1->next!=NULL;ptr1=ptr1->next)
    {
    	for(ptr2=ptr1->next;ptr2!=NULL;ptr2=ptr2->next)
    	{
    		if(ptr1->exponent<ptr2->exponent)
    		{
    			temp=ptr2->exponent;
    			ptr2->exponent=ptr1->exponent;
    			ptr1->exponent=temp;
    		    temp=ptr2->coefficient;
    		    ptr2->coefficient=ptr1->coefficient;
    		    ptr1->coefficient=temp;
			} 
		}
	}
	for(ptr=head;ptr!=NULL;)
	{
		if(ptr->coefficient==0)
		{
			if(ptr==head)
			{
				head=head->next;
				free(ptr);
				ptr=head; 
			}
			else 
			{
				ptr1->next=ptr->next;
				free(ptr);
				ptr=ptr1->next;
			}
		}
		else
		{
		 ptr1=ptr;
		 ptr=ptr->next;
		}
	}
    return head;
}


int main() {
	int m,n;
	arr p1,p2,pa,pb,f,head,head1,head2,ptr;
	
	scanf("%d",&n);
	pa=read(p1,n);
	scanf("%d",&m);
	pb=read(p2,m);
	f=muld(pa,pb,n,m);
	head=paixu(f);
	head1=add(pa,pb);
	head2=paixu(head1);
    if(head!=NULL)
    {
    	for(ptr=head;ptr!=NULL;ptr=ptr->next)
    	{
    		printf("%d %d",ptr->coefficient,ptr->exponent);
    		if(ptr->next!=NULL) printf(" "); 
		}
	}
	else printf("0 0"); 
	printf("\n"); 
	if(head2!=NULL)
    {
    	for(ptr=head2;ptr!=NULL;ptr=ptr->next)
    	{
    		printf("%d %d",ptr->coefficient,ptr->exponent);
    		if(ptr->next!=NULL) printf(" "); 
		}
	}
	else printf("0 0"); 
	return 0;
}

总结
加法和乘法采用不同的链表创建方式,乘法运算边运算边创建新链表,最后再合并同类项;加法是先合并两个链表,在对其中系数相同的进行加法运算,同时完成合并同类项。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值