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

这是MOOC上线性结构最后一节讲的题目,具体描述如下

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

输入格式:
输入分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

程序的主要流程为:

  1. 读取多项式
  2. 多项式加法
  3. 多项式乘法
  4. 输出多项式

所以编写四个函数分别实现四个功能

#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode *Polynomial;
struct PolyNode{
	int coef;//系数
	int expon;//指数
	Polynomial link;//指向后一个节点的指针域
};

Polynomial ReadPoly();//读多项式
void Attach(int c, int e, Polynomial *pRear);//构造多项式
Polynomial Mult(Polynomial P1, Polynomial P2);//多项式乘法
Polynomial Add(Polynomial P1, Polynomial P2);//多项式加法
void PrintPoly(Polynomial P);//输出多项式

int main(int argc, const char * argv[]) {
	// insert code here...
	Polynomial P1, P2, PP, PS;
	P1 = ReadPoly();
	P2 = ReadPoly();
	PP = Mult(P1, P2);
	PrintPoly(PP);
	PS = Add(P1, P2);
	PrintPoly(PS);
	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(int c, int e, Polynomial *pRear){
	Polynomial P;
	
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->coef = c;
	P->expon = e;
	P->link = (*pRear)->link;
	(*pRear)->link = P;
	*pRear = P;
}

Polynomial Add(Polynomial P1, Polynomial P2){
	Polynomial front, rear, t1, t2;
	int sum;
	t1 = P1;
	t2 = P2;
	front = (Polynomial)malloc(sizeof(struct PolyNode));
	front->link = NULL;
	rear = front;
	while (t1 && t2) {
		if (t1->expon == t2->expon) {
			sum = t1->coef + t2->coef;
			if(sum)//考虑指数相同项系数相加为0的情况
				Attach(sum, t1->expon, &rear);
			t1 = t1->link;
			t2 = t2->link;
		}
		else 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;
		}
	}
	//把表中剩余的所有节点都插到链表后
	while (t1){
		Attach(t1->coef, t1->expon, &rear);
		t1 = t1->link;
	}
	while (t2){
		Attach(t2->coef, t2->expon, &rear);
		t2 = t2->link;
	}
	rear = front;
	front = front->link;
	free(rear);
	return front;
}

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(struct PolyNode));
	
	P->link = NULL;//为什么要让p指向空?
	
	Rear = P;
	//先用多项式1的第一项乘以多项式2构造出第一项
	while (t2) {
		Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
		t2 = t2->link;
	}
	t1 = t1->link;//从多项式1的第二项开始
	while (t1) {
	//把多项式1的每一项都乘以多项式2
		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;
	}
	Rear = P;
	P = P->link;
	free(Rear);//释放节点空间
	return P;
}

void PrintPoly(Polynomial 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;
	}
	printf("\n");
}

需要注意的是在输出多项式时有一个要求,输出最后一项不要有空格,可以这样考虑:
输出第一个数字时加一个空格(“A ”),后面的所有元素用系数空格指数的格式(“A B”)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值