欢迎来到天天文库
浏览记录
ID:30884926
大小:147.50 KB
页数:16页
时间:2019-01-03
《数据结构串的基本操作及应用实验报告》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验日期2010.5.10教师签字成绩【实验名称】第四章串的基本操作及应用【实验目的】1、熟悉将算法转换成程序代码的过程。2、了解串的逻辑结构特性,熟练掌握串顺序存储结构的C语言描述方法。3、熟练掌握串的基本操作:求长度、串的连接、插入、删除等,掌握串的存取特性。【实验原理】1.申可以可以有三种存储方式,分别为顺序存储、堆分配存储、链式存储,串的基木操作在这三种存储方式下操作。2.串的模式匹配KMP算法在每一趟匹配过程中出现字符不等时,不需回溯指针,而是利用已经得到的部分匹配结果的结果将模式向右滑动尽可能远的一段距离,继续进行比较。【实验内容】1.串的顺序
2、存储表示及基本操作(插入、删除、求串长、合并连接串.求子串.串比较等)#include#include#include#include#defineSIZE20structHString{charch[SIZE];intlength;};voidStrInsert(HString&s,intpos,HStringt){inti,j;if(poss.length-!-1)cout«nERROR!if(t.length){for(i=s.length-1;i>=p
3、os-li)s.ch[i+t.length]=s.ch[i];for(j=0;j<=t.length-1;j++)s.ch[pos-l+j]=t.ch[j];s.length+=t.length;}}voidStrDelete(HString&s,intpos.intlen){inti;intv=pos-l;if(poss.lengthlllen<0lllen>s.length-pos4-1)cout«'rERROR!";for(i=pos+len-1;i<=s.length-1;i++)s.ch[v++]=s.ch[i];s.length
4、-=len;}voidStrAssign(HString&t,charchars[]){inti;char*c;for(i=0,c=chars;*c;++i,++c);{t」ength=0;}else{for(intj=0;j
5、rn(int)(t.ch[i]-s.ch[i]);}returns.length・t.length;}voidConcat(HString&t,HStringsl,HStrings2){inti=sl.length+s2.length;for(i=0;i6、pos^intlen){if(poss.lengthlllens.length-pos+l){cout«"ERROR!"«endl;return0;}if(!len){sub」ength=0;}else{inti=len;for(i=0;i7、s[20];do{coutvv”选择您要进行的串的基本操作:n«endl;coutvvT.插入H«endl«n2.删除”vvendlvv”3•串连结H«endl«n4.取子串n«endl«H5.串比较,,«endl«,,6.求串长,,«endl«,,7.结束"«endl;cin»i;switch(i){case1:{HStrings,t;intpos;cout«H请输入串s:”;cin»s.ch;StrAssign(s,s.ch);cout«endl;cout«H请输入要插入的串t:n;cin»t.ch;StrAssign(t,t.ch);cout«end8、l;cout«"请输入你所要插入的位置:”;cin»pos;Str
6、pos^intlen){if(poss.lengthlllens.length-pos+l){cout«"ERROR!"«endl;return0;}if(!len){sub」ength=0;}else{inti=len;for(i=0;i7、s[20];do{coutvv”选择您要进行的串的基本操作:n«endl;coutvvT.插入H«endl«n2.删除”vvendlvv”3•串连结H«endl«n4.取子串n«endl«H5.串比较,,«endl«,,6.求串长,,«endl«,,7.结束"«endl;cin»i;switch(i){case1:{HStrings,t;intpos;cout«H请输入串s:”;cin»s.ch;StrAssign(s,s.ch);cout«endl;cout«H请输入要插入的串t:n;cin»t.ch;StrAssign(t,t.ch);cout«end8、l;cout«"请输入你所要插入的位置:”;cin»pos;Str
7、s[20];do{coutvv”选择您要进行的串的基本操作:n«endl;coutvvT.插入H«endl«n2.删除”vvendlvv”3•串连结H«endl«n4.取子串n«endl«H5.串比较,,«endl«,,6.求串长,,«endl«,,7.结束"«endl;cin»i;switch(i){case1:{HStrings,t;intpos;cout«H请输入串s:”;cin»s.ch;StrAssign(s,s.ch);cout«endl;cout«H请输入要插入的串t:n;cin»t.ch;StrAssign(t,t.ch);cout«end
8、l;cout«"请输入你所要插入的位置:”;cin»pos;Str
此文档下载收益归作者所有