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

一元多项式乘法和加法运算

    • 1.1、题目
    • 1.2、题目的解读
    • 1.3、代码

1.1、题目

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

这道题是陈越老师、何钦铭老师(慕课网)上的数据结构里面的课后练习题目,打算使用链表来实现这个功能。

1.2、题目的解读

首先是输入,根据题目的描述,我们先需要输入非零多项式项,然后后面是显示系数,跟着指数,例如:
4 3 4 -5 2 6 1 -2 0
4代表有四个非零项,然后后面是3 4 -5 2 6 1 -2 0系数搭配指数,所以最终多项式是:

3 ∗ x 4 − 5 ∗ x 2 + 6 ∗ x − 2 3 * x^{4} - 5 * x^{2} + 6 * x -2 3x45x2+6x2

输出的话也是按照这个输出方式,输出非零项,显示系数,然后空格紧跟着指数,这里特别注意,题目要求是只输出非零项,除非整个多项式都是零项,这样只需要输出 “0 0”,加法相对简单,我说一下我的乘法思路,乘法思路的话我按照降序的插入,如果相同则合并,不相同则新增,等于0的话我进行插入。

1.3、代码


#include 

using namespace std;

typedef int ElementType;

struct data{
	ElementType coefficient;//系数
	int exponent; //指数

	struct data *pNext;
};

typedef struct data  List_t;


//初始化
void list_init(List_t *p)
{
	p->coefficient = 0;
	p->exponent = 0;
	p->pNext = NULL;
}
//创建一元多项式
void x_create(List_t *p)
{
	List_t *ptemp = NULL;
	List_t *ptemp2 = p;
	for (int i = 0; i < p->coefficient; i++){
		ptemp = new List_t;
		cin >> ptemp->coefficient;
		cin >> ptemp->exponent;
		ptemp->pNext = NULL;

		ptemp2->pNext = ptemp;

		ptemp2 = ptemp2->pNext;
	}
}

//打印一元多项式
void list_print(List_t *p)
{
	List_t *ptem = p;

	if (ptem->pNext == NULL){
		cout << "0 0" << endl;
		return;
	}

	while (ptem->pNext != NULL){
		cout << ptem->pNext->coefficient << " " << ptem->pNext->exponent;
		ptem = ptem->pNext;

		if (ptem->pNext != NULL){
			cout << " ";
		}
	}

	cout << endl;
}

//一元多项式相加
List_t* list_add(List_t *p1, List_t *p2)
{
	List_t *temp = new List_t;
	List_t* ptemp = p1;
	List_t* ptemp1 = p2;

	list_init(temp);

	List_t* ptemp2 = temp;

	while ((ptemp->pNext != NULL) && (ptemp1->pNext != NULL)){
		ptemp2->pNext = new List_t;
		ptemp2->pNext->pNext = NULL;
		if (ptemp->pNext->exponent == ptemp1->pNext->exponent){
			if ((ptemp->pNext->coefficient + ptemp1->pNext->coefficient) == 0){
				delete ptemp2->pNext;
				ptemp2->pNext = NULL;
				ptemp = ptemp->pNext;
				ptemp1 = ptemp1->pNext;
				continue;
			}
			else{
				ptemp2->pNext->coefficient = ptemp->pNext->coefficient + ptemp1->pNext->coefficient;
				ptemp2->pNext->exponent = ptemp->pNext->exponent;
				ptemp = ptemp->pNext;
				ptemp1 = ptemp1->pNext;
			}
			
		}
		else if (ptemp->pNext->exponent < ptemp1->pNext->exponent){
			ptemp2->pNext->coefficient = ptemp1->pNext->coefficient;
			ptemp2->pNext->exponent = ptemp1->pNext->exponent;
			ptemp1 = ptemp1->pNext;
		}
		else{
			ptemp2->pNext->coefficient = ptemp->pNext->coefficient;
			ptemp2->pNext->exponent = ptemp->pNext->exponent;
			ptemp = ptemp->pNext;
		}
		temp->coefficient++;
		ptemp2 = ptemp2->pNext;
	}

	//将剩余的拷贝进去
	if (ptemp->pNext != NULL){
		while (ptemp->pNext != NULL){
			ptemp2->pNext = new List_t;
			ptemp2->pNext->coefficient = ptemp->pNext->coefficient;
			ptemp2->pNext->exponent = ptemp->pNext->exponent;
			ptemp2->pNext->pNext = NULL;
			ptemp2 = ptemp2->pNext;
			ptemp = ptemp->pNext;

			temp->coefficient++;
		}
	}
	else if (ptemp1->pNext != NULL){
		while (ptemp1->pNext != NULL){
			ptemp2->pNext = new List_t;
			ptemp2->pNext->coefficient = ptemp1->pNext->coefficient;
			ptemp2->pNext->exponent = ptemp1->pNext->exponent;
			ptemp2->pNext->pNext = NULL;
			ptemp2 = ptemp2->pNext;
			ptemp1 = ptemp1->pNext;
			temp->coefficient++;

		}
	}
	return temp;
}

//一元多项式插入,如果相同指数则累加,否则根据降序插入
void list_insert(List_t *p, List_t *p2)
{
	List_t* ptemp = p;
	List_t* ptemp1 = NULL;


	while (ptemp->pNext != NULL){
		if (p2->exponent >= ptemp->pNext->exponent){

			if (p2->exponent == ptemp->pNext->exponent){
				if ((ptemp->pNext->coefficient + p2->coefficient) == 0){
					ptemp1 = ptemp->pNext;

					if (ptemp->pNext->pNext != NULL){
						ptemp->pNext = ptemp->pNext->pNext;
					}
					else{
						ptemp->pNext = NULL;
					}
					delete ptemp1;
					ptemp1 = NULL;
					return;
				}
				else{
					ptemp->pNext->coefficient = ptemp->pNext->coefficient + p2->coefficient;
					return; //找到相同项,直接返回了
				}
				
			}
			else{
				p2->pNext = ptemp->pNext;
				ptemp->pNext = p2; //把值插入进去
				return;
			}
		}
		ptemp = ptemp->pNext;
	}

	//都比他小的话只能插入到最后
	ptemp->pNext = p2;

}


//一元多项式相乘
List_t* list_multiplication(List_t *p1, List_t *p2)
{
	List_t *temp = new List_t;

	List_t* ptemp = p1;
	List_t* ptemp1 = NULL;

	list_init(temp);

	List_t* ptemp2 = NULL;

	while (ptemp->pNext != NULL){
		ptemp1 = p2;
		while (ptemp1->pNext != NULL){
			ptemp2 = new List_t;
			ptemp2->pNext = NULL;
			ptemp2->coefficient = ptemp->pNext->coefficient * ptemp1->pNext->coefficient;

			if (ptemp2->coefficient == 0){ //是零项就没必要插进去了
				delete ptemp2;
				ptemp2 = NULL;
				ptemp1 = ptemp1->pNext;
				continue;
			}
			else{
				ptemp2->exponent = ptemp->pNext->exponent + ptemp1->pNext->exponent;
			}
			ptemp1 = ptemp1->pNext;
			temp->coefficient++;

			list_insert(temp, ptemp2);
		}
		ptemp = ptemp->pNext;
	}

	return temp;
}


int main(void)
{

	List_t *pfirst = new List_t;
	List_t *psecond = new List_t;
	List_t *sum_result = NULL;

	List_t *multiplication_result = NULL;

	list_init(pfirst);
	list_init(psecond);

	cin >> pfirst->coefficient; //获取非零项个数
	x_create(pfirst); //创建一元多项式

	//cin.ignore(1);//忽略回车

	cin >> psecond->coefficient; //获取非零项个数
	x_create(psecond); //创建一元多项式


	sum_result = list_add(pfirst, psecond);

	multiplication_result = list_multiplication(pfirst, psecond);


	
	list_print(multiplication_result);
	list_print(sum_result);

	return 0;
}

你可能感兴趣的:(数据结构之题目练习)