单链表实现:一元多项式的乘法与加法运算

一元多项式的乘法与加法运算 

(PTA | 程序设计类实验辅助教学平台  https://pintia.cn/problem-sets/15/problems/710)

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

输入格式:

输入分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 plain copy
#include 
#include 
#include

typedef struct PolyNode* Polynomial;
struct PolyNode {
	int coef;   //系数 
	int expon; //指数 
	Polynomial next;   //指向下一个结点的指针 
};

void attach(int c, int e, Polynomial* pRear);
int compare(int num1, int num2);
Polynomial polyAdd(Polynomial p1, Polynomial p2);
Polynomial polymultiply(Polynomial p1, Polynomial p2,int num);//将p1的第一项与p2的所有项做乘法并返回

int main(void)
{
	Polynomial p1, p2, p1_rear, p2_rear, p_temp;
	p1 = (Polynomial)malloc(sizeof(struct PolyNode));	//初始化头结点 
	p2 = (Polynomial)malloc(sizeof(struct PolyNode));	//初始化头结点

	p1_rear = p1;	//初始化尾指针 
	p2_rear = p2;	//初始化尾指针 
	int num1,num2, c, e;

	scanf("%d", &num1);	//读取p1信息 
	for (int i = 0;i < num1;i++)
	{
		scanf("%d %d", &c, &e);
		attach(c, e, &p1_rear);
	}
	p_temp = p1;
	p1 = p1->next;
	free(p_temp);	//释放头结点 

	scanf("%d", &num2);	//读取p2信息 
	for (int i = 0;i < num2;i++)
	{
		scanf("%d %d", &c, &e);
		attach(c, e, &p2_rear);
	}
	p_temp = p2;
	p2 = p2->next;
	free(p_temp);	//释放头结点 

	//乘法,具体算法是用p1的每一项都和p2的所有项乘,再将所得的加在一起,加法已实现
	Polynomial doMultiplication = p1;	//做乘法
	Polynomial multiplication = NULL;  //记录乘法结果
	for (int i = 0;i < num1;i++)
	{
		multiplication = polyAdd( polymultiply(doMultiplication, p2, num2) , multiplication);
		doMultiplication = doMultiplication->next;
	}

	if (multiplication)	//处理第一项
	{
		printf("%d %d", multiplication->coef, multiplication->expon);
		multiplication = multiplication->next;
	}
	else
	{
		printf("0 0");
	}

	while (multiplication)
	{
		printf(" %d %d", multiplication->coef, multiplication->expon);
		multiplication = multiplication->next;
	}


	printf("\n");

	//加法
	Polynomial sum = polyAdd(p1, p2);
	if (sum)	//处理第一项 
	{
		printf("%d %d", sum->coef, sum->expon);
		sum = sum->next;
	}
	else
	{
		printf("0 0");
	}

	for (;sum;sum = sum->next)	//处理之后项 
	{
		printf(" %d %d", sum->coef, sum->expon);
	}
	return 0;
}

void attach(int c, int e, Polynomial* pRear)	//在尾指针后连上另一项 
{
	Polynomial p = (Polynomial)malloc(sizeof(struct PolyNode));
	p->coef = c;
	p->expon = e;
	p->next = NULL;
	(*pRear)->next = p;
	*pRear = p;
}

int compare(int num1, int num2)	//比较大小 
{
	if (num1 > num2)
		return 1;
	else if (num1 < num2)
		return -1;
	else
		return 0;
}

Polynomial polyAdd(Polynomial p1, Polynomial p2)	//多项式加法 
{
	Polynomial front, rear, temp;
	int sum;
	front = (Polynomial)malloc(sizeof(struct PolyNode));
	rear = front;//由front记录结果多项式链表头结点
	while (p1 && p2)	//当两个多项式都有非零项待处理时 
	{
		switch (compare(p1->expon, p2->expon)) {
		case 1:
			attach(p1->coef, p1->expon, &rear);
			p1 = p1->next;
			break;
		case -1:
			attach(p2->coef, p2->expon, &rear);
			p2 = p2->next;
			break;
		case 0:
			sum = p1->coef + p2->coef;
			if (sum)
				attach(sum, p1->expon, &rear);
			p1 = p1->next;
			p2 = p2->next;
			break;
		}
	}
	//将未处理完的另一个多项式的所有结点复制到结果多项式中
	for (;p1;p1 = p1->next)
		attach(p1->coef, p1->expon, &rear);
	for (;p2;p2 = p2->next)
		attach(p2->coef, p2->expon, &rear);
	rear->next = NULL;
	temp = front;
	front = front->next;//令front指向结果多项式第一个非零项
	free(temp);//释放临时空表头结点
	return front;

}

Polynomial polymultiply(Polynomial p1, Polynomial p2,int num)	//p1的一项与p2所有项做乘法
{
	Polynomial front = (Polynomial)malloc(sizeof(struct PolyNode));
	Polynomial rear = front;
	Polynomial temp = front;
	for (int i = 0;i < num;i++)
	{
		attach(p1->coef * p2->coef, p1->expon + p2->expon, &rear);
		p2 = p2->next;
	}
	front = front->next;
	free(temp);
	return front;
}


你可能感兴趣的:(高级语言程序设计)