多项式相乘(链表)

概述

一元多项式A,B按降次排列,用带头结点的链表存储,求C = A * B

思路

暑假的时候写过多项式A,B相加的,那个比较简单。这个要相乘,那就必须把A中的每一项分别和B中的每一项相乘。
所以就是,A中的每一项分别和B中的所有相乘,得到一个链表,然后把所有链表相加?感觉能做,但是好麻烦啊。有没有更简单的做法呢?
去网上搜索,emmm,快速傅里叶变换?快速数论变换?dbq我数学真的太菜了
具体分析一下,应该需要一个能通过输入创建链表的函数删除链表的函数多项式相加的函数多项式与单项式相乘的函数输出多项式的函数
因为会有新创建链表,插入链表什么的,所以这里用链式存储结构比较方便。

cpp

全部

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstddef>
using namespace std;
typedef struct lnode{
	int coef;
	int exp;
	struct lnode* next;
}lnode, *poly;
void create_polyn(poly &h);//创建一个多项式 
void print_polyn(poly h);//打印一个多项式 
void destroy_polyn(poly h);//删除多项式 
poly multiply_polyn(poly h, int num, int exp);//单项式多项式相乘 
poly polyn_add(poly h1, poly h2);//多项式相加 
poly polyn_polyn(poly h1, poly h2);//多项式多项式相乘 
int main()
{
	poly h1 = NULL, h2 = NULL, h3 = NULL;
	//输入两个多项式 
	create_polyn(h1);
	create_polyn(h2);
	//多项式相乘 
	h3 = polyn_polyn(h1, h2);
	//输出结果 
	print_polyn(h3);
	//释放空间 
	destroy_polyn(h3);
	return 0;
}
void create_polyn(poly &h)
{
	cout<<"请输入多项式:"<<endl;
	int coef, exp;
	h = new lnode;
	poly tem, last = h;
	cin>>coef>>exp;
	//都为0时结束输入 
	while(coef || exp){
		tem = new lnode;
		tem->coef = coef;
		tem->exp = exp;
		last->next = tem;
		last = tem;
		cin>>coef>>exp;
	}
	last->next = NULL;
}
void print_polyn(poly h)
{
	cout<<"多项式:";
	if(h == NULL || h->next == NULL)
	{
		cout<<"0"<<endl;
		return ;
	}
	h = h->next;
	while(h){
		cout<<" "<<h->coef<<"x^"<<h->exp<<" ";
		h = h->next;
		if(h)
		{
			cout<<"+";
		}
	}
	cout<<endl;
}
void destroy_polyn(poly h)
{
	poly tem;
	while(h)
	{
		tem = h;
		h = h->next;
		delete tem;
		tem = NULL;//这一步很重要一定要有 
	}
} 
poly multiply_polyn(poly h, int num, int exp)
{
	poly ne = NULL, last = NULL, tem = NULL;
	ne = new lnode;
	last = ne;//这一步必须放在给ne分配内存后,否则last = NULL 
	if(h == NULL) return ne;
	h = h->next;
	while(h)
	{
		tem = new lnode;
		tem->coef = num * h->coef;
		tem->exp = h->exp + exp;
		
		last->next = tem;
		last = tem;
		h = h->next;
	}
	last->next = NULL;//别忘了这个!!!!! 
	return ne;
}
poly polyn_add(poly h1, poly h2)
{
	
	poly ne = NULL, last = NULL, tem = NULL ,tem1 = h1, tem2 = h2;
	ne = new lnode;
	last = ne;
	
	if(h1) h1 = h1->next;
	if(h2) h2 = h2->next;
	
	delete tem1;
	delete tem2;
	
	while(h1 && h2)
	{
		
		if(h1->exp > h2->exp)
		{
			last->next = h1;
			last = h1;
			h1 = h1->next;
		}
		else if(h1->exp < h2->exp)
		{
			last->next = h2;
			last = h2;
			h2 = h2->next;
		}
		else
		{
			tem = new lnode;
			tem1 = h1;
			tem2 = h2;
			tem->coef = h1->coef + h2->coef;
			tem->exp = h1->exp;
			delete tem1;
			delete tem2;
			h1 = h1->next;
			h2 = h2->next;
			last->next = tem;
			last = tem;
		}
	}
	if(last) last->next = NULL;
	if(h1 == NULL)
	{
		last->next = h2;
	}
	if(h2 == NULL)
	{
		
		last->next = h1;
	}
	return ne;
}
poly polyn_polyn(poly h1, poly h2)
{
	poly h = NULL, result = NULL;
	h1 = h1->next;
	result = new lnode;
	result->next = NULL;
	int num, exp;
	if(h2 == NULL) return result;
	
	while(h1)
	{
		
		num = h1->coef;
		exp = h1->exp;
		h = multiply_polyn(h2, num, exp);//单项式多项式相乘
		result = polyn_add(result, h);//结果加在结果链表中 
		
		h1 = h1->next;
	}
	return result;
}
/*
 测试用例
1.
0 0
3 2
0 0
2.
9 8
6 5
0 0
3 2 
2 1
0 0
answer:27x^10 + 18x^9 + 18x^7 + 12x^6
3.
3 2
0 0
0 0
answer:0
4.
1 2
2 1
1 0
0 0
1 1
1 0
0 0
answer: 1x^3 + 3x^2 + 3x^1 + 1x^0 
5.
-8 40
1 20
0 0
0 0 
answer: 0
6.
6 5
5 4
8 3
3 2
2 0
0 0
2 3
4 0
5 -1
0 0
*/

创建多项式

void create_polyn(poly &h)
{
	cout<<"请输入多项式:"<<endl;
	int coef, exp;
	h = new lnode;
	poly tem, last = h;
	cin>>coef>>exp;
	//都为0时结束输入 
	while(coef || exp){
		tem = new lnode;
		tem->coef = coef;
		tem->exp = exp;
		last->next = tem;
		last = tem;
		cin>>coef>>exp;
	}
	last->next = NULL;
}

打印多项式

void print_polyn(poly h)
{
	cout<<"多项式:";
	if(h == NULL || h->next == NULL)
	{
		cout<<"0"<<endl;
		return ;
	}
	h = h->next;
	while(h){
		cout<<" "<<h->coef<<"x^"<<h->exp<<" ";
		h = h->next;
		if(h)
		{
			cout<<"+";
		}
	}
	cout<<endl;
}

删除多项式

void destroy_polyn(poly h)
{
	poly tem;
	while(h)
	{
		tem = h;
		h = h->next;
		delete tem;
		tem = NULL;//这一步很重要一定要有 
	}
} 

单项式乘多项式

poly multiply_polyn(poly h, int num, int exp)
{
	poly ne = NULL, last = NULL, tem = NULL;
	ne = new lnode;
	last = ne;//这一步必须放在给ne分配内存后,否则last = NULL 
	if(h == NULL) return ne;
	h = h->next;
	while(h)
	{
		tem = new lnode;
		tem->coef = num * h->coef;
		tem->exp = h->exp + exp;
		
		last->next = tem;
		last = tem;
		h = h->next;
	}
	last->next = NULL;//别忘了这个!!!!! 
	return ne;
}

两个多项式相加

poly polyn_add(poly h1, poly h2)
{
	
	poly ne = NULL, last = NULL, tem = NULL ,tem1 = h1, tem2 = h2;
	ne = new lnode;
	last = ne;
	
	if(h1) h1 = h1->next;
	if(h2) h2 = h2->next;
	
	delete tem1;
	delete tem2;
	
	while(h1 && h2)
	{
		
		if(h1->exp > h2->exp)
		{
			last->next = h1;
			last = h1;
			h1 = h1->next;
		}
		else if(h1->exp < h2->exp)
		{
			last->next = h2;
			last = h2;
			h2 = h2->next;
		}
		else
		{
			tem = new lnode;
			tem1 = h1;
			tem2 = h2;
			tem->coef = h1->coef + h2->coef;
			tem->exp = h1->exp;
			delete tem1;
			delete tem2;
			h1 = h1->next;
			h2 = h2->next;
			last->next = tem;
			last = tem;
		}
	}
	if(last) last->next = NULL;
	if(h1 == NULL)
	{
		last->next = h2;
	}
	if(h2 == NULL)
	{
		
		last->next = h1;
	}
	return ne;
}

多项式相乘

poly polyn_polyn(poly h1, poly h2)
{
	poly h = NULL, result = NULL;
	h1 = h1->next;
	result = new lnode;
	result->next = NULL;
	int num, exp;
	if(h2 == NULL) return result;
	
	while(h1)
	{
		
		num = h1->coef;
		exp = h1->exp;
		h = multiply_polyn(h2, num, exp);//单项式多项式相乘
		result = polyn_add(result, h);//结果加在结果链表中 
		
		h1 = h1->next;
	}
	return result;
}

测试用例

1.
0 0
3 2
0 0
2.
9 8
6 5
0 0
3 2 
2 1
0 0
answer:27x^10 + 18x^9 + 18x^7 + 12x^6
3.
3 2
0 0
0 0
answer:0
4.
1 2
2 1
1 0
0 0
1 1
1 0
0 0
answer: 1x^3 + 3x^2 + 3x^1 + 1x^0 
5.
-8 40
1 20
0 0
0 0 
answer: 0
6.
6 5
5 4
8 3
3 2
2 0
0 0
2 3
4 0
5 -1
0 0

注意

注意先判断指针是否为空,在指针->next = NULL。
规定好创建指针的时候,若输入0 0则指针->next = NULL,而不是头指针为NULL.头指针只有出错的时候才为NULL。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值