概述
一元多项式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。