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

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

输入格式:

输入分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>
/*使用数组实现*/
#define MAX_SIZE 2000 //最高指数
typedef int Position;

typedef struct PloyNode {//多项式中每一项的节点
	int Coeff; //系数
	int Expon; //指数
}PolyNode;

typedef struct Polynomial {
	PolyNode NodeList[MAX_SIZE + 1];
	Position Last; //最后一个有效元素的下标

}Polynomial, * PtrPolynomial;


PtrPolynomial createPoly(void); //向多项式当中添加元素
PtrPolynomial newPoly(void); //创建一个零多项式
PtrPolynomial polyAdd(PtrPolynomial poly1, PtrPolynomial poly2);
PtrPolynomial polyMult(PtrPolynomial poly1, PtrPolynomial poly2);
void showPoly(PtrPolynomial poly);
void bubbleSort(PtrPolynomial poly);
void deleteNode(PtrPolynomial poly, Position pos);//删除节点矩阵索引为pos的元素
void deleteZeroNode(PtrPolynomial poly); //删除poly中系数为0的节点
int min2(int a, int b);

int main(void) {
	PtrPolynomial poly1 = createPoly();
	PtrPolynomial poly2 = createPoly();
	PtrPolynomial sumPoly = polyAdd(poly1, poly2);
	PtrPolynomial multPoly = polyMult(poly1, poly2);
	showPoly(multPoly);
	printf("\n");
	showPoly(sumPoly);

	return 0;
}

PtrPolynomial createPoly(void) {
	PtrPolynomial poly = newPoly();
	int num; //多项式非零项的个数
	scanf("%d", &num);
	int coeff, expon;
	for (int i = 0; i < num; i++) {
		scanf("%d %d", &coeff, &expon);
		poly->NodeList[i].Coeff = coeff;
		poly->NodeList[i].Expon = expon;
	}
	poly->Last = num - 1;
	return poly;
}

PtrPolynomial newPoly(void) {
	PtrPolynomial poly = (PtrPolynomial)malloc(sizeof(Polynomial));
	if (poly == NULL)
		exit(-1);
	else {
		poly->Last = -1;
		return poly;
	}	
}

PtrPolynomial polyAdd(PtrPolynomial poly1, PtrPolynomial poly2) {
	/* 多项式的存储按照指数从小到大的顺序,
	遍历两个多项式,每次将指数较大的添加到新的多项式当中,如果指数相等,则进行加法
	当其中一个多项式遍历完成后,将未遍历完的多项式直接添加到新的多项式当中
	*/
	PtrPolynomial poly = newPoly();
	int i = 0, j = 0, k = 0;
	while (i <= poly1->Last && j <= poly2->Last) {
		if (poly1->NodeList[i].Expon > poly2->NodeList[j].Expon) {
			poly->NodeList[k] = poly1->NodeList[i];
			k++;
			poly->Last++;
			i++;
		}
		else if(poly1->NodeList[i].Expon < poly2->NodeList[j].Expon){
			poly->NodeList[k] = poly2->NodeList[j];
			k++;
			poly->Last++;
			j++;
		}
		else {
			poly->NodeList[k].Expon = poly1->NodeList[i].Expon;
			poly->NodeList[k].Coeff = poly1->NodeList[i].Coeff+ poly2->NodeList[j].Coeff;
			poly->Last++;
			k++;
			i++;
			j++;
		}
	}

	if (i == poly1->Last + 1)
		while (j <= poly2->Last) {
			poly->NodeList[k] = poly2->NodeList[j];
			poly->Last++;
			k++;
			j++;
		}
	if (j == poly2->Last + 1)
		while (i <= poly1->Last) {
			poly->NodeList[k] = poly1->NodeList[i];
			poly->Last++;
			k++;
			i++;
		}
	//删除系数为0的节点
	deleteZeroNode(poly);

	return poly;
}

PtrPolynomial polyMult(PtrPolynomial poly1, PtrPolynomial poly2) {
	//直接相乘然后再排序即可
	PtrPolynomial poly = newPoly();
	int k = 0;
	for (int i = 0; i <= poly1->Last; i++)
		for (int j = 0; j <= poly2->Last; j++) {
			poly->NodeList[k].Coeff = poly1->NodeList[i].Coeff * poly2->NodeList[j].Coeff;
			if (poly->NodeList[k].Coeff != 0)
				poly->NodeList[k].Expon = poly1->NodeList[i].Expon + poly2->NodeList[j].Expon;
			k++;
			poly->Last++;
		}
	//排序
	bubbleSort(poly);
	//合并指数相同的项
	for (int i = 0; i < poly->Last; i++)
		if (poly->NodeList[i].Expon == poly->NodeList[i + 1].Expon) {
			poly->NodeList[i].Coeff += poly->NodeList[i + 1].Coeff;
			deleteNode(poly, i + 1);
			i--;
		}
	//删除系数为0的节点
	deleteZeroNode(poly);
	return poly;
}

int min2(int a, int b) {
	return a < b ? a : b;
}

void showPoly(PtrPolynomial poly) {
	/*零多项式的输出:当poly->Last==-1,即为零多项式。指数项即使不为0也要输出0 */
	if (poly->Last == -1) {
		printf("0 0");
		return;
	}

	for (int i = 0; i <= poly->Last; i++) {
		if (poly->NodeList[i].Coeff != 0) {//系数为0不输出
			printf("%d %d", poly->NodeList[i].Coeff, poly->NodeList[i].Expon);
			if (i < poly->Last) //结尾不能有多余的空格
				printf(" ");
		}
	}
		
}
void bubbleSort(PtrPolynomial poly) {
	PolyNode tempNode;
	for (int i = 0; i < poly->Last; i++)
		for (int j = 0; j < poly->Last - i; j++) {
			if (poly->NodeList[j].Expon < poly->NodeList[j + 1].Expon) {
				tempNode = poly->NodeList[j];
				poly->NodeList[j] = poly->NodeList[j + 1];
				poly->NodeList[j + 1] = tempNode;
			}	
		}
}

void deleteNode(PtrPolynomial poly, Position pos) {//删除节点矩阵索引为pos的元素
	for (int i = pos; i < poly->Last; i++)
		poly->NodeList[i] = poly->NodeList[i + 1];

	poly->Last--;
}

void deleteZeroNode(PtrPolynomial poly) { //删除poly中系数为0的节点
	for (int i = 0; i <= poly->Last; i++)
		if (poly->NodeList[i].Coeff == 0) {
			deleteNode(poly, i);
			i--;
		}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值