线性表之多项式乘法

多项式乘法

时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 298            测试通过 : 131

描述

线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。

现给两个一元整系数多项式,请求解两者的乘积。



输入

两组数据,每一组代表一个一元整系数多项式,有多行组成,其中每一行给出多项式每一项的系数和指数,这些行按指数递减次序排序。每一组结束行输入为0 -1

输出

三组数据,前两组为一元整系数多项式,最后一组为两个多项式的乘积。

一元整系数多项式输出形式如下:

1)多项式项4x输出为4X

2)多项式项4x2输出为4X^2

3)第一项系数为正数时,加号不要输出

4)除常系数项外,项系数为1不显式输出,-1输出为-

例如,4x3- x2+x-1正确输出形式为4X^3-X^2+X-1,错误输出形式为 +4X^3-1X^2+1X-1

样例输入

3 14
-8 8
6 2
2 0
0 -1
2 10
4 8
-6 2
0 -1

样例输出

3X^14-8X^8+6X^2+2
2X^10+4X^8-6X^2
6X^24+12X^22-16X^18-50X^16+12X^12+76X^10+8X^8-36X^4-12X^2


多项式乘法可以看做是做了多次的加法,多项式加法在我另外一篇博客当中,上代码~

#include <iostream>
#include <list>
using namespace std;
typedef class item{
public:
	int xs;
	int zs;
	item(int xs,int zs){
		this->xs = xs;
		this->zs = zs;
	}
}ITEM;

typedef list<ITEM> LISTITEM;

/*
显示系数,+/-1是不显示那个1的
*/
void displayXS(int xs){
	if (xs == 1 || xs == -1) 
		return;
	else 
		cout << xs;
}

/*
显示指数,指数为0是不显示那个X的,指数为1显示X
*/
void displayZS(int zs){
	if (zs == 0)
		return;
	else if (zs == 1)
		cout << "X";
	else
		cout << "X^" << zs;
}

void display(LISTITEM &L){
	LISTITEM::iterator i = L.begin();
	if (i == L.end()) {
		cout << "0" << endl;
		return;
	}
	//开头是不需要显示+号的,开头遇到+1/-1也是需要好好考虑的
	/*
	1、指数为0,系数为+1/-1
	2、指数不为0.系数为-1
	3、其他情况
	*/
	if (i->zs == 0&&(i->xs==-1||i->xs==1)){
		cout << i->xs;
	}
	else if (i->zs != 0 && i->xs == -1){
		cout << "-";
		displayZS(i->zs);
	}
	else{
		displayXS(i->xs);
		displayZS(i->zs);
	}
	i++;
	for (i; i != L.end(); i++){
		if (i->xs > 0){
			cout << "+";
		}
		if (i->zs == 0 && (i->xs == -1 || i->xs == 1)){
			cout << i->xs;
		}
		else if (i->zs != 0 && i->xs == -1){
			cout << "-";
			displayZS(i->zs);
		}
		else{
			displayXS(i->xs);
			displayZS(i->zs);
		}
	}
	cout << endl;
}
//h1 = h1 + h2
void add(LISTITEM &h1, LISTITEM &h2){
	LISTITEM res;
	LISTITEM::iterator iter1, iter2;
	iter1 = h1.begin();
	iter2 = h2.begin();
	if (iter1 == h1.end() && iter2 == h2.end()){
		return;
	}
	for (; iter1 != h1.end() && iter2 != h2.end();){
		if (iter1->zs > iter2->zs){
			ITEM *temp = new ITEM(iter1->xs, iter1->zs);
			res.push_back(*temp);
			iter1++;
		}
		else if (iter1->zs < iter2->zs){
			ITEM *temp = new ITEM(iter2->xs, iter2->zs);
			res.push_back(*temp);
			iter2++;
		}
		else{
			if (iter1->xs + iter2->xs == 0){
				iter1++;
				iter2++;
			}
			else{
				ITEM *temp = new ITEM(iter1->xs+iter2->xs, iter1->zs);
				res.push_back(*temp);
				iter1++;
				iter2++;
			}
		}
	}
	if(iter1 == h1.end() && iter2!=h2.end())
	{
		for (; iter2 != h2.end(); iter2++){
			ITEM *temp = new ITEM(iter2->xs, iter2->zs);
			res.push_back(*temp);
		}
	}
	else if (iter2 == h2.end() && iter1 != h1.end())
	{
		for (; iter1 != h1.end(); iter1++){
			ITEM *temp = new ITEM(iter1->xs, iter1->zs);
			res.push_back(*temp);
		}
	}
	h1.clear();
	for (iter1 = res.begin(); iter1 != res.end(); iter1++){
		h1.push_back(*iter1);
	}
}

void multi(LISTITEM &res, LISTITEM &h1, LISTITEM &h2){
	LISTITEM::iterator iter1, iter2;
	iter1 = h1.begin();
	iter2 = h2.begin();
	if (iter1 == h1.end() && iter2 == h2.end()){
		return;
	}
	LISTITEM temp;
	for (; iter1 != h1.end(); iter1++){		
		for (iter2 = h2.begin(); iter2 != h2.end(); iter2++){
			ITEM *tempI = new ITEM(iter1->xs*iter2->xs, iter1->zs + iter2->zs);
			temp.push_back(*tempI);
		}
		add(res, temp);
		temp.clear();
	}
		
}

int main(){
	LISTITEM head[2];
	int xs, zs;
	for (int i = 0; i < 2; i++){
		while (cin >> xs >> zs && (xs != 0 || zs != -1)){
			if (xs == 0) continue; //系数为零的就不加入list了
			ITEM *temp = new ITEM(xs,zs);
			head[i].push_back(*temp);
		}
	}
	display(head[0]); display(head[1]);
	LISTITEM res;
	multi(res, head[0], head[1]);
	display(res);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qugename/article/details/41418307