数据结构实验报告-串_第1页
数据结构实验报告-串_第2页
数据结构实验报告-串_第3页
数据结构实验报告-串_第4页
数据结构实验报告-串_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、-实验四串【实验目的】1、掌握串的存储表示及根本操作;2、掌握串的两种模式匹配算法:BF和KMP。3、了解串的应用。【实验学时】2学时【实验预习】答复以下问题:1、串和子串的定义串的定义:串是由零个或多个任意字符组成的有限序列。子串的定义:串中任意连续字符组成的子序列称为该串的子串。2、串的模式匹配串的模式匹配即子串定位是一种重要的串运算。设s和t是给定的两个串,从主串s的第start个字符开场查找等于子串t的过程称为模式匹配,如果在S中找到等于t的子串,则称匹配成功,函数返回t在s中首次出现的存储位置或序号;否则,匹配失败,返回0。【实验容和要求】1、按照要求完成程序e*p4_1.c,实现串

2、的相关操作。调试并运行如下测试数据给出运行结果: 求"This is a boy的串长; 比拟abc3和"abcde";表示空格 比拟english和"student"; 比拟abc和"abc"; 截取串white,起始2,长度2; 截取串white,起始1,长度7; 截取串white,起始6,长度2; 连接串asddffgh和12344;#include<stdio.h>#include<string.h>#define MA*SIZE 100#define ERROR 0#define OK 1/

3、*串的定长顺序存储表示*/typedef struct char dataMA*SIZE; int length; SqString;int strInit(SqString *s); /*初始化串*/int strCreate(SqString *s); /*生成一个串*/int strLength(SqString *s); /*求串的长度*/int strpare(SqString *s1,SqString *s2); /*两个串的比拟*/int subString(SqString *sub,SqString *s,int pos,int len); /*求子串*/int strCon

4、cat(SqString *t,SqString *s1,SqString *s2); /*两个串的连接*/*初始化串*/int strInit(SqString *s) s->length=0; s->data0='0' return OK;/*strInit*/*生成一个串*/int strCreate(SqString *s) printf("input string :"); gets(s->data); s->length=strlen(s->data); return OK;/*strCreate*/*1-求串的长度

5、*/int strLength(SqString *s) return s->length;/*strLength*/*2-两个串的比拟,S1>S2返回>0,s1<s2返回<0,s1=s2返回0*/int strpare(SqString *s1,SqString *s2) int i; for(i=0;i<s1->length&&i<s2->length;i+) if(s1->datai>s2->datai) return 1; if(s1->datai<s2->datai) retur

6、n -1; return 0;/*strpare*/*3-求子串,sub为返回的子串,pos为子串的起始位置,len为子串的长度*/int subString(SqString *sub,SqString *s,int pos,int len) int i; if(pos<1|pos>s->length|len<0|len>s->length-pos+1) return ERROR; sub->length=0; for(i=0;i<len;i+) sub->datai=s->datai+pos-1; sub->length+;

7、 sub->datai='0' return OK;/*subString*/*4-两个串连接,s2连接在s1后,连接后的结果串放在t中*/int strConcat(SqString *t,SqString *s1,SqString *s2) int i=0,j=0; while(i<s1->length) t->datai=s1->datai; i+; while(j<s2->length) t->datai+=s2->dataj+; t->datai='0' t->length=s1->

8、;length+s2->length; return OK;/*strConcat*/int main() int n,k,pos,len; SqString s,t,*; do printf("n -String- n"); printf(" 1. strLentghn"); printf(" 2. strparen"); printf(" 3. subStringn"); printf(" 4. strConcatn"); printf(" 0. E*ITn");

9、printf("n -String-n"); printf("ninput choice:"); scanf("%d",&n); getchar(); switch(n) case 1: printf("n*show strLength*n"); strCreate(&s); printf("strLength is %dn",strLength(&s); break; case 2: printf("n*show strpare*n"); strCr

10、eate(&s); strCreate(&t); k=strpare(&s,&t); /*5-调用串比拟函数比拟s,t*/ if(k=0) printf("two string equal!n"); else if(k<0) printf("first string<second string!n"); else printf("first string>second string!n"); break; case 3: printf("n*show subString*n&

11、quot;); strCreate(&s); printf("input substring pos,len:"); scanf("%d,%d",&pos,&len); if(subString(&t,&s,pos,len) printf("subString is %sn",t.data); else printf("pos or len ERROR!n"); break; case 4: printf("n*show subConcat*n"); st

12、rCreate(&s); strCreate(&t); if(strConcat(&*,&s,&t) /*6-调用串连接函数连接s&t*/ printf("Concat string is %s",*.data); else printf("Concat ERROR!n"); break; case 0: e*it(0); default: break; while(n); return 0;2、按照要求完成程序e*p4_2.c,实现BF&KMP串的模式匹配算法。调试及测试数据并给出结果: 应用BF

13、算法求子串JING在主串BEIJING中的位置,测试起始位置分别为1和5的情况; 应用KMP算法求子串abaabcac在主串acabaabaabcacaabc中的位置,测试起始位置分别为1,10的情况,并写出子串的ne*t值;e*p4_2.c局部代码如下:#include<stdio.h>#include<string.h>#define MA*SIZE 100#define ERROR 0#define OK 1/*串的定长顺序存储表示*/typedef struct char dataMA*SIZE; int length; SqString;int strCrea

14、te(SqString *s);int inde*Bf(SqString *s,SqString *t,int pos); /*串的模式匹配BF*/void getNe*t(SqString *t,int ne*t); /*KMP求ne*t值*/int inde*Kmp(SqString *s,SqString *t,int start,int ne*t); /*串的模式匹配KMP*/*生成一个串*/int strCreate(SqString *s) printf("input string :"); gets(s->data); s->length=strl

15、en(s->data); return OK;/*strCreate*/*1-串的模式匹配BF*/int inde*Bf(SqString *s,SqString *t,int pos) int i=pos-1,j=0; while(i<s->length&&j<t->length) if(s->datai=t->dataj) i+; j+; else i=i-j+1; j=0; if(j>=t->length) return i-t->length+1; else return 0; /*inde*_bf*/*2-K

16、MP求ne*t值*/void getNe*t(SqString *t,int ne*t) int i=0,j=-1; ne*t0=-1; while(i<t->length) if(j=-1)|(t->datai=t->dataj) j+; i+; ne*ti=j; else j=ne*tj; /*getNe*t*/*3-KMP模式匹配*/int inde*Kmp(SqString *s,SqString *t,int start,int ne*t) int i=start-1,j=0; while(i<s->length&&j<t-&

17、gt;length) if(j=-1|s->datai=t->dataj) i+; j+; else j=ne*tj; if(j>=t->length) return i-t->length+1; else return 0; /*inde*_kmp*/int main() int n,i,pos,ne*tMA*SIZE; SqString s,t; do printf("n -String- n"); printf(" 1. Inde*_BFn"); printf(" 2. INde*_KMPn");

18、printf(" 0. E*ITn"); printf("n -String-n"); printf("ninput choice:"); scanf("%d",&n); getchar(); switch(n) case 1: printf("n*show Inde*_BF*n"); printf(" s:"); strCreate(&s); printf(" t:"); strCreate(&t); printf("input start position:"); scan

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论