数据结构—— 一元多项式的乘法与加法运算

设计函数分别求两个一元多项式的乘积与和。

输入格式:
输入分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

C语言代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode *Polynomial;

struct PolyNode {
    
    
	int coef;
	int expon;
	Polynomial link;
};

Polynomial ReadPoly();
void Attach( int c, int e, Polynomial *pRear );
Polynomial Add( Polynomial P1, Polynomial P2 );
Polynomial Mult( Polynomial P1, Polynomial P2 );
void PrintPoly( Polynomial P );

int main()
{
    
    
	Polynomial P1, P2, PP, PS;
	P1 = ReadPoly();
	P2 = ReadPoly();
	PP = Mult( P1, P2 );
	PrintPoly( PP );
	PS = Add( P1, P2 );
	PrintPoly( PS );
	return 0;
}

Polynomial ReadPoly()
{
    
    
	Polynomial P, Rear, t;
	int c, e, N;
	scanf("%d", &N);
	P=(Polynomial)malloc(sizeof(struct PolyNode)); /* 链表头空结点 */
	P->link=NULL;
	Rear=P;
	while(N--) 
	{
    
    
		scanf("%d %d", &c, &e);
		Attach(c, e, &Rear); /* 将当前项插入多项式尾部 */
	}
	t = P;
	P = P->link;
	free(t); /* 删除临时生成的头结点 */
	
	return P;
}

void Attach( int c, int e, Polynomial *pRear )
{
    
    
	Polynomial P;
	P=(Polynomial)malloc(sizeof(struct PolyNode)); 
	P->coef = c;       /* 对新结点赋值 */ 
	P->expon = e;
	P->link = NULL;
	(*pRear)->link=P; 
	*pRear = P;       /* 修改pRear值 */ 
}

Polynomial Mult( Polynomial P1, Polynomial P2 )
{
    
     
	Polynomial P,Rear,t1,t2,t;
	int c, e;
	if (!P1 || !P2) 
		return NULL;
	t1 = P1; t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;
	while (t2)      /* 先用P1的第1项乘以P2,得到P */
	{
    
    
		Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
		t2 = t2->link;
	}
	t1 = t1->link;
	while (t1) 
	{
    
    
		t2 = P2; Rear = P;
		while (t2) 
		{
    
    
			e = t1->expon + t2->expon;
			c = t1->coef * t2->coef;
			while (Rear->link&&Rear->link->expon>e)
				Rear = Rear->link;
			if (Rear->link && Rear->link->expon == e) 
			{
    
    
				if (Rear->link->coef+c)
					Rear->link->coef+= c;
				else 
				{
    
    
					t = Rear->link;
					Rear->link = t->link;
					free(t);
				}
			}
			else 
			{
    
    
				t = (Polynomial)malloc(sizeof(struct PolyNode));
				t->coef = c;
				t->expon = e;
				t->link = Rear->link;
				Rear->link = t; 
				Rear = Rear->link;
			}
			t2 = t2->link;
		}
		t1 = t1->link;
	}
	t2 = P; 
	P = P->link; 
	free(t2);
	
	return P;
}

Polynomial Add( Polynomial P1, Polynomial P2 )
{
    
    
	Polynomial P,Rear,temp,t1,t2;
	t1 = P1; t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode)); 
	P->link = NULL;
	Rear = P;
	while (t1 && t2) 
	{
    
    
		if (t1->expon == t2->expon) 
		{
    
    
			if(t1->coef+t2->coef)
				Attach(t1->coef+t2->coef,t1->expon,&Rear);
			t1=t1->link;
			t2=t2->link;
		}
		else if (t1->expon > t2->expon) 
		{
    
    
			Attach(t1->coef,t1->expon,&Rear);
			t1=t1->link;
		}
		else 
		{
    
    
			Attach(t2->coef,t2->expon,&Rear);
			t2=t2->link;
		} 
	}
	while (t1) 
	{
    
    
		Attach(t1->coef,t1->expon,&Rear);
		t1=t1->link;
	}
	while (t2) 
	{
    
    
		Attach(t2->coef,t2->expon,&Rear);
		t2=t2->link;
	}
	Rear->link=NULL;
	temp=P;
	P=P->link;
	free(temp);
	
	return P;
}

void PrintPoly( Polynomial P )             /* 输出多项式 */
{
    
    
	int flag = 0;          /* 辅助调整输出格式用 */
	if (!P) 
	{
    
    
		printf("0 0\n"); 
		return;
	}
	while ( P ) 
	{
    
    
		if (!flag)
			flag = 1;
		else
			printf(" ");
		printf("%d %d", P->coef, P->expon);
		P = P->link;
	}
	
	printf("\n");
}

分析
如何将两个多项式相乘

1.将乘法运算转换为加法运算
将P1的首项依次与P2多项式各项相乘,将结果存入新建的链表中,此时链表中的各项按指数从高到低排列

2.逐项插入
将P1的其余项乘P2的各项,得到其余项,将其插入到新建的链表中。注意得到的项的指数可能与原先的项的指数相同,此时要考虑系数和是否为0,如果为0,要删除一个节点。

猜你喜欢

转载自blog.csdn.net/weixin_46155777/article/details/107582881
今日推荐