利用链表处理多项式.ppt

上传人:牧羊曲112 文档编号:5244196 上传时间:2023-06-18 格式:PPT 页数:33 大小:730.50KB
返回 下载 相关 举报
利用链表处理多项式.ppt_第1页
第1页 / 共33页
利用链表处理多项式.ppt_第2页
第2页 / 共33页
利用链表处理多项式.ppt_第3页
第3页 / 共33页
利用链表处理多项式.ppt_第4页
第4页 / 共33页
利用链表处理多项式.ppt_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《利用链表处理多项式.ppt》由会员分享,可在线阅读,更多相关《利用链表处理多项式.ppt(33页珍藏版)》请在三一办公上搜索。

1、利用链表处理多项式,1.编出制造多项式的函数creatlist();llink creatlist()/制造多项式llink head,p;int code,j,k,guard;printf(请输入多项式项数n);label:k=scanf(%d,printf(按系数-指数的顺序初始化多项式n);printf(输入格式:系数和指数间有一逗号隔开n);head=(llink)malloc(sizeof(node);if(!head)printf(内存分配失败n);exit(1);head-fin=-1;head-tak=-1;head-next=NULL;p=head;,for(j=0;jnex

2、t=(llink)malloc(sizeof(node);if(!p-next)printf(内存分配失败n);exit(1);guard=scanf(%f,%f,p-next-next=NULL;p=p-next;p-next=head;/创建循环链表return head;编出制造多项式函数后,下面编造其他功能函数/计算多项式值的函数,要把头指针传进去double result(llink head)llink p;float x;double sum=0;p=head-next;printf(请输入未知数的值n);scanf(%f,while(p!=head)sum+=(p-tak)*p

3、ow(x,p-fin);p=p-next;return sum;/多项式求导函数,把多项式的头指针传进去qiudao(llink head)llink p,q,r;,p=head-next;r=(llink)malloc(sizeof(node);/设置头结点,为创建循环链表if(!r)printf(内存分配失败n);exit(1);r-fin=-1;r-tak=-1;q=r;q-next=NULL;,while(p!=head)q-next=(llink)malloc(sizeof(node);if(!q-next)printf(内存分配失败n);exit(1);q-next-next=NU

4、LL;q-next-fin=p-fin-1;q-next-tak=(p-fin)*(p-tak);p=p-next;q=q-next;q-next=r;/将链表首尾连接起来,创建循环链表,printf(求导后的多项式是:);q=r-next;while(q!=r)printf(%.2fX%.2f,q-tak,q-fin);q=q-next;if(q-tak0,/多项式求积分函数,要传入多项式头指针传进去jifen(llink head)llink p,q,r;p=head-next;r=(llink)malloc(sizeof(node);if(!r)printf(内存分配失败n);exit(

5、1);r-next=NULL;r-fin=-1;r-tak=-1;q=r,while(p!=head)q-next=(llink)malloc(sizeof(node);if(!q-next)printf(内存分配失败n);exit(1);q-next-fin=p-fin+1;/根据积分运算原则,来为新多项式初始化q-next-tak=p-tak/q-next-fin;p=p-next;q=q-next;q-next=r;q=r;q=q-next;,while(q!=r)printf(%.2fX%.2f,q-tak,q-fin);q=q-next;if(q-tak0,/计算两多项式和的函数的函

6、数llink add(llink plo,llink plp)llink head1,head2,result,before,check,another;head1=plo-next;head2=plp-next;result=(llink)malloc(sizeof(node);/创建头结点,为创建循环链表if(!result)return NULL;result-fin=-1;/头结点初始化result-tak=-1;before=result;/另一同类型指针来造出剩余结点,制造头结点的指针不能改变,以便返回该指针before-next=NULL;,while(plo!=head1|pl

7、p!=head2)/分三种情况,依据指数的大小分类before-next=(llink)malloc(sizeof(node);if(!before-next)printf(内存分配失败n);exit(1);before-next-next=NULL;if(head1-finhead2-fin)before-next-fin=head1-fin;before-next-tak=head1-tak;head1=head1-next;before=before-next;,elseif(head1-finfin)before-next-fin=head2-fin;before-next-tak=h

8、ead2-tak;head2=head2-next;before=before-next;elsebefore-next-fin=head1-fin;before-next-tak=head1-tak+head2-tak;head1=head1-next;head2=head2-next;before=before-next;,before-next=result;/将链表首尾连接起来,创建循环链表check=result-next;another=result-next;while(another-fin!=-1)if(another-fin=check-next-fin)another-t

9、ak+=check-next-tak;check-next=check-next-next;check=check-next;if(check-fin=-1)another=another-next;check=another;return result;,/多项式相减llink minus(llink plo,llink plp)llink head1,head2,result,before,check,another;head1=plo-next;head2=plp-next;result=(llink)malloc(sizeof(node);/创建头结点,为创建循环链表if(!resul

10、t)return NULL;result-fin=-1;/头结点初始化result-tak=-1;before=result;/另一同类型指针来造出剩余结点,制造头结点的指针不能改变,以便返回该指针before-next=NULL;,while(plo!=head1|plp!=head2)/分三种情况,依据指数的大小分类before-next=(llink)malloc(sizeof(node);if(!before-next)printf(内存分配失败n);exit(1);before-next-next=NULL;if(head1-finhead2-fin)before-next-fin=

11、head1-fin;before-next-tak=head1-tak;head1=head1-next;before=before-next;,elseif(head1-finfin)before-next-fin=head2-fin;before-next-tak=-head2-tak;head2=head2-next;before=before-next;elsebefore-next-fin=head1-fin;before-next-tak=head1-tak-head2-tak;head1=head1-next;head2=head2-next;before=before-next

12、;before-next=result;/将链表首尾连接起来,创建循环链表check=result-next;another=result-next;,while(another-fin!=-1)if(another-fin=check-next-fin)another-tak+=check-next-tak;check-next=check-next-next;check=check-next;if(check-fin=-1)another=another-next;check=another;return result;,/打印多项式函数putout(llink head)llink pt

13、r;ptr=head-next;while(ptr!=head)printf(%.2fX%.2f,ptr-tak,ptr-fin);ptr=ptr-next;if(ptr-tak0)/头结点初始化时,系数为-1,所以不必在加另一个约束条件printf(+);printf(n);,/多项式相乘函数llink cheng(llink p1,llink p2)llink p,q,s,result,check,another;p=p1-next;q=p2-next;result=(llink)malloc(sizeof(node);if(!result)printf(内存分配失败n);exit(1);

14、result-fin=-1;result-tak=-1;s=result;,while(p!=p1)while(q!=p2)s-next=(llink)malloc(sizeof(node);if(!s-next)printf(内存分配失败n);exit(1);s-next-tak=p-tak*q-tak;s-next-fin=p-fin+q-fin;q=q-next;s=s-next;p=p-next;q=p2-next;s-next=result;check=result-next;another=result-next;,while(another-fin!=-1)/此处校验相同指数项并

15、合并他们if(another-fin=check-next-fin)another-tak+=check-next-tak;check-next=check-next-next;check=check-next;if(check-fin=-1)another=another-next;check=another;return result;,/释放内存函数freelist(llink dsp)llink ptr,prl;prl=dsp;ptr=NULL;prl=prl-next;while(ptr!=dsp)ptr=prl;prl=prl-next;free(ptr);,/功能实现函数func

16、tion(llink head1,llink head2)llink head3,head4;double sum1,sum2;int king;printf(按如下列数字进行功能选择:n);printf(1.打出两个多项式t2.两多项式相加n);printf(3.输入未知数,计算两多项式的数值,并比较大小n);printf(4.对两多项式求导t5.对两多项式求积分n);printf(6.求两多项式的积t7.退出程序,释放内存n);printf(8.重新输入多项式,重新进行处理n);printf(9.计算多项式的差n);,for(;)scanf(%d,printf(积分后的多项式:);prin

17、tf(第一个多项式积分结果n);jifen(head1);printf(第二个多项式积分结果n);jifen(head2);break;case 4:printf(求导后多项式n);printf(第一个多项式求导结果n);qiudao(head1);printf(第二个多项式求导结果n);qiudao(head2);break;,case 3:printf(请输入第一个多项式的未知数数值n);sum1=result(head1);printf(请输入第二个多项式的未知数数值n);sum2=result(head2);printf(第一个多项式和第二个多项式的值分别为:);printf(%.2f

18、t%.2fn,sum1,sum2);if(sum1sum2)printf(f1f2n);else if(sum1sum2)printf(f1f2n);elseprintf(f1=f2n);break;,case 7:freelist(head1);freelist(head2);exit(1);case 6:printf(两多项式的积为:n);head3=cheng(head1,head2);putout(head3);break;case 8:freelist(head1);freelist(head2);head1=creatlist();head2=creatlist();functio

19、n(head1,head2);break;,case 9:printf(多项式之差为:n);head4=minus(head1,head2);putout(head4);break;printf(还可继续进行其他功能n);,/主程序main()llink head1,head2;head1=creatlist();/创建第一个多项式if(!head1)printf(内存分配失败n);exit(1);printf(请为第二个多项式初始化n);head2=creatlist();/创建第二个多项式if(!head2)/检验内存是否分配成功printf(内存分配失败n);exit(1);function(head1,head2);/实现函数功能,谢 谢,

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号