02-线性结构2 一元多项式的乘法与加法运算 (20分)

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

输入格式:

输入分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<bits/stdc++.h>
using namespace std;

struct polyNode {
	int coef;
	int expon;
	polyNode* next=NULL; //如果此处不是默认NULL,那么在后面每一次new的时候都需要手动设置,否则会出现错误
};

void attach(int c, int e, polyNode** rear) {
	polyNode* p=new polyNode;
	p->coef = c;
	p->expon = e;
	p->next = NULL;
	(*rear)->next = p;
	*rear = p;
}

polyNode* readPoly() {
	polyNode *p, *rear, *tmp;  //p是一个dummyHead
	p = new polyNode;
	p->next = NULL;
	rear = p;

	int n, c, e;
	cin >> n;
	while (n--) {
		cin >> c >> e;
		attach(c, e, &rear);
	}
	tmp = p;
	p = p->next;
	delete tmp;
	return p;
}

polyNode* add(polyNode* p1, polyNode* p2) {

	polyNode *p = new polyNode;
    p->next=NULL;
	polyNode *rear, *tmp;
	rear = p;

	while (p1 && p2) {
		if (p1->expon > p2->expon) {
			attach(p1->coef, p1->expon, &rear);
			p1 = p1->next;
		}
		else if (p1->expon < p2->expon) {
			attach(p2->coef, p2->expon, &rear);
			p2 = p2->next;
		}
		else {
			if (p1->coef + p2->coef != 0) {
				attach(p1->coef + p2->coef, p1->expon, &rear);
			}
			p1 = p1->next;
			p2 = p2->next;
		}
	}
	while (p1) {
		attach(p1->coef, p1->expon, &rear);
		p1 = p1->next;
	}
	while (p2) {
		attach(p2->coef, p2->expon, &rear);
		p2 = p2->next;
	}
	tmp = p;
	p = p->next;
	delete tmp;
	return p;
}

polyNode* mult(polyNode* p1, polyNode* p2) {
	if (!p1 || !p2) return NULL;
	polyNode *p, *rear, *t1, *t2, *t;
	int c, e;
	t1 = p1; t2 = p2;
	p = new polyNode;
	p->next = NULL;
	rear = p;
	while (t2) { /* 先用P1的第1项乘以P2,得到P */
		attach(t1->coef*t2->coef, t1->expon + t2->expon, &rear);
		t2 = t2->next;
	}
	t1 = t1->next;
	while (t1) {
		t2 = p2, rear = p;
		while (t2) {
			c = t1->coef*t2->coef;
			e = t1->expon + t2->expon;
			while (rear->next && rear->next->expon > e)
				rear = rear->next;
			if (rear->next && rear->next->expon == e) {
				if (rear->next->coef + c) {
					rear->next->coef += c;
				}
				else {
					t = rear->next;
					rear->next = t->next;
					delete t;
				}
			}
			else {
				polyNode* t=new polyNode;
				t->coef = c;
				t->expon = e;
				t->next = rear->next;
				rear->next = t;
				rear = rear->next;
			}
			t2 = t2->next;
		}
		t1 = t1->next;
	}
	t2 = p; p = p->next; delete t2;
	return p;
}

void printPoly(polyNode* p) {
	if (!p) {
		cout << 0 << " " << 0 << endl;
		return;
	}
	int flag = 0; //0表示每行第一次输出前面不需要空格
	while (p) {
		if (!flag) {
			flag = 1;
		}
		else {
			cout << " ";
		}
		cout << p->coef << " " << p->expon;
		p = p->next;
	}
	cout << endl;
}


int main() {
	polyNode *p1, *p2, *pm, *pa;
	p1 = readPoly();
	p2 = readPoly();
	pm = mult(p1, p2);
	printPoly(pm);
	pa = add(p1, p2);
	printPoly(pa);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值