链表实现一元多项式加,减,乘,求导

链表实现一元多项式加,减,乘,求导.

#include<iostream>
#include<cstring>
using namespace std;
typedef struct polyn{
	float coef;
	int expn;
	struct polyn *next;
}POLYN, *pPOLYN;
pPOLYN polynAdd(pPOLYN a, pPOLYN b,int judge);
pPOLYN createPolyn();
void printPolyn(pPOLYN head);
pPOLYN copyPolyn(pPOLYN a);
pPOLYN polynMul(pPOLYN a, pPOLYN b);
pPOLYN itemMul(pPOLYN a, pPOLYN b);
pPOLYN polynDer(pPOLYN a);

pPOLYN polynDer(pPOLYN a){
	pPOLYN tem, p1;
	p1 = a->next;
	if (p1->expn==0)
	{
		a->next = p1->next;
		a->expn--;
		tem = p1;
		p1 = p1->next;
	}
	while (p1!=NULL){
		p1->coef *= p1->expn;
		p1->expn--;
		p1 = p1->next;
	}
	return a;
}
pPOLYN itemMul(pPOLYN a, pPOLYN b){
	pPOLYN res;
	if (!(res = (pPOLYN)malloc(sizeof(POLYN)))){
		cout << "error!" << endl;
		exit(0);
	}
	res->coef = a->coef*b->coef;
	res->expn = a->expn + b->expn;
	return res;
}
pPOLYN polynMul(pPOLYN a,pPOLYN b){
	pPOLYN c,p1, p2;
	pPOLYN jItems[1000];
	pPOLYN iItems[1000];
	c  =(pPOLYN) malloc(sizeof(POLYN));
	c->next = NULL;
	c->expn = 0;
	p1 = a->next;
	p2 = b->next;
	for (int i = 0; i < a->expn; i++)
	{
		for (int j = 0; j < b->expn; j++)
		{
			jItems[j]=itemMul(p1,p2);
			jItems[j]->next = NULL;
			p2 = p2->next;
		}
		iItems[i] = (pPOLYN)malloc(sizeof(POLYN));
		iItems[i]->expn =0;
		iItems[i]->next = NULL;
		for (int k = 0; k <b->expn; k++)
		{
			pPOLYN tem = (pPOLYN)malloc(sizeof(POLYN));
			tem->next = jItems[k];
			tem->expn = 1;
			iItems[i]=polynAdd(iItems[i],tem,1);
		}
		p2 = b->next;
		p1 = p1->next;
	}
	for (int i = 0; i < a->expn; i++)
	{
		c = polynAdd(c,iItems[i],1);
	}
	return c;
}
pPOLYN createPolyn(){
	int min=-99999,num;
	pPOLYN q,p,head;
	cout << "请输入创建的一元多项式项数"<< endl;
	if (!(head = (POLYN *)malloc(sizeof(POLYN)))){
		cout << "error" << endl;
		exit(0);
	}
	cin >> head->expn;
	num = head->expn;
	if (head->expn==0)
	{
		return head;
	}
	q = head;
	q->next = NULL;
	for (int i = 0; i < num; i++)
	{
		if (!(p = (POLYN *)malloc(sizeof(POLYN))))
		{
			cout << "error" << endl;
			exit(0);
		}
		cout << "输入第"<<i+1<<"个项的指数和系数:" << endl;
		cin >> p->expn>>p->coef;
		while (p->expn<min)
		{
			cout << "输入的指数不能比上一项小!" << endl;
			cout << "输入第" << i + 1 << "个项的指数和系数:" << endl;
			cin >> p->expn >> p->coef;
		}
		if (p->expn==min)
		{
			q->coef += p->coef;
			head->expn--;
			free(p);
			continue;
		}
		min = p->expn;
		q->next = p;
		q = q->next;
	}
	q->next = NULL;
	p = head;
	while (p->next!=NULL){
		if (p->next->coef==0)
		{
			q = p->next;
			p->next = q->next;
			free(q);
			head->expn--;
		}
		p = p->next;
	}
	return head;
}
void printPolyn(pPOLYN head){
	int len = head->expn;
	pPOLYN p1 = head->next;
	for (int i = 0; i < len; i++)
	{
		cout <<"第"<<i+1<<"项指数:"<<p1->expn<<"	\t系数:"<<p1->coef<< endl<<endl;
		p1 = p1->next;
	}
}
pPOLYN copyPolyn(pPOLYN a){
	pPOLYN q,p,tem,head;
	tem = a->next;
	if (!(q = (pPOLYN)malloc(sizeof(POLYN))))
	{
		cout << "error" << endl;
		exit(0);
	}
	head = q;
	q->expn = a->expn;
	for (int i = 0; i <a->expn ; i++)
	{
		if (!(p = (pPOLYN)malloc(sizeof(POLYN))))
		{
			cout << "error" << endl;
			exit(0);
		}
		p->coef = tem->coef;
		p->expn = tem->expn;
		tem = tem->next;
		q->next = p;
		q = q->next;
	}
	q->next = NULL;
	return head;
}
pPOLYN polynAdd(pPOLYN a,pPOLYN b,int judge){
	pPOLYN c,p1, p2,p3;
	p1 = a->next;
	p2 = b->next;
	c=p3 = a;
	c->expn = a->expn + b->expn;
	while (p1!=NULL&&p2!=NULL){
		if (judge==2)
		{
			p2->coef = -p2->coef;
		}
		if (p1->expn<p2->expn)
		{
			p3->next = p1;
			p3 = p3->next;
			p1 = p1->next;
		}
		else if (p1->expn>p2->expn){
			p3->next = p2;
			p3 = p3->next;
			p2 = p2->next;
		}
		else if (p1->expn==p2->expn)
		{
			pPOLYN tem;
			c->expn--;
			if ((p1->coef+p2->coef)!=0)
			{
				tem = p2;
				p3->next = p1;
				p3 = p3->next;
				p1 = p1->next;
				p3->coef += p2->coef;
				p2 = p2->next;
				free(tem);
			}
			else{
				c->expn--;
				tem = p1;
				p1 = p1->next;
				free(tem);
				tem = p2;
				p2 = p2->next;
				free(tem);
			}
		}
		p3->next = NULL;
	}
	if (p1!=NULL){
		p3->next = p1;
	}
	if (p2!=NULL){
		p3->next = p2;
	}
	free(b);
	return c;
}
int main(){
	pPOLYN operand1, operand2, addRes, copy1, copy2, reduceRes, mulRes, copyA, copyB, derRes;
	operand1=createPolyn();
	operand2 = createPolyn();
	copy1 = copyPolyn(operand1);
	copy2 = copyPolyn(operand2);
	copyA = copyPolyn(operand1);
	copyB = copyPolyn(operand2);
	addRes = polynAdd(operand1, operand2, 1);
	reduceRes = polynAdd(copy1,copy2,2);
	mulRes = polynMul(copyA,copyB);
	cout << "两个一元多项式相加:" << endl;
	printPolyn(addRes);
	cout << "两个一元多项式相减:" << endl;
	printPolyn(reduceRes);
	cout << "两个一元多项式相乘:" << endl;
	printPolyn(mulRes);
	derRes = polynDer(copyA);
	cout << "第一个一元多项式求导:" << endl;
	printPolyn(derRes);
	getchar();
	getchar();
	return 0;
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值