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

一个小时敲,五分钟改错。比一年前进步还是很大的。

但是如果测试点没有提示的话,改到哪年就不一定了( ´◔︎ ‸◔︎`)

02-线性结构2 一元多项式的乘法与加法运算_第1张图片

思路

多项式加法,极其类似Merge

(测试点2:系数加完要是0的话就不用添入结果多项式里了~)

多项式乘法,逐项相乘,看看在结果多项式里是合并同类项还是新添一项

(测试点1:合并完如果系数为0,要在结果多项式中remove这一项)

 code

# include 
# include 

struct item {
	int a; //系数
	int n; //指数
};

struct listNode {
	item data;
	listNode * pred, *succ;
	listNode(){}
	listNode(item d, listNode* p, listNode* s) : data(d), pred(p), succ(s){}
	void insertAsPred(item val)
	{
		listNode* tmp = new listNode(val, pred, this);
		pred->succ = tmp;
		pred = tmp;
	}
	void insertAsSucc(item val)
	{
		listNode* tmp = new listNode(val, this, succ);
		succ->pred = tmp;
		succ = tmp;
	}
};

struct Poly { //多项式类
	listNode *head, *tail;
	int size;

	Poly() : size(0)
	{
		head = new listNode;
		tail = new listNode;
		head->pred = NULL; head->succ = tail;
		tail->pred = head; tail->succ = NULL;
	}

	void insertAsLast(item val) //尾部追加
	{
		++size;
		tail->insertAsPred(val);
	}

	void print()
	{
		if (size == 0) std::cout << "0 0\n";

		listNode * p = head->succ;
		int _size = size;
		while (_size--)
		{
			std::string str_tail = _size == 0? "\n" : " ";
			std::cout << p->data.a << " " << p->data.n << str_tail;
			p = p->succ;
		}
	}

	void add(Poly const & P2, Poly & sum) //加和放入sum
	{
		listNode *point1 = head->succ, *point2 = P2.head->succ;
		while (point1 != tail && point2 != P2.tail)
		{
			if (point1->data.n > point2->data.n)
			{
				sum.insertAsLast(point1->data);
				point1 = point1->succ;
			}
			else if (point1->data.n < point2->data.n)
			{
				sum.insertAsLast(point2->data);
				point2 = point2->succ;
			}
			else
			{
				if (point1->data.a + point2->data.a != 0) //attention!!!
					sum.insertAsLast(item{point1->data.a+point2->data.a, point1->data.n});
				point1 = point1->succ;
				point2 = point2->succ;
			}
		}
		while (point1 != tail)
		{
			sum.insertAsLast(point1->data);
			point1 = point1->succ;
		}
		while (point2 != P2.tail)
		{
			sum.insertAsLast(point2->data);
			point2 = point2->succ;
		}
	}
	void times(Poly const & P2, Poly & product) //加和放入product
	{
		for (listNode * point1 = head->succ; point1 != tail; point1 = point1->succ)
		{
			for (listNode * point2 = P2.head->succ; point2 != P2.tail; point2 = point2->succ)
			{
				item tmp{ point1->data.a * point2->data.a, point1->data.n + point2->data.n };
				product.insert(tmp, product.search(tmp.n));
			}
		}
	}

	listNode * search(int n) //n是指数,按指数查找
	{
		listNode * p = head->succ;
		while (p != tail && p->data.n > n) //跳出时,要么等于,要么小于,要么是tail,在insert函数中,累加,或向前插入
		{
			p = p->succ;
		}
		return p;
	}
	void insert(const item & val, listNode * p) //多项式插入(有合并功能)
	{
		if (p != tail && p->data.n == val.n)
		{
			p->data.a += val.a;
			if (p->data.a == 0) remove(p);
		}
		else
		{
			p->insertAsPred(val);
			++size;
		}
	}
	void remove(listNode * p)
	{
		p->pred->succ = p->succ;
		p->succ->pred = p->pred;
		--size;
		delete p;
	}
};
int main(void)
{
	Poly P1, P2;

	// 输入两个多项式(高次项在头,低次项在尾)

	int K, a, n;
	std::cin >> K;
	while (K--) { std::cin >> a >> n; P1.insertAsLast(item{ a, n }); }

	std::cin >> K;
	while (K--) { std::cin >> a >> n; P2.insertAsLast(item{ a, n }); }


	// 计算

	Poly sum;
	P1.add(P2, sum);

	Poly product;
	P1.times(P2, product);


	// 输出两个结果多项式

	product.print();
	sum.print();
	

	return 0;
}

你可能感兴趣的:(ZJU数据结构,数据结构)