两个一元多项式的乘积与和

#include <iostream>
using namespace std;

typedef struct node {
	int x;
	int y;
	node *next;
}dx;

dx *readPoly();
dx *mult(dx *p1, dx *p2);
void printAdd(dx *p1, dx *p2);

int main(int argc, char *argv[])
{
	dx *p1 = readPoly();
	dx *p2 = readPoly();
	dx *mul = mult(p1, p2);

	
	while (mul != NULL)
	{
		cout << mul->x << " " << mul->y;
		if (mul->next != NULL) cout << " ";
		mul = mul->next;
	}
	cout << endl;
	printAdd(p1, p2);
	
	
	

	

	return 0;
}

dx *readPoly()
{
	int n;
	cin >> n;
	dx *p = new dx;
	p->next = NULL;
	dx *l = p;
	for (int i = 0; i < n; i++)
	{
		l->next = new dx;
		l = l->next;
		cin >> l->x;
		cin >> l->y;
		l->next = NULL;
	}
	dx *p1 = p->next;
	delete p;
	return p1;

}

dx *mult(dx *p1, dx *p2)
{
	dx *ret = new dx;
	dx *rear = ret;
	rear->y = 1000;
	dx *tmp = ret;
	while (p1 != NULL)
	{ 
		dx *p22 = p2;
		while (p22 != NULL)
		{
			dx *pt = new dx;
			pt->x = p1->x*p22->x;
			pt->y = p1->y + p22->y;
			pt->next = NULL;
			rear->next = pt;
			if (pt->y >= rear->y)
			{
				while (tmp->next->y > pt->y)
				{
					tmp = tmp->next;
				}
				if (tmp->next->y == pt->y)
				{
					tmp->next->x += pt->x;
					rear->next = NULL;
					delete pt;
					tmp = ret;
					p22 = p22->next;
					continue;
				}
				pt->next = tmp->next;
				tmp->next = pt;
				tmp = ret;
				rear->next = NULL;
				p22 = p22->next;
				continue;
			}
			rear = rear->next;
			p22 = p22->next;
		}
		p1 = p1->next;
	}
	dx *ret1 = ret->next;
	delete ret;
	return ret1;




}

void printAdd(dx *p1, dx *p2)
{
	while (p1 != NULL && p2 != NULL)
	{
		if (p1->y > p2->y)
		{
			cout << p1->x << " " << p1->y << " ";
			p1 = p1->next;
		}
		else if (p1->y < p2->y)
		{
			cout << p2->x << " " << p2->y << " ";
			p2 = p2->next;
		}
		else
		{
			cout << p1->x + p2->x << " " << p1->y << " ";
			p1 = p1->next;
			p2 = p2->next;
		}
	}
	while (p1 != NULL)
	{
		cout << p1->x << " " << p1->y;
		if (p1->next != NULL) cout << " ";
		p1 = p1->next;
	}
	while (p2 != NULL)
	{
		cout << p2->x << " " << p2->y;
		if (p2->next != NULL) cout << " ";
		p2 = p2->next;
	}
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值