1、数据结构课程设计回文判断范文数据结构课程设计回文判断设计题目:3.4.4 回文判断专业:*姓名:*学号:*日期: .09.11一, 问题描述二, 需求分析三, 概要设计四, 详细设计五, 测试分析六, 源程序清单七, 用户使用手册八, 心得体会一、问题描述判断依次读入的一个以为结束符的字母序列,是否形如“序列1&序列2”模式的字符序列。其中序列1和序列2都不含“&”字符,且序列2是序列1的逆序列。二、需求分析 (1)输入测试数据组数,接着分组输入字符串,以结尾。 (2)输入序列总长不超过 (MAX_N = 10005)/2 个。将序列1先入栈,接着处理序列2,同时出栈判断。 (3)将序列1全部
2、入栈,接着输入序列2,同时出栈判断。 (4)如果序列满足题目要求,则输出“回文序列”;否则,输出“非回文序列”。 (5)测试数据:pal.txt 12a+b&b+aa&ba&a & & & &ab&baabc&bcbc&cba&abab&三、概要设计 (1)数据结构: typedef struct Stack int top,size; char strMAX_N1;使用结构体,内部定义数组模拟栈。top为栈顶指针,指向当前元素的下一个位置,size表示栈内的元素个数。 (2)函数介绍: void st_init(Stack *st); /栈的初始化 bool st_push(Stack *s
3、t,const char *temp); /入栈bool st_top(Stack *st,char *temp); /出栈 (3)程序流程图: +i N Y Y N四、详细设计 主要出栈和入栈函数,能够对边界进行处理,防止栈为空情况。bool st_push(Stack *st,const char *temp) if( st-topst-size ) return false; st-strst-top+ = *temp; return true;bool st_pop(Stack *st) if( st-top=0 ) return false; st-top-; return true
4、;另外还有取栈顶函数。bool st_top(Stack *st,char *temp) if( st-top=0 ) return false; *temp = st-strst-top-1; return true;五、测试分析 (1)测试环境:CodeBlock 10.04 (2)输入过程:因为使用 getchar() 输入,因此有些地方需要谨慎处理。用于getchar() 能够读入任意字符,因此回车和空格之类的特殊字符也作为序列参与判断了。 (3)测试结果: 六、源程序清单#include #include #include const int MAX_N = 10005;typede
5、f struct Stack int top,size; char strMAX_N1;void st_init(Stack *st) st-size=MAX_N1; st-top=0;bool st_push(Stack *st,const char *temp) if( st-topst-size ) return false; st-strst-top+ = *temp; return true;bool st_pop(Stack *st) if( st-top=0 ) return false; st-top-; return true;bool st_top(Stack *st,ch
6、ar *temp) if( st-top=0 ) return false; *temp = st-strst-top-1; return true;int main() char strMAX_N,c; int i,j,cas,len; Stack st; bool flag;/ freopen(pal.txt,r,stdin); printf(请输入测试组数:n); scanf(%d,&cas); getchar(); j=0; while( cas- ) +j; printf(n第 %d 组数据n,j); printf(n请输入数据(字符串1&字符串2):n); for( i=0; 1;
7、 i+ ) stri=getchar(); if( stri= ) stri=0; break; getchar(); flag = true; len = strlen(str); st_init(&st); if( !len&1 ) flag = false; else for( i=0; ilen; i+ ) if( stri=& ) break; st_push(&st,&stri); for( +i; i0 ) flag = false; printf(nCase : %dn,j); if( flag ) printf(回文序列。n); else printf(非回文序列。n); printf(n); printf(输入结束。By changning.huang);/ while( true ); return 0;七、用户使用手册 (1)开始需要输入要测试的组数,这样方便结束,减少一些繁琐的结束判断。 (2)因为定义了文件流,能够用文件方便的进行读写。如使用文件读写,稍微将以下语句注释掉: printf(请输入测试组数:n); printf(n第 %d 组数据n,j); printf(n请输入数据(字符串1&字符串2):n); 当然,不需要的话,注释掉 freopen(pal.txt,r,stdin); 即可。 (3)输入格式依照题目要求即可。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1