一元多项式的加减求导运算算法数据结构算法.docx
《一元多项式的加减求导运算算法数据结构算法.docx》由会员分享,可在线阅读,更多相关《一元多项式的加减求导运算算法数据结构算法.docx(25页珍藏版)》请在冰豆网上搜索。
一元多项式的加减求导运算算法数据结构算法
实验题目:
一元多项式运算
班级:
13级数学一班姓名:
张保昌学号:
2013433037日期:
2014—10—09
一、需求分析
1.问题描述;
设计一个简单的一元稀疏多项式加减及求导运算器。
2.基本要求的功能要求;
(1)输入多项式时可以按任意次序输入各项的数据(输入并建立多项式A与B),不必按指数有序;在算法中实现建立按指数有序的多项式。
(2)计算多项式A与B的和,即建立多项式A+B。
(3)按照指数升序次序,输出多项式A、B、A+B。
(4)计算多项式A与B的差,即建立多项式A-B;
(5)计算多项式A的导函数Aˊ。
3.测试数据。
(1)(x+3x6-8.6x11)+(6-3x6+21x9)=6+x+21x9-8.6x11
(2)(3x-3-x+4.1x2-1.2x9)+(―3x―3-5.1x2+7.8x12)=-x-x2-1.2x9+7.8x12
(3)(x+x3)+(―x―x3)=0
(4)(x+x2+x3)+0=x+x2+x3
(5)(x+x2+x3)—(x+x2+x3)=0
(6)(x+x2+x3)ˊ=1+2x+3x2
二、概要设计
1.本程序所用的抽象数据类型的定义;
typedefstructpnode
{
doublecoef;/*系数域*/
intexp;/*指数域*/
structpnode*next;/*指针域,*/
}polynode,*polylink;
polylinkinsert_list(polylinkh,charo);//输入多项式。
polylinkorder_list(polylinkh);//按指数升序排列
polylinksimply_list(polylinkh);//初步整理(合并多项式,并删除系数为零的式子)
polylinkadd(polylinka,polylinkb);//加法运算
polylinkopposite(polylinkb);//将减法统归为加法
polylinkderivative(polylinka);//求导函数
voidlist_display(polylinkh,charo);//输出显示
voidindex();//菜单函数
2.模块划分。
1)主函数模块。
2)加法运算模块3)减法运算模块4)导数模块。
3.主模块的流程及各子模块的主要功能;
三、详细设计
1.采用c++语言定义相关的数据类型;
typedefstructpnode
{
doublecoef;/*系数域*/
intexp;/*指数域*/
structpnode*next;/*指针域
}polynode,*polylink;
Coef
系数域
Exp
指数域
*next
指针域
2.写出各模块的伪码算法;
voidindex()//菜单函数。
{
cout<<"一元多项式运算"<cout<<"1.一元多项式加法"<cout<<"2.一元多项式减法"<cout<<"3.一元多项式导数"<cout<<"0.结束"<}
polylinkinsert_list(polylinkh,charo)//输入多项式
{
h=newpolynode;
doublecoef1;
intnum,expo1;
polylinktemp;
polynode*data;
temp=h;
h->next=NULL;//头结点
cout<<"多项式"<";
cin>>num;
for(inti=1;i<=num;i++)
{
cout<<"请输入第"<
cout<<"系数:
";
cin>>coef1;
cout<<"指数:
";
cin>>expo1;
data=newpolynode;
data->coef=coef1;
data->exp=expo1;
data->next=NULL;
temp->next=data;
temp=data;
}
returnh;
}
polylinksimply_list(polylinkh)//初步化简,系数无0,无重复指数
{
polylinkp,q,r,k;
p=h->next;
if(!
p)
returnh;//空表
while(p)
{
k=p;
q=k->next;
while(q)
{
if(q->exp==p->exp)
{
r=q;
q=q->next;
p->coef+=r->coef;
k->next=r->next;
deleter;
}
else
{
q=q->next;
k=k->next;
}
}
p=p->next;
}
k=h;
q=h->next;
while(q)
{
if(q->coef==0)
{
r=q;
q=q->next;
k->next=r->next;
deleter;
}
else
{
q=q->next;
k=k->next;
}
}
returnh;
}
voidlist_display(polylinkh,charo)//显示多项式
{
polylinkp;
doublecoef1;
intexpo1,i=0;
p=h->next;
if(!
p)
{
cout<<"多项式"<0"<}
else
cout<<"多项式"<";
while(p)
{
coef1=p->coef;
expo1=p->exp;
if(i==0)
{
if(expo1==0)
{
i=1;
cout<}
elseif(expo1==1)
{
i=1;
if(coef1==1)
cout<<"X";
elseif(coef1==-1)
cout<<"-X";
else
cout<}
else
{
i=1;
if(coef1==1)
cout<<"X^"<elseif(coef1==-1)
cout<<"-X^"<else
cout<}
}
else
{
if(expo1==1)
{
if(coef1==1)
cout<<"+X";
elseif(coef1==-1)
cout<<"-X";
elseif(coef1>0)
cout<<"+"<else
cout<}
else
{
if(coef1==-1)
cout<<"-X^"<elseif(coef1==1)
cout<<"+X^"<elseif(coef1>0)
cout<<"+"<else
cout<}
}
p=p->next;
}
cout<}
polylinkorder_list(polylinkh)//升序排列
{
polynodetemp;
polylinkp,q,r;
p=h->next;
if(!
p)returnh;
while(p->next)
{
q=p->next;
r=p;
while(q)
{
if(q->expexp)
r=q;
q=q->next;
}
temp.coef=r->coef;
temp.exp=r->exp;
r->coef=p->coef;
r->exp=p->exp;
p->coef=temp.coef;
p->exp=temp.exp;
p=p->next;
}
returnh;
}
polylinkadd(polylinkha,polylinkhb)//加法
{
polylinka;
a=ha;
while(a->next)
a=a->next;
a->next=hb->next;
deletehb;
ha=simply_list(ha);
ha=order_list(ha);
returnha;
}
polylinkopposite(polylinkb)
{
polylinkhb;
hb=b->next;
while(hb)
{
hb->coef=(-1)*hb->coef;
hb=hb->next;
}
returnb;
}
polylinkderivative(polylinka)//求导
{
polylinkha;
ha=a->next;
while(ha)
{
ha->coef*=ha->exp;
ha->exp--;
ha=ha->next;
}
returna;
}
四、调试分析
1.调试中遇到的问题及对问题的解决方法;
指针指向的错误。
导致程序无法正常运行,经过逐步调试,发现了问题,认真分析指针指向的内存空间。
并做了合理的修改。
2.算法的时间复杂度和空间复杂度。
polylinkorder_list(polylinkh);O(n²)
polylinksimply_list(polylinkh);O(n²)
polylinkadd(polylinka,polylinkb);O(n²)
polylinkopposite(polylinkb);//O(n)
polylinkinsert_list(polylinkh,charo)O(n)
polylinkderivative(polylinka);//O(n)
voidlist_display(polylinkh,charo);O(