本文只有代码,介绍了有关栈的基本操作。
已经过调试没有很大问题。
如有错误,还请批评指正。
一、串的实现和表示:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int Status;
#define Size 50
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
//串的表示
typedef struct
{
char* ch;//若是非空串,则按串长分配存储区,否则ch为NULL
int length;//串长度
}HString;
二、串的基本操作:
Status StrAssign(HString* T,const char* chars);//生成一个其值等于串常量chars的串
//StrAssign(&T, chars)
int StrLenth(HString S);//返回串的元素个数,即串的长度StrLenth(S)
int StrCompare(HString T, HString S);//比较两个串的大小StrCompare(T, S)
Status ClearString(HString* S);//清空串ClearString(&S)
Status Concat(HString* T, HString S1, HString S2);//将S1、S2连接Concat(&T, S1, S2)
Status SubString(HString* Sub, HString S, int pos, int len);//返回串S的第pos个字符起长度为
//len的子串SubString(&Sub, S, pos, len)
void StrCopy(HString* T, HString* S);//拷贝字符串StrCopy(&T, &S)
Status StrEmpty(HString S);//判断是否空串StrEmpty(S)
int Index(HString* S, HString T, int pos);//返回字串在主串中第pos个字符之后第一次出现的位置
//Index(&S, T, pos)
Status Replace(HString* S, HString T, HString V, int pos);//用V替换主串S中从pos开始的与T相
//等且不重叠的第一个子串Replace(&S, T, V, pos)
Status ReplaceAll(HString* S, HString T, HString V);//用V替换主串S中所有与T相等的不重叠的子
//串ReplaceAll(&S, T, V)
Status StrInsert(HString* S, int pos, HString T);//在串S的第pos个字符之前插入T串
//StrInsert(&S, pos, T)
Status StrDelete(HString* S, int pos, int len);//从串的第pos个字符起删除长度为len的子串
//StrDelete(&S, pos, len)
Status DestoryString(HString* S);//销毁串DestoryString(&S)
Status StrPrint(HString S);//输出串StrPrint(S)
1、
/*---------------------------------------------------------------------------------------
功能:生成一个其值等于串常量chars的串
参数:1、串指针 2、串常量
输出:OK、ERROR
*/
//生成一个其值等于串常量chars的串StrAssign(T, chars)
Status StrAssign(HString* T, const char* chars)
{
if (T->ch)//释放T原有的空间
{
free(T->ch);
T->ch = NULL;
}
char* c;
int len = 0;
for (len = 0, c = chars; *c; ++len, ++c);
if (len)
{
if (T->ch = (char*)malloc(sizeof(char) * Size))
{
for (int i = 0; i < len; ++i)
{
T->ch[i] = chars[i];
}
T->length = len;
}
else
{
return ERROR;
}
}
else
{
T->ch = NULL;
T->length = 0;
}
return OK;
}
2、
/*---------------------------------------------------------------------------------------
功能:返回串的元素个数,即串的长度
参数:1、串
输出:长度
*/
//返回串的元素个数,即串的长度StrLenth(S)
int StrLenth(HString S)
{
return S.length;
}
3、
/*---------------------------------------------------------------------------------------
功能:比较两个串的大小
参数:1、串 2、串
输出:整型
*/
//比较两个串的大小StrCompare(S, T)
int StrCompare(HString S, HString T)
{
for (int i = 0; i < S.length && i < T.length; ++i)
{
if (S.ch[i] != T.ch[i])
{
return S.ch[i] - T.ch[i];
}
}
return S.length - T.length;
}
4、
/*---------------------------------------------------------------------------------------
功能:清空串
参数:1、串指针
输出:OK、ERROR
*/
//清空串ClearString(&S)
Status ClearString(HString* S)
{
if (S->ch)
{
free(S->ch);
S->ch = NULL;
}
S->length = 0;
return OK;
}
5、
/*---------------------------------------------------------------------------------------
功能:将S1、S2连接
参数:1、串指针 2、串 3、串
输出:OK、ERROR
*/
//将S1、S2连接Concat(&T, S1, S2)
Status Concat(HString* T, HString S1, HString S2)
{
if (T->ch)//释放旧空间
{
free(T->ch);
T->ch = NULL;
}
if (!(T->ch = (char*)malloc(sizeof(char) * Size)))
{
return ERROR;
}
int i = 0;
for (i = 0; i < S1.length; ++i)
{
T->ch[i] = S1.ch[i];
}
for (int j = 0; i < S1.length + S2.length && j < S2.length; ++j, ++i)
{
T->ch[i] = S2.ch[j];
}
T->length = S1.length + S2.length;
return OK;
}
6、
/*---------------------------------------------------------------------------------------
功能:返回串S的第pos个字符起长度为len的子串
参数:1、串 2、位置 3、长度
输出:串
*/
//返回串S的第pos个字符起长度为len的子串SubString(&Sub, S, pos, len)
Status SubString(HString* Sub, HString S, int pos, int len)
{
if (pos<1 || pos>S.length || len<0 || len>S.length - pos + 1)
{
return ERROR;
}
if (Sub->ch)
{
free(Sub->ch);
}
if (len)
{
Sub->ch = (char*)malloc(sizeof(char) * len);
if (!Sub->ch)
{
return ERROR;
}
for (int i = 0, j = pos - 1; i < len && j < pos + len - 1; ++i, ++j)
{
Sub->ch[i] = S.ch[j];
Sub->length = len;
}
}
else
{
return ERROR;
}
return OK;
}
7、
/*---------------------------------------------------------------------------------------
功能:拷贝字符串
参数:1、串指针 2、串指针
输出:空
*/
//拷贝字符串StrCopy(&T, &S)
void StrCopy(HString* T, HString* S)
{
if (T->ch)
{
free(T->ch);
T->ch = NULL;
}
T->ch = (char*)malloc(sizeof(char) * Size);
if (!T->ch)
{
return ERROR;
}
for (int i = 0; i < S->length; ++i)
{
T->ch[i] = S->ch[i];
}
T->length = S->length;
return OK;
}
8、
/*---------------------------------------------------------------------------------------
功能:判断是否空串
参数:1、串
输出:OK、ERROR
*/
//判断是否空串StrEmpty(S)
Status StrEmpty(HString S)
{
if (S.length == 0)
{
return TRUE;
}
return FALSE;
}
9、
/*---------------------------------------------------------------------------------------
功能:返回字串在主串中第pos个字符之后第一次出现的位置
参数:1、串 2、串 3、位置
输出:位置下标(比位置-1)
*/
//返回字串在主串中第pos个字符之后第一次出现的位置Index(&S, T, pos)
int Index(HString* S, HString T, int pos)
{
if (pos < 1 || pos > S->length || T.length == 0)
{
return -1;
}
int i = pos, j = 1;
while (i < S->length && j < T.length)
{
if (S->ch[i] == T.ch[j])
{
i++;
j++;
}
else
{
i = i - j + 2;
j = 0;
}
}
if (j >= T.length)
{
return i - T.length;
}
else
{
return -1;
}
}
10、
/*---------------------------------------------------------------------------------------
功能:用V替换主串S中从pos开始的与T相等且不重叠的第一个子串
参数:1、串指针 2、串 3、串 4、位置
输出:OK、ERROR
*/
//用V替换主串S中从pos开始的与T相等且不重叠的第一个子串Replace(&S, T, V, pos)
Status Replace(HString* S, HString T, HString V, int pos)
{
if (S->ch == NULL || T.ch == NULL || V.ch == NULL)
{
return ERROR;
}
int index = Index(S, T, pos);//index为下标
if (index < 0)
{
return ERROR;
}
StrDelete(S, index + 1, T.length);
return StrInsert(S, index + 1, V);
}
11、
/*---------------------------------------------------------------------------------------
功能:用V替换主串S中所有与T相等的不重叠的子串
参数:1、串指针 2、串 3、串
输出:OK、ERROR
*/
//用V替换主串S中所有与T相等的不重叠的子串ReplaceAll(&S, T, V)
Status ReplaceAll(HString* S, HString T, HString V)
{
if (S->ch == NULL || T.ch == NULL || V.ch == NULL)
{
return ERROR;
}
while (Replace(S, T, V, 1));
return OK;
}
12、
/*---------------------------------------------------------------------------------------
功能:在串S的第pos个字符之前插入T串
参数:1、串指针 2、位置 3、串
输出:OK、ERROR
*/
//在串S的第pos个字符之前插入T串StrInsert(&S, pos, T)
Status StrInsert(HString* S, int pos, HString T)
{
if (pos < 1 || pos > S->length || S->length + T.length > Size)
{
return ERROR;
}
for (int i = S->length - 1; i >= pos - 1; --i)
{
S->ch[i + T.length] = S->ch[i];
}
for (int j = 0; j < T.length; ++j)
{
S->ch[j + pos - 1] = T.ch[j];
}
S->length += T.length;
return OK;
}
13、
/*---------------------------------------------------------------------------------------
功能:从串的第pos个字符起删除长度为len的子串
参数:1、串指针 2、位置 3、长度
输出:OK、ERROR
*/
//从串的第pos个字符起删除长度为len的子串StrDelete(&S, pos, len)
Status StrDelete(HString* S, int pos, int len)
{
if (pos < 1 || pos>S->length - len + 1 || len > S->length || len < 0)
{
return ERROR;
}
for (int i = pos - 1; i < len + pos - 1; ++i)
{
S->ch[i] = S->ch[i + len];
}
S->length -= len;
return OK;
}
14、
/*---------------------------------------------------------------------------------------
功能:销毁串
参数:1、串指针
输出:OK、ERROR
*/
//销毁串DestoryString(&S)
Status DestoryString(HString* S)
{
if (S->ch)
{
free(S->ch);
S->ch = NULL;
}
return OK;
}
15、
/*---------------------------------------------------------------------------------------
功能:输出串
参数:1、串
输出:OK、ERROR
*/
//输出串StrPrint(S)
Status StrPrint(HString S)
{
for (int i = 0; i < S.length; i++)
{
printf("%c", S.ch[i]);
}
printf("\n");
return OK;
}
三、测试主程序:
int main()
{
char* chars = "Hello world!";
char* chars1 = "I am marshal.";
char* chars2 = "I love C program.";
int len = 0;
int pos = 0;
HString S, T, U, V, Sub;
S.ch = NULL;//Hello world!
T.ch = NULL;//I am marshal.
U.ch = NULL;//Hello world!I am marshal.
V.ch = NULL;//I love C program.
Sub.ch = NULL;
if (StrAssign(&S, chars)) //StrAssign
{
printf("初始化成功!!!\n");
}
printf("S的内容为:");
StrPrint(S); //StrPrint
len = StrLenth(S);
printf("S的长度为:%d\n", len); //StrLenth
StrAssign(&T, chars1);
printf("T的内容为:");
StrPrint(T);
StrAssign(&V, chars2);
printf("V的内容为:");
StrPrint(V);
if (StrCompare(T, S) > 0) //StrCompare
{
printf("T > S\n");
}
else if(StrCompare(T, S) == 0)
{
printf("T = S\n");
}
else
{
printf("T < S\n");
}
if (Concat(&U, S, T)) //Concat
{
printf("S、T串联后的串U为:");
StrPrint(U);
}
pos = 18;
len = 7;
if (SubString(&Sub, U, pos, len)) //SubString
{
printf("U中第%d位后连续%d位的串的内容为:", pos, len);
StrPrint(Sub);
}
pos = 1; //Index
pos = Index(&U, T, pos) + 1;
printf("T(I am marshal.)在U(Hello world!I am marshal.)的位置是第%d个字符\n", pos);
if (StrInsert(&U, pos, V)) //StrInsert
{
printf("将V插入到U的第%d个字符之前的串U的内容为:", pos);
StrPrint(U);
}
len = V.length;
if (StrDelete(&U, pos - 1, len)) //StrDelete
{
printf("将U的第%d个字符之后删除长度为%d的子串后U的内容为:", pos-1, len);
StrPrint(U);
}
pos = 1;
Replace(&U, S, T, pos); //Replace
printf("用T替换主串U中一个与S相等的不重叠的子串后,U的内容为:");
StrPrint(U);
StrCopy(&T, &S); //StrCopy
printf("将S的内容拷贝给T之后,T的内容为:");
StrPrint(T);
if (ClearString(&S)) //ClearString
{
printf("串S清空成功!!!\n");
printf("S的内容为:");
StrPrint(S);
if (StrEmpty(S))
{
printf("S为空!!!\n");
}
else
{
printf("S不为空~\n");
}
}
if (DestoryString(&S) && DestoryString(&T) && DestoryString(&U) && DestoryString(&V) && DestoryString(&Sub))
{
printf("串S、T、U、V、Sub销毁成功!!!\n");
} //DestoryString
system("pause");
return 0;
}