数据结构串实践报告Word格式文档下载.docx
《数据结构串实践报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构串实践报告Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。
![数据结构串实践报告Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-11/23/838dfc1e-9d44-472c-a89f-cd74d4b8b010/838dfc1e-9d44-472c-a89f-cd74d4b8b0101.gif)
利用所学字符串基本运算的函数得到结果串为:
s3=’caabcbcaaaaaacaaaaaa’。
三、实验用设备仪器及材料
计算机
四、实验原理及接线
五、实验操作步骤
//main4-1.cpp检验bo4-1.cpp的主程序
//c1.h(程序名)
#include<
string.h>
ctype.h>
malloc.h>
//malloc()等
limits.h>
//INT_MAX等
stdio.h>
//EOF(=^Z或F6),NULL
stdlib.h>
//atoi()
io.h>
//eof()
math.h>
//floor(),ceil(),abs()
process.h>
//exit()
//#include<
iostream.h>
//cout,cin
//函数结果状态代码
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
//#defineOVERFLOW-2因为在math.h中已定义OVERFLOW的值为3,故去掉此行
typedefintStatus;
//Status是函数的类型,其值是函数结果状态代码,如OK等
typedefintBoolean;
//Boolean是布尔类型,其值是TRUE或FALSE
//c4-1.h串的定长顺序存储表示
#defineMAXSTRLEN30//用户可在255以内定义最大串长(1个字节)
typedefcharSString[MAXSTRLEN+1];
//0号单元存放串的长度
//bo4-1.cpp串采用定长顺序存储结构(由c4-1.h定义)的基本操作(14个)
//SString是数组,故不需引用类型。
此基本操作包括算法4.2,4.3,4.5
StatusStrAssign(SString&
T,char*chars)
{
///生成一个其值等于chars的串T
inti;
if(strlen(chars)>
MAXSTRLEN)
returnERROR;
else
{
T[0]=strlen(chars);
for(i=1;
i<
=T[0];
i++)
T[i]=*(chars+i-1);
returnOK;
}
}
StatusStrCopy(SStringT,SStringS)
///由串S复制得串T
for(i=0;
=S[0];
T[i]=S[i];
StatusStrEmpty(SStringS)
///若S为空串,则返回TRUE,否则返回FALSE
if(S[0]==0)
returnTRUE;
returnFALSE;
intStrCompare(SStringS,SStringT)
///初始条件:
串S和T存在
///操作结果:
若S>
T,则返回值>
0;
若S=T,则返回值=0;
若S<
T,则返回值<
=S[0]&
&
i<
++i)
if(S[i]!
=T[i])
returnS[i]-T[i];
returnS[0]-T[0];
intStrLength(SStringS)
///返回串的元素个数
returnS[0];
StatusClearString(SStringS)
串S存在。
操作结果:
将S清为空串
S[0]=0;
//令串长为零
StatusConcat(SString&
T,SStringS1,SStringS2)//算法4.2改
//用T返回S1和S2联接而成的新串。
若未截断,则返回TRUE,否则FALSE
if(S1[0]+S2[0]<
=MAXSTRLEN)
//未截断
=S1[0];
T[i]=S1[i];
=S2[0];
T[S1[0]+i]=S2[i];
T[0]=S1[0]+S2[0];
//截断S2
=MAXSTRLEN-S1[0];
T[0]=MAXSTRLEN;
StatusSubString(SString&
Sub,SStringS,intpos,intlen)
///用Sub返回串S的第pos个字符起长度为len的子串。
算法4.3
if(pos<
1||pos>
S[0]||len<
0||len>
S[0]-pos+1)
=len;
Sub[i]=S[pos+i-1];
Sub[0]=len;
intIndex(SStringS,SStringT,intpos)
///返回子串T在主串S中第pos个字符之后的位置。
若不存在,则函数值为0。
///其中,T非空,1≤pos≤StrLength(S)。
算法4.5
inti,j;
if(1<
=pos&
pos<
=S[0])
i=pos;
j=1;
while(i<
j<
=T[0])
if(S[i]==T[j])//继续比较后继字符
++i;
++j;
else//指针后退重新开始匹配
i=i-j+2;
if(j>
T[0])
returni-T[0];
return0;
StatusStrInsert(SStringS,intpos,SStringT)
//初始条件:
串S和T存在,1≤pos≤StrLength(S)+1
//操作结果:
在串S的第pos个字符之前插入串T。
完全插入返回TRUE,部分插入返回FALSE
S[0]+1)
if(S[0]+T[0]<
///完全插入
for(i=S[0];
i>
=pos;
i--)
S[i+T[0]]=S[i];
for(i=pos;
pos+T[0];
S[i]=T[i-pos+1];
S[0]=S[0]+T[0];
///部分插入
for(i=MAXSTRLEN;
S[i]=S[i-T[0]];
S[0]=MAXSTRLEN;
StatusStrDelete(SStringS,intpos,intlen)
串S存在,1≤pos≤StrLength(S)-len+1
从串S中删除第pos个字符起长度为len的子串
S[0]-len+1||len<
0)
for(i=pos+len;
S[i-len]=S[i];
S[0]-=len;
StatusReplace(SStringS,SStringT,SStringV)
串S,T和V存在,T是非空串(此函数与串的存储结构无关)
用V替换主串S中出现的所有与T相等的不重叠的子串
inti=1;
//从串S的第一个字符起查找串T
if(StrEmpty(T))//T是空串
do
i=Index(S,T,i);
//结果i为从上一个i之后找到的子串T的位置
if(i)//串S中存在串T
StrDelete(S,i,StrLength(T));
//删除该串T
StrInsert(S,i,V);
//在原串T的位置插入串V
i+=StrLength(V);
//在插入的串V后面继续查找串T
while(i);
voidDestroyString()
//由于SString是定长类型,无法销毁
voidStrPrint(SStringT)
//输出字符串T。
另加
printf("
%c"
T[i]);
\n"
);
//printf("
intmain()
charg='
1'
;
Statusk;
chars,c[MAXSTRLEN+1];
SStringt,s1,s2;
请输入串s1:
"
gets(c);
k=StrAssign(s1,c);
if(!
k)
串长超过MAXSTRLEN(=%d)\n"
MAXSTRLEN);
exit(0);
串长为%d串空否?
%d(1:
是0:
否)\n"
StrLength(s1),StrEmpty(s1));
StrCopy(s2,s1);
拷贝s1生成的串为:
StrPrint(s2);
请输入串s2:
k=StrAssign(s2,c);
串长超过MAXSTRLEN(%d)\n"
i=StrCompare(s1,s2);
if(i<
s='
<
'
elseif(i==0)
='
>
串s1%c串s2\n"
s);
k=Concat(t,s1,s2);
串s1联接串s2得到的串t为:
StrPrint(t);
if(k==FALSE)
串t有截断\n"
ClearString(s1);
清为空串后,串s1为:
StrPrint(s1);
求串t的子串,请输入子串的起始位置,子串长度:
scanf("
%d,%d"
&
i,&
j);
k=SubString(s2,t,i,j);
if(k)
子串s2为:
从串t的第pos个字符起,删除len个字符,请输入pos,len:
StrDelete(t,i,j);
删除后的串t为:
i=StrLength(s2)/2;
StrInsert(s2,i,t);
在串s2的第%d个字符之前插入串t后,串s2为:
i);
i=Index(s2,t,1);
s2的第%d个字母起和t第一次匹配\n"
SubString(t,s2,1,1);
串t为:
"
Concat(s1,t,t);
串s1为:
Replace(s2,t,s1);
用串s1取代串s2中和串t相同的不重叠的串后,串s2为:
while(g!
0'
)scanf("
g);
//由于SString是