一元多项式的加减求导运算算法数据结构算法.docx

上传人:b****6 文档编号:4508985 上传时间:2022-12-01 格式:DOCX 页数:25 大小:147.36KB
下载 相关 举报
一元多项式的加减求导运算算法数据结构算法.docx_第1页
第1页 / 共25页
一元多项式的加减求导运算算法数据结构算法.docx_第2页
第2页 / 共25页
一元多项式的加减求导运算算法数据结构算法.docx_第3页
第3页 / 共25页
一元多项式的加减求导运算算法数据结构算法.docx_第4页
第4页 / 共25页
一元多项式的加减求导运算算法数据结构算法.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

一元多项式的加减求导运算算法数据结构算法.docx

《一元多项式的加减求导运算算法数据结构算法.docx》由会员分享,可在线阅读,更多相关《一元多项式的加减求导运算算法数据结构算法.docx(25页珍藏版)》请在冰豆网上搜索。

一元多项式的加减求导运算算法数据结构算法.docx

一元多项式的加减求导运算算法数据结构算法

实验题目:

一元多项式运算

班级:

13级数学一班姓名:

张保昌学号:

**********日期:

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(n)

voidindex();O

(1)

五、使用说明及测试结果

根据提示语输入相应的信息。

如下为运行结果。

1)菜单

2)加法

3)减法

4)导数

六、源程序

#include

usingnamespacestd;

typedefstructpnode

{

doublecoef;/*系数域*/

intexp;/*指数域*/

structpnode*next;/*指针域,指向下一个系数不为0的子项*/

}polynode,*polylink;

polylinkinsert_list(polylinkh,charo);

polylinkorder_list(polylinkh);

polylinksimply_list(polylinkh);

polylinkadd(polylinka,polylinkb);

polylinkopposite(polylinkb);

polylinkderivative(polylinka);

voidlist_display(polylinkh,charo);

voidindex();

voidmain()

{

index();

intmark=1;

polylinkA=NULL,B=NULL,C=NULL;

chara='A',b='B',c='C',Da='d';

while(mark)

{

cout<<"请选择-->mark="<<"";

cin>>mark;

cin.get();

system("cls");

switch(mark)

{

case1:

A=B=C=NULL;

cout<<"一元多项式加法"<

cout<<"请输入";

A=insert_list(A,a);

B=insert_list(B,b);

A=simply_list(A);

A=order_list(A);

B=simply_list(B);

B=order_list(B);

cin.get();

system("cls");

cout<<"一元多项式加法"<

list_display(A,a);

list_display(B,b);

cout<

cout<<"多项式A与B相加得:

"<

C=add(A,B);

list_display(C,c);

break;

case2:

A=B=C=NULL;

cout<<"一元多项式减法"<

cout<<"请输入被减数";

A=insert_list(A,a);

cout<<"请输入要减数";

B=insert_list(B,b);

A=simply_list(A);

A=order_list(A);

B=simply_list(B);

B=order_list(B);

cin.get();

system("cls");

cout<<"一元多项式减法"<

cout<<"被减数";

list_display(A,a);

cout<<"减数";

list_display(B,b);

cout<

cout<<"多项式(A-B)得:

"<

B=opposite(B);

C=add(A,B);

list_display(C,c);

break;

case3:

A=NULL;

cout<<"一元多项式求导运算"<

A=insert_list(A,a);

A=simply_list(A);

A=order_list(A);

list_display(A,a);

cout<<"其导数为:

"<

A=derivative(A);

list_display(A,Da);

system(“pause”)

break;

default:

break;

}

if(mark==0)

break;

cin.get();

system("cls");

index();

}

}

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)

{

polylink

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1