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 int ElementType;
typedef struct PolyNode *Polynomial;
struct PolyNode {
	ElementType coef;
	ElementType expon;
	Polynomial link;
};

Polynomial ReadPoly();
Polynomial MultPoly(Polynomial P1, Polynomial P2);
Polynomial AddPoly(Polynomial P1, Polynomial P2);
void Attach(ElementType c, ElementType e, Polynomial *pRear);
void PrintPoly(Polynomial P);

int main()
{
	Polynomial L1, L2, LM, LA;
	L1 = ReadPoly();
	L2 = ReadPoly();
	LM = MultPoly(L1, L2);
	PrintPoly(LM);
	LA = AddPoly(L1, L2);
	PrintPoly(LA);
	system("pause");
	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(ElementType c, ElementType 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;
}

Polynomial MultPoly(Polynomial P1, Polynomial P2)
{
	Polynomial t1, t2, t, P, Rear;
	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)                // 构建第一个链
	{
		Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
		t2 = t2->link;
	}
	t1 = t1->link;
	while (t1)                // 循环插入
	{
		t2 = P2;
		Rear = P;
		while (t2)
		{
			c = t1->coef*t2->coef;
			e = t1->expon+t2->expon;
			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 AddPoly(Polynomial P1, Polynomial P2)
{
	Polynomial t1, t2, P, Rear, t;
	t1 = P1;
	t2 = P2;
	 
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;
	while (t1&&t2)
	{
 		if (t1->expon > t2->expon)
		{
			Attach(t1->coef, t1->expon, &Rear);
			t1 = t1->link;
		}
		else if (t1->expon < t2->expon)
		{
			Attach(t2->coef, t2->expon, &Rear);
			t2 = t2->link;
		}
		else
		{
			if (t1->coef + t2->coef)
				Attach(t1->coef + t2->coef, t1->expon, &Rear);
			t1 = t1->link;
			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;
	}

	t = P;
	P = P->link;
	free(t);
	return P;
}

void PrintPoly(Polynomial P)
{
	int flag = 0;

	if (!P)
		printf("%d %d", 0, 0);
	
	while (P)
	{
		if (!flag)
		{
			flag = 1;
			printf("%d %d", P->coef, P->expon);
		}
		else
		{
			printf(" %d %d", P->coef, P->expon);
		}
		P = P->link;
	}
	printf("\n");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值