1、数据结构课程设计文档数据结构课程设计题目一、设计目的1.加强对已学知识的巩固。2.会灵活运用所学知识,解决具体问题。3.对于前面部分实验内容若觉得知识量较少同学,可进行必要的补充调整。二、设计题目(三选二)必做题:1、 利用栈与函数调用关系,设计一个算术表达式的求值程序。要求:(1)算术表达式中有括号、函数。 (2)转换为后缀表达式求解。2、当输入一棵二叉树的前序序列和中序序列(或后序序列和中序序列),请设计自动生成二叉树的程序。 要求:在程序中以简单图形的方式输出该二叉树。2、 对一个项目数是n(10n1)个连续相等整数,在压缩数组中存入C和这个整数。(2)一个不连续相等的整数段,如有C(C
2、1)个整数,其中每个整数与其相邻的整数不等,在压缩文件中存入-C和这C个整数。例如,原数组为2 2 2 4 4 4 8 3 8 1 1 1 1 2 1 则它的压缩数组为3 2 3 4 -3 8 3 8 4 1 -2 2 14 找最长相同单字从给定的两个由英文单字(词)组成的字符串s和t中,找出其中都包含的最长的相同单字(同一字母的大小写视作不同字符)。约定单字全由英文字母组成,单字之间由一个或多个空白符分隔。 提示:可自左至右顺序扫视字符串s,逐个找出单字(单字开始位置和单字长度),当该单字的长度比已找到的单字更长时,就从头至尾扫视字符串t,在从t中找出与该单字长度相等、字符相同的单字后,登录
3、该单字的开始位置和长度,并回到s,在其中找一个更长的单字,上述寻找过程直至字符串s扫视结束,最后输出找到的单字。5 寻找长整数设A的个位数a0为指定的数p(取值分别为2,3,4,9,)。若将A的个位数字移到其他各位数字之前,则其数值为原数值的A的p倍。例如,p为4,则A为102564(各位数字分别存入a5,a4, ,a0之中),有102564*4=410256。 寻找从 a0=p出发,用p乘已确定的位数的数值可推出其前1的数字,逐位进行,直到用P乘an-1等于a0,递推计算结束,A即为:an-1an-2 a0数据结构与算法课程设计报告专业班级:学号:姓名:日期:实例 报数问题1问题描述设有n个
4、人围坐一圈并按顺时针方向1-n循环报数。若从第1个人开始报数,凡是报的数字是7的倍数(如14、49)或其中包含7(如17、71),则此人出圈,再从他的下一个人继续报数,如此进行下去,直到只有一个人为止。设计程序,要找到那个能留到最后的人的原始位置号与最后应该报的数字。2算法分析与设计3参考程序4运行结果图 是由本程序计算出的3个传教士和3个野人的安全渡河方案。图14-1 野人渡河问题的解答5总结与体会 附:野人渡河程序3个野人和3个传教士来到河边,打算乘一只船从右岸到左岸去。该船的负载能力为两人。在任何时候,如果野人人数超过传教士人数,野人就会把传教士吃掉。他们怎样才能用这条船安全地把所有人都
5、渡过河去呢?这个问题还可以拓展为m个野人和n个传教士,而船一次可以装下r个人的情况。#include #include #define maxloop 100 /最大层数#define pristnum 3 /传教士默认值#define slavenum 3 /野人默认值struct SPQ int sr,pr; /船运行一个来回后河右岸的野人、传教士的人数 int sl,pl; /船运行一个来回后河左岸的野人、传教士的人数 int ssr,spr; /回来(由左向右时)船上的人数 int sst,spt; /去时(由右向左时)船上的人数 int loop; /本结点所在的层数 struct
6、SPQ *upnode ,*nextnode; /本结点的父结点和同层的下一个结点的地址 spq; int loopnum; /记录总的扩展次数int openednum; /记录已扩展节点个数int unopenednum; /记录待扩展节点个数int resultnum;struct SPQ *opened;struct SPQ *oend;struct SPQ *unopened; struct SPQ *uend;struct SPQ *result;void initiate( );void releasemem( );void showresult( );void addtoope
7、ned(struct SPQ *ntx);int search( );void goon( );int stretch(struct SPQ* ntx);void recorder( );void main( ) int flag; /标记扩展是否成功 for( ; ; ) initiate( );flag = search ( );if(flag = 1) recorder( );releasemem( );showresult( );goon( );else printf(无法找到符合条件的解);releasemem( );goon( );void initiate( ) int x;ch
8、ar choice;uend = unopened = (struct SPQ*)malloc(sizeof(spq);if(uend=NULL) printf(n内存不够! n); exit(0);unopenednum=1;openednum=0;unopened - upnode = unopened; /保存父结点的地址以成链表unopened - nextnode = unopened;unopened - sr = slavenum;unopened - pr = pristnum;unopened - sl = 0;unopened - pl = 0;unopened - sst
9、 = 0;unopened - spt = 0;unopened - ssr = 0;unopened - spr = 0;unopened - loop = 0;printf(题目:设有n个传教士和m个野人来到河边,打算乘一只船从右岸到左岸去。n);printf(该船的负载能力为两人。在任何时候,如果野人人数超过传教士人数,野人n);printf(就会把传教士吃掉。他们怎样才能用这条船安全地把所有人都渡过河去呢-n);printf(n默认的n、m值皆为3n);for(; ;)printf(n是否修改?(Y/N):); scanf(%s,&choice); choice=toupper(cho
10、ice); if(choice=Y)printf(n请输入传教士人数:); for(;) scanf(%d,&x); if(x0) unopened -pr = x; break; else printf(n输入值应大于0!n请重新输入); printf(请输入野人人数:); for(;) scanf(%d,&x); if(x0) unopened - sr = x; break; else printf(n输入值应大于0!n请重新输入); break; if(choice=N) break; int search( ) int flag;struct SPQ *ntx; /提供将要扩展的结点
11、的指针for( ; ; ) ntx = unopened; /从待扩展链表中提取最前面的一个 if(ntx-loop = maxloop) return 0; addtoopened(ntx); /将ntx加入已扩展链表,并将这个节点从待扩展链表中去掉 flag = stretch(ntx); /对ntx进行扩展, 返回-1,0,1 if(flag = 1) return 1; int stretch(struct SPQ *ntx) int fsr , fpr ; /在右岸上的人数int fsl , fpl ; /在左岸上的人数int sst , spt ; /出发时在船上的人数int ss
12、r , spr ; /返回时船上的人数struct SPQ *newnode;for (sst = 0 ; sst sr;fpr = ntx - pr; fsl = ntx - sl; fpl = ntx - pl; if (sst = fsr) & ( 2 - sst) upnode = ntx; /保存父结点的地址以成链表 newnode - nextnode = NULL; newnode - sr = 0; newnode - pr = 0; newnode - sl = opened - sr; newnode - pl = opened - pr; newnode - sst =
13、sst; newnode - spt = spt; newnode - ssr = 0; newnode - spr = 0; newnode - loop = ntx - loop + 1; oend - nextnode = newnode; oend = newnode; openednum+; return 1; else if (fpr - fsr) * fpr = 0) /判断是否满足传教士人数大于或等于野人人数 fsl = fsl + sst; fpl = fpl + spt; for (ssr = 0 ; ssr = 1 ; ssr+) /返回 int ffsl , ffpl;
14、 if (ssr = fsl) & (1 - ssr) = 0) /若符合条件则分配内存并赋值 int ffsr , ffpr; ffsr = fsr + ssr; ffpr = fpr + spr; newnode = (struct SPQ*) malloc (sizeof(spq); if ( newnode=NULL) printf(n内存不够!n); exit(0); newnode - upnode = ntx; /保存父结点的地址 newnode - sr = ffsr; newnode - pr = ffpr; newnode - sl = ffsl; newnode - pl
15、 = ffpl; newnode - sst = sst; newnode - spt = spt; newnode - ssr = ssr; newnode - spr = spr; newnode - loop = ntx - loop + 1; uend - nextnode = newnode; uend = newnode; unopenednum+; return 0;void addtoopened(struct SPQ *ntx) unopened = unopened - nextnode;unopenednum-;if ( openednum = 0 ) oend = op
16、ened = ntx;oend - nextnode = ntx;oend = ntx;openednum+;void recorder( ) int i , loop;struct SPQ *newnode;struct SPQ *ntx;loop = oend - loop;ntx = oend;resultnum = 0;for( i = 0 ; i sr = ntx - sr; newnode - pr = ntx - pr; newnode - sl = ntx - sl; newnode - pl = ntx - pl; newnode - sst = ntx - sst; new
17、node - spt = ntx - spt; newnode - ssr = ntx - ssr; newnode - spr = ntx - spr; newnode - nextnode = NULL; ntx = ntx - upnode; if ( i = 0) result = newnode; newnode - nextnode = result; result = newnode; resultnum+; void releasemem( ) int i;struct SPQ* nodefree;for ( i = 1 ; i nextnode;free(nodefree);
18、for ( i = 0 ; i nextnode;free(nodefree);void showresult( )int i; int fsr , fpr ; /在右岸上的人数int fsl , fpl ; /在左岸上的人数struct SPQ* nodefree;printf(n);printf(%d个传教士和%d个野人渡河:, result - pr,result - sr);for ( i = 1 ; i nextnode; free(nodefree); printf(nnt左岸人数 船上人数及方向 右岸人数n); printf(第%d轮n,i); fpl = result - pl
19、 - result - spt + result - spr; fpr = result - pr - result - spr; fsl = result - sl - result - sst + result - ssr; fsr = result - sr - result - ssr; printf(传教士%8d%8dt spt,fpr); printf(野 人%8d%8dt sst,fsr); printf(传教士%8d%8dt-t%8dn,result - pl,result - spr,result - pr - result - spr); printf(野 人%8d%8dt-t%8dn,result - sl,result - ssr,result - sr - result - ssr); printf(n全体传教士和野人全部到达对岸!);free(result);void goon( )char choice;for(;) printf(n是否继续?(Y/N):); scanf (%s , &choice); choice=toupper(choice); if(choice=Y)break; if(choice=N)exit(0);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1