线性结构: 一元多项式的乘法与加法运算

该博客介绍了如何设计函数来计算两个一元多项式的乘积和和。输入包括两个非零项个数及各自系数和指数,输出则按照指数递减排列。示例展示了具体的计算过程。
摘要由CSDN通过智能技术生成

设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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 struct PolyNode *Polynomial;
typedef struct PolyNode{//结构体:系数,指数,指针
	int coef;
	int expon;
	Polynomial link;
};

void Attach(int c, int e, Polynomial *pRear);
Polynomial ReadPoly();
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;
	                                                         
}
void Attach(int c, int e,Polynomial *pRear){//连接结点
	Polynomial P;
	P = (Polynomial)malloc(sizeof(PolyNode));
	P->coef = c;
	P->expon = e;
	P->link = (*pRear)->link;
	(*pRear)->link = P;
	(*pRear) = P;
}
Polynomial ReadPoly(){          //建立链表
	Polynomial P, Rear, First;
	int n, c, e;
	P = (Polynomial)malloc(sizeof(PolyNode));
	P->link = NULL;
	Rear = P;
	scanf("%d", &n);
	while (n--){
		scanf("%d %d", &c, &e);
		Attach(c, e, &Rear);
	}
	First = P;
	P = P->link;
	free(First);
	return P;
}
Polynomial Add(Polynomial P1, Polynomial P2){//做加法
	Polynomial temp, temp1, temp2, P;
	temp1 = P1;
	temp2 = P2;
	P = (Polynomial)malloc(sizeof(PolyNode));
	P->link = NULL;
	temp = P;
	while (temp1&&temp2){
		if (temp1->expon > temp2->expon){
			Attach(temp1->coef, temp1->expon, &temp);
			temp1 = temp1->link;
		}
		else if (temp1->expon < temp2->expon){
			Attach(temp2->coef, temp2->expon, &temp);
			temp2 = temp2->link;
		}
		else {
			if (!(temp1->coef + temp2->coef)== 0){
				Attach(temp1->coef + temp2->coef, temp1->expon, &temp);
				temp1 = temp1->link;
				temp2 = temp2->link;
			}
			else {
				temp1 = temp1->link;
				temp2 = temp2->link;
			}
		}
	}
	for (; temp1; temp1 = temp1->link){
		Attach(temp1->coef, temp->expon, &temp);

	}
	for (; temp2; temp2 = temp2->link){
		Attach(temp2->coef, temp->expon, &temp);
	}
	temp = P;
	P = P->link;
	free(temp);
	return P;
}
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(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){
			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 {
					Rear->link->coef = 0;
					Rear->link->expon = 0;
				}
			}
			else {
				t = (Polynomial)malloc(sizeof(PolyNode));
				t->coef = c;
				t->expon = e;
				t->link = Rear->link;
				Rear->link = t;
				Rear = Rear->link;
			}
			t2 = t2->link;
		}
		t1 = t1->link;
	}
	Rear = P;
	P = P->link;
	free(Rear);
	return P;
}
void PrintPoly(Polynomial P){//输出链表
	int flag = 0;
	if (!P){
		printf("0 0\n"); 
	}
	while (P){
		if (!flag){
			flag = 1;
		}
		else {
			printf(" ");
		}
		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、付费专栏及课程。

余额充值