1、一元多项式的加减求导运算算法数据结构算法实验题目:一元多项式运算班级:13级数学一班 姓名: 张保昌 学号:2013433037 日期:20141009一、需求分析1问题描述;设计一个简单的一元稀疏多项式加减及求导运算器。2基本要求的功能要求; (1)输入多项式时可以按任意次序输入各项的数据(输入并建立多项式A与B),不必按指数有序;在算法中实现建立按指数有序的多项式。 (2)计算多项式A与B的和,即建立多项式A+B。 (3)按照指数升序次序,输出多项式A、B、A+B。 (4)计算多项式A与B的差,即建立多项式AB; (5) 计算多项式A的导函数A 。3测试数据。 (1)(x+3x68.6x1
2、1)+(63x6+21x9)=6+x+21x98.6x11 (2)(3x3x+4.1x21.2x9)(3x35.1x2 +7.8x12)=xx21.2x9+7.8x12 (3)(x+x3)+(xx3)=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本程序所用的抽象数据类型的定义;typedef struct pnode double coef; /*系数域*/int exp; /*指数域*/struct pnode *next; /*指针域,*/polynode, *polylink;
3、polylink insert_list(polylink h,char o); /输入多项式。polylink order_list(polylink h); /按指数升序排列polylink simply_list(polylink h); /初步整理(合并多项式,并删除系数为零的式子)polylink add(polylink a,polylink b); /加法运算polylink opposite(polylink b); /将减法统归为加法polylink derivative(polylink a); /求导函数void list_display(polylink h,char
4、o); /输出显示void index(); /菜单函数2模块划分。 1)主函数模块。2)加法运算模块 3)减法运算模块 4)导数模块。3主模块的流程及各子模块的主要功能; 三、详细设计1采用c+语言定义相关的数据类型;typedef struct pnode double coef; /*系数域*/int exp; /*指数域*/struct pnode *next; /*指针域polynode, *polylink;Coef系数域Exp指数域*next指针域2写出各模块的伪码算法;void index() /菜单函数。 cout 一元多项式运算 endlendl; cout 1.一元多项式
5、加法endl; cout 2.一元多项式减法endl; cout 3.一元多项式导数endl; cout 0. 结束 endlendlnext =NULL;/头结点 cout多项式onum; for(int i=1;i=num;i+) cout请输入第i项endl; coutcoef1; coutexpo1; data=new polynode; data-coef=coef1; data-exp =expo1; data-next =NULL; temp-next =data; temp=data; return h;polylink simply_list(polylink h) /初步化
6、简,系数无0,无重复指数 polylink p,q,r,k; p=h-next ; if(!p) return h; /空表 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 ; delete r; 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 ; delete r; else
7、q=q-next ; k=k-next ; return h;void list_display(polylink h,char o) /显示多项式 polylink p; double coef1; int expo1,i=0; p=h-next ; if(!p) cout多项式o : 0 endl; else cout多项式ocoef ; expo1=p-exp ; if(i=0) if(expo1=0) i=1; coutcoef1; else if(expo1=1) i=1; if(coef1=1) coutX; else if(coef1=-1) cout-X; else coutc
8、oef1X; else i=1; if(coef1=1) coutXexpo1; else if(coef1=-1) cout-Xexpo1; else coutcoef1Xexpo1; else if(expo1=1) if(coef1=1) cout+X; else if(coef1=-1) cout0) cout+coef1X; else coutcoef1X; else if(coef1=-1) cout-Xexpo1; else if(coef1=1) cout+X0) cout+coef1Xexpo1; else coutcoef1Xnext ; coutnext; if(!p)r
9、eturn h; while(p-next) q=p-next ; r=p; while(q) if(q-exp exp ) 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 ; return h;polylink add(polylink ha,polylink hb)/加法 polylink a; a=ha; while(a-next ) a=a-next ; a-next =
10、hb-next ; delete hb; ha=simply_list(ha); ha=order_list(ha); return ha;polylink opposite(polylink b) polylink hb; hb=b-next; while(hb) hb-coef =(-1)*hb-coef; hb=hb-next ; return b;polylink derivative(polylink a)/求导 polylink ha; ha=a-next ; while(ha) ha-coef *=ha-exp ; ha-exp -; ha=ha-next ; return a;
11、 四、调试分析1调试中遇到的问题及对问题的解决方法; 指针指向的错误。导致程序无法正常运行,经过逐步调试,发现了问题,认真分析指针指向的内存空间。并做了合理的修改。2算法的时间复杂度和空间复杂度。 polylink order_list(polylink h); O(n) polylink simply_list(polylink h); O(n) polylink add(polylink a,polylink b); O(n) polylink opposite(polylink b);/ O(n) polylink insert_list(polylink h,char o) O(n) polylink derivative(polylink a); /O(n) void list_display(polylink h,char o); O(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1