一元多项式的加法和乘法(浙大)

题目来源:
题目连接

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

输入格式:

输入分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>
/*
多项式加法:
采用不带头结点的单向链表,按照指数递减的顺序排列各项
思路:p1,p2分别指向两个多项式的第一个结点, 不断循环
if p1->expon==p2->exoon:系数相加,如果结果不为零,则 作为结果对应项的系数。移动指针
if p1->expon>p2->expon:将p1的当前项存入使p1移动。
最后再接剩余项
else 反之
*/
struct PolyNode {
	int coef;//系数
	int expon;//指数
	struct PolyNode* link;//指向下一个结点的指针
};

typedef struct PolyNode* Poloynomial;
Poloynomial P1, P2;

Poloynomial ReadPoly()
{
	Poloynomial P, Rear, t;
	int num,c,e;
	scanf("%d", &num);
	P = (Poloynomial)malloc(sizeof(struct PolyNode));/*链表头空结点*/
	P->link = NULL;
	Rear = P;
	while (num--)
	{
		scanf("%d", &c, &e);
		Attach(c, e, &Rear);
	}
	t = P; P = P->link; free(t);//把空结点去掉,返回一个非空结点
	return P;
}


int Compare(int p1, int p2)
{
	if (p1 > p2)
		return 1;
	else if (p1 < p2)
		return -1;
	else
		return 0;
}

void Attach(int p1, int p2, Poloynomial *rear)
{
	Poloynomial temp;
	temp = (Poloynomial)malloc(sizeof(struct PolyNode));
	temp->coef = p1;
	temp->expon = p2;
	temp->link = NULL;
	(*rear)->link = temp;
	*rear = temp;
}

Poloynomial PolyAdd(Poloynomial P1, Poloynomial P2)
{
	Poloynomial front, rear, temp;
	int sum;
	rear = (Poloynomial)malloc(sizeof(struct PolyNode));
	front = rear;/*有front记录结果多项式链表头结点*/
	while (P1 && P2)
	{
		switch (Compare(P1->expon,P2->expon))
		{
		case 1:
			Attach(P1->coef, P1->expon,&rear);//传入为指针所在的地址
			P1 = P1->link;
			break;
		case -1:
			Attach(P2->coef, P2->expon, &rear);
			P2=P2->link;
			break;
		case 0:
			sum = P1->coef + P2->coef;
			if (sum) Attach(sum, P1->expon, &rear);
			P1 = P1->link;
			P2 = P2->link;
			break;		
		}
	}
	/*将未处理完的另一个多项式的所以结点依次复制到结果多项式中去*/
	for (; P1; P1 = P1->link) Attach(P1->coef, P1->expon, &rear);
	for (; P2; P2 = P2->link) Attach(P2->coef, P2->expon, &rear);
	rear->link = NULL;
	temp = front;
	front = front->link;/*令front指向结果多项式的非零项*/
	free(temp);
	return front;

}

/*一元多项式的乘积*/
Poloynomial PolyMult(Poloynomial P1, Poloynomial P2)
{
	Poloynomial P, Rear, t1, t2, t;
	int c, e;

	if (!P1 || !P2) return NULL;

	t1 = P1; t2 = P2;
	P = (Poloynomial)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 = t2->link;
	while (t1)
	{/*t1的每一项*t2的每一项*/
		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 = (Poloynomial)malloc(sizeof(struct PolyNode));
				t->coef = c; t->expon = 2;
				t->link = Rear->link;
				Rear->link = t; Rear = Rear->link;
			}

			
		}

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


}

void PrintPoly(Poloynomial 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;
			
	}

}


int main(void)
{

	Poloynomial P1, P2, PP, PS;
	
	
	/*读入多项式1*/
	P1 = ReadPoly();
	/*读入多项式2*/
	P2 = ReadPoly();
	/*乘法运算输出*/
	PP = PolyMult(P1, P2);
	PrintPoly(PP);
	
	
	/*加法运算输出*/
	PS = PolyAdd(P1, P2);
	PrintPoly(PS);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值