/******************************************************************************
程序功能:串的顺序存储实现
时间:2013年4月21日
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#define ERROR 0
#define OVERFLOW -2
#define OK 1
#define FALSE 0
#define TRUE 1
#define MAXSTRLEN 255
typedef int Status;
typedef unsigned char SString[MAXSTRLEN + 1];
Status StrAssign(SString &T,const char *chars)
{
//生成一个其值为chars的串T
if(strlen(chars) > MAXSTRLEN)
return ERROR;
else
{
T[0] = strlen(chars);
for(int index = 1; index <= T[0]; ++index)
{
T[index] = chars[index-1];
}
}
return OK;
}
Status StrCopy(SString &T, const SString &S)
{
//串S复制到串T
for(int index = 0; index <= S[0]; ++index)
{
T[index] = S[index];
}
return OK;
}
Status StrEmpty(const SString &T)
{
//S为空串,返回TRUE;否则返回FALSE
if(T[0])
return FALSE;
else
return TRUE;
}
Status StrCompare(const SString &S, const SString &T)
{
//串S和T存在
//若S>T,则返回>0;若S=T,则返回 =0;若S<T,则返回<0
if(T[0] != S[0])
return S[0] - T[0];
else
{
int index = 1;
for( ; index <= T[0]; ++index)
{
if(S[index] != T[index])
{
return S[index]-T[index];
}
}
return S[index] - T[index];
}
}
Status StrLength(const SString &T)
{
//返回串长
return T[0];
}
Status ClearString(SString &S)
{
S[0] = 0;
return OK;
}
Status Concat(SString &T, const SString &S1, const SString &S2)
{
//用T返回由S1和S2联接而成的新串。若未截断,则返回TRUE,否则返回FALSE.
if(S1[0] + S2[0] <= MAXSTRLEN)
{
T[0] = S1[0] + S2[0];
int index =1;
for(; index <= S1[0]; ++index)
{
T[index] = S1[index];
}
for(index =1; index <= S2[0]; ++index)
{
T[S1[0]+ index] = S2[index];
}
return TRUE;
}
else if(S1[0] <MAXSTRLEN)
{
int index =1;
for(; index <= S1[0]; ++index)
{
T[index] = S1[index];
}
for(index =1; index <= MAXSTRLEN - S1[0]; ++index)
{
T[S1[0]+ index] = S2[index];
}
T[0] = MAXSTRLEN;
return FALSE;
}
else
{
T[0] = S1[0];
for(int index = 1; index <= S1[0]; ++index)
{
T[index] = S1[index];
}
return FALSE;
}
}
Status SubString(SString &Sub, const SString &S, int pos, int len)
{
//串S存在,1 <= pos <= StrLength(S) 且0 <= len <= StrLength(S) - pos +1
//用Sub返回串S的第pos个字符起长度为len的子串
if( pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1)
return ERROR;
else
{
Sub[0] = len;
for(int index = pos; index <= len ; ++index)
{
Sub[index - pos + 1] = S[index];
}
return OK;
}
}
Status Index(const SString &S, const SString &T, int pos )
{
//1 <= pos <= StrLength(S),若主串S中存在和串T相同的子串,则返回它在主串
//S中第pos个字符之后第一次出现的位置;否则函数值为0
if(pos < 1 || pos > S[0])
return ERROR;
else
{
for(int index = pos; index <= S[0] - T[0] + 1; ++index)
{
int i = 1;
for(; i <= T[0]; ++i)
{
if(S[index + i -1] != T[i])
break;
}
if(i == T[0] + 1)
return index;
}
return ERROR;
}
}
Status DisStr(const SString &S)
{
for(int index = 1; index <= S[0]; ++index)
{
printf("%c",S[index]);
}
return OK;
}
int main()
{
SString T1,T2,T3,T4;
char *chars1 = "shanquanliquanang!";
char *chars2 = "quan";
StrAssign(T1,chars1);
StrAssign(T2,chars2);
printf("%d\n",Index(T1,T2,5));
DisStr(T1);
printf("\n");
DisStr(T2);
printf("\n");
return 0;
}
数据结构:串的顺序存储实现
最新推荐文章于 2022-10-19 19:52:32 发布