💡 【教材】 ▲04 串

This commit is contained in:
康建伟
2018-01-05 20:24:28 +08:00
parent 7a24d4e813
commit ce7d108fa3
17 changed files with 2451 additions and 0 deletions

View File

@@ -0,0 +1,145 @@
/*************************************
* *
* 文件夹: ▲04 串\01 SequenceString *
* *
* 内 容: 顺序串相关函数测试 *
* *
*************************************/
#include <stdio.h>
#include "SequenceString.c" //**▲04 串**//
int main(int argc, char **argv)
{
char *chars = "abcdef g";
char *t = "** *";
char *v = "^^^ ^";
SString S, Tmp, T, V, Sub;
int i;
printf("▼1\n▲函数 StrAssign_Sq 测试...\n"); //1.函数StrAssign_Sq测试
{
printf("为顺序串 Tmp 赋值...\n");
StrAssign_Sq(Tmp, chars);
printf("\n");
}
PressEnter;
printf("▼3\n▲函数 StrEmpty_Sq 测试...\n"); //3.函数StrEmpty_Sq测试
{
StrEmpty_Sq(Tmp) ? printf(" Tmp 为空!!\n") : printf(" Tmp 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼5\n▲函数 StrLength_Sq 测试...\n"); //5.函数StrLength_Sq测试
{
i = StrLength_Sq(Tmp);
printf(" Tmp 的长度为 %d \n", i);
printf("\n");
}
PressEnter;
printf("▼14\n▲函数 StrPrint_Sq 测试...\n"); //14.函数StrPrint_Sq测试
{
printf(" Tmp 中的元素为Tmp = ");
StrPrint_Sq(Tmp);
printf("\n\n");
}
PressEnter;
printf("▼2\n▲函数 StrCopy_Sq 测试...\n"); //2.函数StrCopy_Sq测试
{
printf("复制 Tmp 到 S ...\n");
StrCopy_Sq(S, Tmp);
printf(" S 中的元素为S = ");
StrPrint_Sq(S);
printf("\n\n");
}
PressEnter;
printf("▼4\n▲函数 StrCompare_Sq 测试...\n"); //4.函数StrCompare_Sq测试
{
printf("比较字符串 Tmp 、 S ...\n");
i = StrCompare_Sq(Tmp, S);
i==0 ? printf("Tmp==S\n") : (i<0 ? printf("Tmp<S\n") : printf("Tmp>S\n"));
printf("\n");
}
PressEnter;
printf("▼11\n▲函数 StrInsert_Sq 测试...\n"); //11.函数StrInsert_Sq测试
{
printf("\"***\" 赋给T...\n");
StrAssign_Sq(T, t);
printf("在 S 的第 5 个字符前插入T...\n");
StrInsert_Sq(S, 5, T);
printf(" S 中的元素为S = ");
StrPrint_Sq(S);
printf("\n\n");
}
PressEnter;
printf("▼9\n▲函数 Index_Sq_1 测试...\n"); //9-1.函数Index_Sq_1测试
{
printf("获取字符串 \"***\" 在 S 中从第1个字符算起的第一次出现的位置...\n");
i = Index_Sq_1(S, T, 1);
printf(" \"***\" 在 S 中第1个字符后第一次出现的位置为%d\n", i);
printf("\n");
}
PressEnter;
printf("▼8\n▲函数 SubString_Sq 测试...\n"); //8.函数SubString_Sq测试
{
printf("用 Sub 返回 S 中第 5 个字符起的 3 个字符...\n");
SubString_Sq(Sub, S, 5, 3);
printf(" Sub 中的元素为Sub = ");
StrPrint_Sq(Sub);
printf("\n\n");
}
PressEnter;
printf("▼10\n▲函数 Replace_Sq、Index_Sq_2 测试...\n");//9-2、10.函数Replace_Sq、Index_Sq_2测试
{
printf("\"^^^^\" 赋给V...\n");
StrAssign_Sq(V, v);
printf("\"^^^^\" 替换S中的 \"***\" ...\n");
Replace_Sq(S, T, V);
printf(" S 中的元素为S = ");
StrPrint_Sq(S);
printf("\n\n");
}
PressEnter;
printf("▼12\n▲函数 StrDelete_Sq 测试...\n"); //12.函数StrDelete_Sq测试
{
printf("删除 S 中第 5 个字符起的 4 个字符...\n");
StrDelete_Sq(S, 5, 4);
printf(" S 中的元素为S = ");
StrPrint_Sq(S);
printf("\n\n");
}
PressEnter;
printf("▼6\n▲函数 ClearString_Sq 测试...\n"); //6.函数ClearString_Sq测试
{
printf("清空 S 前:");
StrEmpty_Sq(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
ClearString_Sq(S);
printf("清空 S 后:");
StrEmpty_Sq(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼7\n▲函数 Concat_Sq 测试...\n"); //7.函数Concat_Sq测试
{
printf("连接 T 和 V 形成 Tmp ...\n");
Concat_Sq(Tmp, T, V);
printf(" Tmp 中的元素为Tmp = ");
StrPrint_Sq(Tmp);
printf("\n\n");
}
PressEnter;
return 0;
}

View File

@@ -0,0 +1,254 @@
/*************************************
* *
* 文件夹: ▲04 串\01 SequenceString *
* *
* 文件名: SequenceString.c *
* *
* 算 法: 4.1、4.2、4.3、4.5 *
* *
*************************************/
#ifndef SEQUENCESTRING_C
#define SEQUENCESTRING_C
#include "SequenceString.h" //**▲04 串**//
Status StrAssign_Sq(SString T, const char *chars) //属于最小操作子集
{
int i, len;
len = strlen(chars);
if(len>MAXSTRLEN) //chars过长
return ERROR;
else
{
T[0] = len;
for(i=1; i<=len; i++)
T[i] = chars[i-1]; //chars为常量
return OK;
}
}
void StrCopy_Sq(SString T, SString S)
{
int i;
for(i=0; i<=S[0]; i++)
T[i] = S[i];
}
Status StrEmpty_Sq(SString S)
{
if(S[0]==0)
return TRUE;
else
return FALSE;
}
int StrCompare_Sq(SString S, SString T) //属于最小操作子集
{
int i = 1;
while(i<=S[0] && i<=T[0])
{
if(S[i]==T[i])
i++;
else
return S[i] - T[i];
}
return S[0] - T[0];
}
int StrLength_Sq(SString S) //属于最小操作子集
{
return S[0];
}
void ClearString_Sq(SString S)
{
S[0] = 0;
}
void DestroyString_Sq(SString S)
{
//顺序串不能被销毁
}
/*════╗
║ 算法4.2║
╚════*/
Status Concat_Sq(SString T, SString S1, SString S2) //属于最小操作子集
{
int i;
for(i=1; i<=S1[0]; i++)
T[i] = S1[i];
if(S1[0]+S2[0]<=MAXSTRLEN)
{
for(i=1; i<=S2[0]; i++)
T[S1[0]+i] = S2[i];
T[0] = S1[0]+S2[0];
return OK;
}
else
{
for(i=1; S1[0]+i<=MAXSTRLEN; i++)
T[S1[0]+i] = S2[i];
T[0]=MAXSTRLEN;
return ERROR;
}
}
/*════╗
║ 算法4.3║
╚════*/
Status SubString_Sq(SString Sub, SString S, int pos, int len)//属于最小操作子集
{
int i;
if(pos<1 || pos>S[0] || len<0 || pos+len-1>S[0])
return ERROR;
for(i=1; i<=len; i++)
Sub[i] = S[pos+i-1];
Sub[0] = len;
return OK;
}
/*════╗
║ 算法4.1║
╚════*/
int Index_Sq_1(SString S, SString T, int pos) //用到串类型的最小操作子集
{
int s, t;
SString Sub;
if(pos>0)
{
s = StrLength_Sq(S);
t = StrLength_Sq(T);
if(s && t)
{
while(pos+t-1<=s)
{
SubString_Sq(Sub, S, pos, t);
if(!StrCompare_Sq(Sub, T))
return pos;
pos++;
}
}
}
return 0;
}
/*════╗
║ 算法4.5║
╚════*/
/* 串的模式匹配算法 */
int Index_Sq_2(SString S, SString T, int pos) //不依赖其他串操作的匹配算法
{
int i = pos;
int j = 1;
if(pos<1)
return 0;
while(i<=S[0] && j<=T[0])
{
if(S[i]==T[j])
{
i++;
j++;
}
else
{
i = i - (j-1) +1; //j-1代表徒劳地前进了j-1个位置
j = 1;
}
}
if(j>T[0] && T[0]) //T不为空串
return i-T[0]; //匹配成功
else
return 0;
}
Status Replace_Sq(SString S, SString T, SString V)
{
int i;
i = Index_Sq_2(S, T, 1); //寻找第一个匹配的位置
while(S[0]-T[0]+V[0]<=MAXSTRLEN && i) //有匹配的字符串且可以被完全替换
{
StrDelete_Sq(S, i, T[0]); //删除T
StrInsert_Sq(S, i, V); //插入V
i += V[0]; //i切换到下一个位置
i = Index_Sq_2(S, T, i); //定位下一个匹配的字符串
}
if(i==0) //S中的T已全部被替换
return OK;
else //S中尚有T但是V已经插不进去了
return ERROR;
}
Status StrInsert_Sq(SString S, int pos, SString T)
{
int i;
if(pos<1 || pos>S[0]+1 || S[0]+T[0]>MAXSTRLEN)
return ERROR;
for(i=S[0]; i>=pos; i--)
S[i+T[0]] = S[i];
for(i=pos; i<=pos+T[0]-1; i++)
S[i] = T[i-pos+1];
S[0] += T[0];
return OK;
}
Status StrDelete_Sq(SString S, int pos, int len)
{
int i;
if(pos<1 || pos+len-1>S[0] || len<0)
return ERROR;
for(i=pos+len; i<=S[0]; i++)
S[i-len] = S[i];
S[0] -= len;
return OK;
}
void StrPrint_Sq(SString S)
{
int i;
for(i=1; i<=S[0]; i++)
printf("%c", S[i]);
}
#endif

View File

@@ -0,0 +1,100 @@
/*************************************
* *
* 文件夹: ▲04 串\01 SequenceString *
* *
* 文件名: SequenceString.h *
* *
* 内 容: 顺序串相关操作列表 *
* *
*************************************/
#ifndef SEQUENCESTRING_H
#define SEQUENCESTRING_H
#include <stdio.h>
#include <string.h> //提供strlen原型
#include "../../▲01 绪论/Status.h" //**▲01 绪论**//
/* 宏定义 */
#define MAXSTRLEN 255 //顺序串的最大串长
/* 串的顺序存储类型定义 */
typedef unsigned char SString[MAXSTRLEN+1]; //0号单元存放串的长度
/* 顺序串函数列表 */
Status StrAssign_Sq(SString T, const char *chars);
/*━━━━━━━━━━━━━━━━━━━━━━━┓
┃(01)生成一个其值等于常量chars的串T串赋值。┃
┗━━━━━━━━━━━━━━━━━━━━━━━*/
void StrCopy_Sq(SString T, SString S);
/*━━━━━━━━━━━┓
┃(02)由串S复制得到串T。┃
┗━━━━━━━━━━━*/
Status StrEmpty_Sq(SString S);
/*━━━━━━━━━━━━━━━━━━━━┓
┃(03)若S为空串返回TRUE,否则返回FALSE。 ┃
┗━━━━━━━━━━━━━━━━━━━━*/
int StrCompare_Sq(SString S, SString T);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(04)若S>T返回值>0若S<T返回值<0否则返回值=0。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
int StrLength_Sq(SString S);
/*━━━━━━┓
┃(05)求串长。┃
┗━━━━━━*/
void ClearString_Sq(SString S);
/*━━━━━━┓
┃(06)清空S。 ┃
┗━━━━━━*/
Status Concat_Sq(SString T, SString S1, SString S2);
/*━━━━━━━━━━━━━━━━━━━━━━┓
┃(07)算法4.2用T返回由S1和S2联接而成的新串。┃
┗━━━━━━━━━━━━━━━━━━━━━━*/
Status SubString_Sq(SString Sub, SString S, int pos, int len);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(08)算法4.3用Sub返回串S的第pos个字符起长度为len的子串。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
int Index_Sq_1(SString S, SString T, int pos);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(09-1)算法4.1返回T在S中第pos个字符后第一次出现的位置不存在则返回0。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
int Index_Sq_2(SString S, SString T, int pos);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(09-2)算法4.5返回T在S中第pos个字符后第一次出现的位置不存在则返回0。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status Replace_Sq(SString S, SString T, SString V);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(10)用V替换主串S中出现的所有与T相等的不重叠的子串可以被完全替换才返回OK。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status StrInsert_Sq(SString S, int pos, SString T);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(11)在串S的第pos个字符之前插入串T。可以完全插入返回OK,否则返回ERROR。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status StrDelete_Sq(SString S, int pos, int len);
/*━━━━━━━━━━━━━━━━━━━━━━━┓
┃(12)从串S中删除第pos个字符起长度为len的子串。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━*/
void DestroyString_Sq(SString S);
/*━━━━━━━┓
┃(13)销毁串S。 ┃
┗━━━━━━━*/
void StrPrint_Sq(SString S);
/*━━━━━━━┓
┃(14)输出串S。 ┃
┗━━━━━━━*/
#endif

View File

@@ -0,0 +1,153 @@
/*********************************
* *
* 文件夹: ▲04 串\02 HeapString *
* *
* 内 容: 堆串相关函数测试 *
* *
*********************************/
#include <stdio.h>
#include "HeapString.c" //**▲04 串**//
int main(int argc, char **argv)
{
char *chars = "abcdefg";
char *t = "***";
char *v = "^^^^";
HString S, Tmp, T, V, Sub;
int i;
printf("▼1\n▲函数 InitString_H 测试...\n"); //1.函数InitString_H测试
{
printf("初始化空串 Tmp ...\n");
InitString_H(&Tmp);
printf("\n");
}
PressEnter;
printf("▼2\n▲函数 StrAssign_H 测试...\n"); //2.函数StrAssign_H测试
{
printf("为 Tmp 赋值 %s ...\n", chars);
StrAssign_H(&Tmp, chars);
printf("\n");
}
PressEnter;
printf("▼4\n▲函数 StrEmpty_H 测试...\n"); //4.函数StrEmpty_H测试
{
StrEmpty_H(Tmp) ? printf(" Tmp 为空!!\n") : printf(" Tmp 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼6\n▲函数 StrLength_H 测试...\n"); //6.函数StrLength_H测试
{
i = StrLength_H(Tmp);
printf(" Tmp 的长度为 %d \n", i);
printf("\n");
}
PressEnter;
printf("▼15\n▲函数 StrPrint_H 测试...\n"); //15.函数StrPrint_H测试
{
printf(" Tmp 中的元素为Tmp = ");
StrPrint_H(Tmp);
printf("\n\n");
}
PressEnter;
printf("▼3\n▲函数 StrCopy_H 测试...\n"); //3.函数StrCopy_H测试
{
printf("复制 Tmp 到 S ...\n");
StrCopy_H(&S, Tmp);
printf(" S 中的元素为S = ");
StrPrint_H(S);
printf("\n\n");
}
PressEnter;
printf("▼5\n▲函数 StrCompare_H 测试...\n"); //5.函数StrCompare_H测试
{
printf("比较字符串 Tmp 、 S ...\n");
i = StrCompare_H(Tmp, S);
i==0 ? printf("Tmp==S\n") : (i<0 ? printf("Tmp<S\n") : printf("Tmp>S\n"));
printf("\n");
}
PressEnter;
printf("▼12\n▲函数 StrInsert_H 测试...\n"); //12.函数StrInsert_H测试
{
printf("\"***\" 赋给 T ...\n");
StrAssign_H(&T, t);
printf("在 S 的第 5 个字符前插入 T ...\n");
StrInsert_H(&S, 5, T);
printf(" S 中的元素为S = ");
StrPrint_H(S);
printf("\n\n");
}
PressEnter;
printf("▼10\n▲函数 Index_H 测试...\n"); //10.函数Index_H测试
{
printf("获取字符串 \"***\" 在 S 中从第 1 个字符算起的第一次出现的位置...\n");
i = Index_H(S, T, 1);
printf(" \"***\" 在 S 中第1个字符后第一次出现的位置为 %d \n", i);
printf("\n");
}
PressEnter;
printf("▼9\n▲函数 SubString_H 测试...\n"); //9.函数SubString_H测试
{
printf("用 Sub 返回 S 中第 5 个字符起的 3 个字符...\n");
SubString_H(&Sub, S, 5, 3);
printf(" Sub 中的元素为Sub = ");
StrPrint_H(Sub);
printf("\n\n");
}
PressEnter;
printf("▼11\n▲函数 Replace_H 测试...\n"); //11.函数Replace_H测试
{
printf("\"^^^^\" 赋给 V ...\n");
StrAssign_H(&V, v);
printf("\"^^^^\" 替换 S 中的 \"***\" ...\n");
Replace_H(&S, T, V);
printf(" S 中的元素为S = ");
StrPrint_H(S);
printf("\n\n");
}
PressEnter;
printf("▼13\n▲函数 StrDelete_H 测试...\n"); //13.函数StrDelete_H测试
{
printf("删除 S 中第 5 个字符起的 4 个字符...\n");
StrDelete_H(&S, 5, 4);
printf(" S 中的元素为S = ");
StrPrint_H(S);
printf("\n\n");
}
PressEnter;
printf("▼7\n▲函数 ClearString_H 测试...\n"); //7.函数ClearString_H测试
{
printf("清空 S 前:");
StrEmpty_H(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
ClearString_H(&S);
printf("清空 S 后:");
StrEmpty_H(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼8\n▲函数 Concat_H 测试...\n"); //8.函数Concat_H测试
{
printf("联接 T 和 V 形成Tmp...\n");
Concat_H(&Tmp, T, V);
printf(" Tmp 中的元素为Tmp = ");
StrPrint_H(Tmp);
printf("\n\n");
}
PressEnter;
return 0;
}

View File

@@ -0,0 +1,274 @@
/*********************************
* *
* 文件夹: ▲04 串\02 HeapString *
* *
* 文件名: HeapString.c *
* *
* 算 法: 4.4 *
* *
*********************************/
#ifndef HEAPSTRING_C
#define HEAPSTRING_C
#include "HeapString.h" //**▲04 串**//
void InitString_H(HString *S)
{
(*S).ch = NULL;
(*S).length = 0;
}
Status StrAssign_H(HString *T, char *chars)
{
int i, j;
InitString_H(T);
i = strlen(chars);
if(!i)
return ERROR;
else
{
(*T).ch = (char*)malloc(i*sizeof(char));
if(!((*T).ch))
exit(OVERFLOW);
for(j=0; j<i; j++)
(*T).ch[j] = chars[j];
(*T).length = i;
}
return OK;
}
Status StrCopy_H(HString *T, HString S)
{
int i;
InitString_H(T);
if(StrEmpty_H(S))
return ERROR;
(*T).ch = (char*)malloc(S.length*sizeof(char));
if(!(*T).ch)
exit(OVERFLOW);
for(i=0; i<S.length; i++)
(*T).ch[i] = S.ch[i];
(*T).length = S.length;
return OK;
}
Status StrEmpty_H(HString S)
{
if(S.length==0 && S.ch==NULL)
return TRUE;
else
return FALSE;
}
Status StrCompare_H(HString S, HString T)
{
int i;
for(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;
}
int StrLength_H(HString S)
{
if(StrEmpty_H(S))
return 0;
else
return S.length;
}
Status ClearString_H(HString *S)
{
if((*S).ch)
{
free((*S).ch);
(*S).ch = NULL;
}
(*S).length = 0;
return OK;
}
Status Concat_H(HString *T, HString S1, HString S2)
{
int i;
InitString_H(T);
(*T).length = S1.length + S2.length;
(*T).ch = (char*)malloc((*T).length*sizeof(char));
if(!(*T).ch)
exit(OVERFLOW);
for(i=0; i<S1.length; i++)
(*T).ch[i] = S1.ch[i];
for(i=0; i<S2.length; i++)
(*T).ch[S1.length+i] = S2.ch[i];
return OK;
}
Status SubString_H(HString *Sub, HString S, int pos, int len)
{
int i;
InitString_H(Sub);
if(StrEmpty_H(S))
return ERROR;
if(pos<1 || pos>S.length || len<0 || pos+len-1>S.length)
return ERROR;
if(len) //非空子串
{
(*Sub).ch = (char*)malloc(len*sizeof(char));
if(!(*Sub).ch)
exit(OVERFLOW);
for(i=0; i<len; i++)
(*Sub).ch[i] = S.ch[i+pos-1];
(*Sub).length = len;
}
return OK;
}
int Index_H(HString S, HString T, int pos)
{
int s, t, i;
HString Sub;
InitString_H(&Sub);
if(pos>0)
{
s = S.length;
t = T.length;
i = pos;
while(i+t-1<=s)
{
SubString_H(&Sub, S, i, t);
if(StrCompare_H(Sub, T))
i++;
else
return i;
}
}
return 0;
}
Status Replace_H(HString *S, HString T, HString V)
{
int i;
if(StrEmpty_H(T))
return ERROR;
i = Index_H(*S, T, 1);
while(i!=0)
{
StrDelete_H(S, i, StrLength_H(T));
StrInsert_H(S, i, V);
i += StrLength_H(V);
i = Index_H(*S, T, i);
}
return OK;
}
/*════╗
║ 算法4.4║
╚════*/
Status StrInsert_H(HString *S, int pos, HString T)
{
int i;
if(pos<1 || pos>(*S).length+1)
return ERROR;
if(StrEmpty_H(T))
return ERROR;
else
{
(*S).ch = (char*)realloc((*S).ch, ((*S).length+T.length)*sizeof(char));
if(!(*S).ch)
exit(OVERFLOW);
for(i=(*S).length-1; i>=pos-1; i--) //为插入T而腾出位置
(*S).ch[i+T.length] = (*S).ch[i];
for(i=0; i<T.length; i++)
(*S).ch[pos-1+i] = T.ch[i]; //插入T
(*S).length += T.length;
}
return OK;
}
Status StrDelete_H(HString *S, int pos, int len)
{
int i;
if(StrEmpty_H(*S))
return ERROR;
if(pos<1 || pos+len-1>(*S).length ||len<0)
return ERROR;
for(i=pos-1; i+len<=(*S).length; i++)
(*S).ch[i] = (*S).ch[i+len];
(*S).length -= len;
(*S).ch = (char*)realloc((*S).ch, (*S).length*sizeof(char)); //缩小分配的空间
return OK;
}
void DestroyString_H(HString *S)
{
//堆串不能被销毁
}
void StrPrint_H(HString S)
{
int i;
if(StrEmpty_H(S))
printf("S为空串不可输出");
for(i=0; i<S.length; i++)
printf("%c", S.ch[i]);
}
#endif

View File

@@ -0,0 +1,102 @@
/*********************************
* *
* 文件夹: ▲04 串\02 HeapString *
* *
* 文件名: HeapString.h *
* *
* 内 容: 堆串相关操作列表 *
* *
*********************************/
#ifndef HEAPSTRING_H
#define HEAPSTRING_H
#include <stdio.h>
#include <stdlib.h> //提供malloc、realloc、free、exit原型
#include <string.h> //提供strlen原型
#include "../../▲01 绪论/Status.h" //**▲01 绪论**//
/* 串的堆存储表示 */
typedef struct
{
char *ch; //若是非空串则按串长分配存储区否则ch为NULL
int length;
}HString;
/* 堆串函数列表 */
void InitString_H(HString *S);
/*━━━━━━━━━━┓
┃(01)初始化S为空串。 ┃
┗━━━━━━━━━━*/
Status StrAssign_H(HString *T, char *chars);
/*━━━━━━━━━━━━━━━━━━┓
┃(02)生成一个其值等于常量chars的串T。┃
┗━━━━━━━━━━━━━━━━━━*/
Status StrCopy_H(HString *T, HString S);
/*━━━━━━━━━━━┓
┃(03)由串S复制得到串T。┃
┗━━━━━━━━━━━*/
Status StrEmpty_H(HString S);
/*━━━━━━━━━━━━━━━━━━━━┓
┃(04)若S为空串返回TRUE,否则返回FALSE。 ┃
┗━━━━━━━━━━━━━━━━━━━━*/
Status StrCompare_H(HString S, HString T);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(05)若S>T返回值>0若S<T返回值<0否则返回值=0。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
int StrLength_H(HString S);
/*━━━━━━┓
┃(06)求串长。┃
┗━━━━━━*/
Status ClearString_H(HString *S);
/*━━━━━━┓
┃(07)清空S。 ┃
┗━━━━━━*/
Status Concat_H(HString *T, HString S1, HString S2);
/*━━━━━━━━━━━━━━━━━━┓
┃(08)用T返回由S1和S2联接而成的新串。 ┃
┗━━━━━━━━━━━━━━━━━━*/
Status SubString_H(HString *Sub, HString S, int pos, int len);
/*━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(09)用Sub返回串S的第pos个字符起长度为len的子串。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━*/
int Index_H(HString S, HString T, int pos);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(10)返回T在S中第pos个字符后第一次出现的位置不存在则返回0。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status Replace_H(HString *S, HString T, HString V);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(11)用V替换主串S中出现的所有与T相等的不重叠的子串。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status StrInsert_H(HString *S, int pos, HString T);
/*━━━━━━━━━━━━━━━━━━━━━━┓
┃(12)算法4.4在串S的第pos个字符之前插入串T。┃
┗━━━━━━━━━━━━━━━━━━━━━━*/
Status StrDelete_H(HString *S, int pos, int len);
/*━━━━━━━━━━━━━━━━━━━━━━━┓
┃(13)从串S中删除第pos个字符起长度为len的子串。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━*/
void DestroyString_H(HString *S);
/*━━━━━━━┓
┃(14)销毁串S。 ┃
┗━━━━━━━*/
void StrPrint_H(HString S);
/*━━━━━━━┓
┃(15)输出串S。 ┃
┗━━━━━━━*/
#endif

View File

@@ -0,0 +1,153 @@
/***************************************
* *
* 文件夹: ▲04 串\03 BlockChainString *
* *
* 内 容: 块链串相关函数测试 *
* *
***************************************/
#include <stdio.h>
#include "BlockChainString.c" //**▲04 串**//
int main(int argc, char **argv)
{
char *chars = "kuai-lian-chuan";
char *t = "***";
char *v = "^^^^";
LString S, Tmp, T, V, Sub;
int i;
printf("▼1\n▲函数 InitString_L 测试...\n"); //1.函数InitString_L测试
{
printf("初始化空串 Tmp ...\n");
InitString_L(&Tmp);
printf("\n");
}
PressEnter;
printf("▼2\n▲函数 StrAssign_L 测试...\n"); //2.函数StrAssign_L测试
{
printf("为 Tmp 赋值 %s ...\n", chars);
StrAssign_L(&Tmp, chars);
printf("\n");
}
PressEnter;
printf("▼4\n▲函数 StrEmpty_L 测试...\n"); //4.函数StrEmpty_L测试
{
StrEmpty_L(Tmp) ? printf(" Tmp 为空!!\n") : printf(" Tmp 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼6\n▲函数 StrLength_L 测试...\n"); //6.函数StrLength_L测试
{
i = StrLength_L(Tmp);
printf(" Tmp 的长度为 %d \n", i);
printf("\n");
}
PressEnter;
printf("▼15\n▲函数 StrPrint_L 测试...\n"); //15.函数StrPrint_L测试
{
printf(" Tmp 中的元素为Tmp = ");
StrPrint_L(Tmp);
printf("\n\n");
}
PressEnter;
printf("▼3\n▲函数 StrCopy_L 测试...\n"); //3.函数StrCopy_L测试
{
printf("复制 Tmp 到 S ...\n");
StrCopy_L(&S, Tmp);
printf(" S 中的元素为S = ");
StrPrint_L(S);
printf("\n\n");
}
PressEnter;
printf("▼5\n▲函数 StrCompare_L 测试...\n"); //5.函数StrCompare_L测试
{
printf("比较字符串 Tmp 、 S ...\n");
i = StrCompare_L(Tmp, S);
i==0 ? printf("Tmp==S\n") : (i<0 ? printf("Tmp<S\n") : printf("Tmp>S\n"));
printf("\n");
}
PressEnter;
printf("▼12\n▲函数 StrInsert_L 测试...\n"); //12.函数StrInsert_L测试
{
printf("\"***\" 赋给 T ...\n");
StrAssign_L(&T, t);
printf("在 S 的第 5 个字符前插入 T ...\n");
StrInsert_L(&S, 5, T);
printf(" S 中的元素为S = ");
StrPrint_L(S);
printf("\n\n");
}
PressEnter;
printf("▼10\n▲函数 Index_L 测试...\n"); //10.函数Index_L测试
{
printf("获取字符串 \"***\" 在 S 中从第 1 个字符算起的第一次出现的位置...\n");
i = Index_L(S, T, 1);
printf(" \"***\" 在 S 中第 1 个字符后第一次出现的位置为 %d \n", i);
printf("\n");
}
PressEnter;
printf("▼9\n▲函数 SubString_L 测试...\n"); //9.函数SubString_L测试
{
printf("用 Sub 返回S中第 5 个字符起的 3 个字符...\n");
SubString_L(&Sub, S, 5, 3);
printf(" Sub 中的元素为Sub = ");
StrPrint_L(Sub);
printf("\n\n");
}
PressEnter;
printf("▼11\n▲函数 Replace_L 测试...\n"); //11.函数Replace_L测试
{
printf("\"^^^^\" 赋给 V ...\n");
StrAssign_L(&V, v);
printf("\"^^^^\" 替换 S 中的 \"***\" ...\n");
Replace_L(&S, T, V);
printf(" S 中的元素为S = ");
StrPrint_L(S);
printf("\n\n");
}
PressEnter;
printf("▼13\n▲函数 StrDelete_L 测试...\n"); //13.函数StrDelete_L测试
{
printf("删除 S 中第 5 个字符起的 4 个字符...\n");
StrDelete_L(&S, 5, 4);
printf(" S 中的元素为S = ");
StrPrint_L(S);
printf("\n\n");
}
PressEnter;
printf("▼7\n▲函数 ClearString_L 测试...\n"); //7.函数ClearString_L测试
{
printf("清空 S 前:");
StrEmpty_L(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
ClearString_L(&S);
printf("清空 S 后:");
StrEmpty_L(S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
printf("\n");
}
PressEnter;
printf("▼8\n▲函数 Concat_L 测试...\n"); //8.函数Concat_L测试
{
printf("联接 T 和 V 形成 Tmp ...\n");
Concat_L(&Tmp, T, V);
printf(" Tmp 中的元素为Tmp = ");
StrPrint_L(Tmp);
printf("\n\n");
}
PressEnter;
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,115 @@
/***************************************
* *
* 文件夹: ▲04 串\03 BlockChainString *
* *
* 文件名: BlockChainString.h *
* *
* 内 容: 块链串相关操作列表 *
* *
***************************************/
#ifndef BLOCKCHAINSTRING_H
#define BLOCKCHAINSTRING_H
#include <stdio.h>
#include <stdlib.h> //提供malloc、realloc、free、exit原型
#include <string.h> //提供strlen原型
#include "../../▲01 绪论/Status.h" //**▲01 绪论**//
/* 宏定义 */
#define CHUNKSIZE 3 //块大小(自定义)
/* 串的块链存储类型定义 */
typedef struct Chunk
{
char ch[CHUNKSIZE];
struct Chunk *next;
}Chunk;
typedef struct
{
Chunk *head, *tail; //串的头和尾指针
int curlen; //串的当前长度
}LString;
/* 块链串函数列表 */
void InitString_L(LString *T);
/*━━━━━━━━━━┓
┃(01)初始化块链串T。 ┃
┗━━━━━━━━━━*/
Status StrAssign_L(LString *T, char *chars);
/*━━━━━━━━━━━━━━━━━━┓
┃(02)生成一个其值等于常量chars的串T。┃
┗━━━━━━━━━━━━━━━━━━*/
Status StrCopy_L(LString *T, LString S);
/*━━━━━━━━━━━┓
┃(03)由串S复制得到串T。┃
┗━━━━━━━━━━━*/
Status StrEmpty_L(LString S);
/*━━━━━━━━━━━━━━━━━━━━┓
┃(04)若S为空串返回TRUE,否则返回FALSE。 ┃
┗━━━━━━━━━━━━━━━━━━━━*/
int StrCompare_L(LString S, LString T);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(05)若S>T返回值>0若S<T返回值<0否则返回值=0。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
int StrLength_L(LString S);
/*━━━━━━┓
┃(06)求串长。┃
┗━━━━━━*/
void ClearString_L(LString *S);
/*━━━━━━┓
┃(07)清空S。 ┃
┗━━━━━━*/
void Concat_L(LString *T, LString S1, LString S2);
/*━━━━━━━━━━━━━━━━━━┓
┃(08)用T返回由S1和S2联接而成的新串。 ┃
┗━━━━━━━━━━━━━━━━━━*/
Status SubString_L(LString *Sub, LString S, int pos, int len);
/*━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(09)用Sub返回串S的第pos个字符起长度为len的子串。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━*/
int Index_L(LString S, LString T, int pos);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(10)返回T在S中第pos个字符后第一次出现的位置不存在则返回0。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status Replace_L(LString *S, LString T, LString V);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(11)用V替换主串S中出现的所有与T相等的不重叠的子串。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status StrInsert_L(LString *S, int pos, LString T);
/*━━━━━━━━━━━━━━━━━━┓
┃(12)在串S的第pos个字符之前插入串T。 ┃
┗━━━━━━━━━━━━━━━━━━*/
Status StrDelete_L(LString *S, int pos, int len);
/*━━━━━━━━━━━━━━━━━━━━━━━┓
┃(13)从串S中删除第pos个字符起长度为len的子串。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━*/
void DestroyString_L(LString *S);
/*━━━━━━━┓
┃(14)销毁串S。 ┃
┗━━━━━━━*/
void StrPrint_L(LString S);
/*━━━━━━━┓
┃(15)输出串S。 ┃
┗━━━━━━━*/
Status GetChar_L(LString S, char *c, int i);
/*━━━━━━━━━━━┓
┃用*c返回串S中第i个字符┃
┗━━━━━━━━━━━*/
#endif

View File

@@ -0,0 +1,54 @@
/*******************************
* *
* 文件夹: ▲04 串\04 KMP *
* *
* 内 容: KMP算法相关函数测试 *
* *
*******************************/
#include "KMP.c" //**▲04 串**//
int main(int argc, char **argv)
{
int L, i1, i2, pos, i;
SString S, T;
char *s = "abaaabcaabaabcacabaabcaabaabcac";
char *t = "abaabcac";
StrAssign_Sq(S, s); //主串
StrAssign_Sq(T, t); //模式串
L = StrLength_Sq(T); //模式串长度
int next[L+1]; //模式串的next函数值
int nextval[L+1]; //模式串的nextval函数值
get_next(T, next); //算法4.7
get_nextval(T, nextval); //算法4.8即算法4.7的改进版
pos = 1; //匹配算法起点
i1 = Index_KMP(S, T, pos, next); //算法4.6
i2 = Index_KMP(S, T, pos, nextval);
printf("主 串 ");
StrPrint_Sq(S);
printf("\n");
printf("模式串 ");
StrPrint_Sq(T);
printf("\n\n");
printf("next ");
for(i=1; i<=L; i++)
printf("%d",next[i]);
printf("\n");
printf("从%d个字符起第一次匹配成功的位置为i1 = %d\n", pos, i1);
printf("\n");
printf("nextval");
for(i=1; i<=L; i++)
printf("%d",nextval[i]);
printf("\n");
printf("从%d个字符起第一次匹配成功的位置为i2 = %d\n", pos, i2);
printf("\n");
return 0;
}

View File

@@ -0,0 +1,93 @@
/**************************
* *
* 文件夹: ▲04 串\04 KMP *
* *
* 文件名: KMP.c *
* *
* 算 法: 4.6、4.7、4.8 *
* *
**************************/
#ifndef KMP_C
#define KMP_C
#include "KMP.h" //**▲04 串**//
/*════╗
║ 算法4.6║
╚════*/
int Index_KMP(SString S, SString T, int pos, int next[])
{
int i = pos;
int j = 1;
if(pos<1)
return 0;
while(i<=S[0] && j<=T[0])
{
if(S[i]==T[j] || j==0)
{
i++;
j++;
}
else //失配时回到前一个适当的位置
j = next[j];
}
if(j>T[0]) //匹配成功
return i - T[0];
else //匹配失败
return 0;
}
/*════╗
║ 算法4.7║
╚════*/
void get_next(SString T, int next[])
{
int i = 1;
int j = 0;
next[1] = 0; //第一个字符处失配
while(i<T[0])
{
if(T[i]==T[j] || !j)
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
/*════╗
║ 算法4.8║
╚════*/
void get_nextval(SString T, int nextval[])
{
int i = 1;
int j = 0;
nextval[1] = 0;
while(i<T[0])
{
if(T[i]==T[j] || !j)
{
i++;
j++;
if(T[i]!=T[j])
nextval[i] = j;
else
nextval[i] = nextval[j];
}
else
j = nextval[j];
}
}
#endif

View File

@@ -0,0 +1,33 @@
/*******************************
* *
* 文件夹: ▲04 串\04 KMP *
* *
* 文件名: KMP.h *
* *
* 内 容: KMP算法相关操作列表 *
* *
*******************************/
#ifndef KMP_H
#define KMP_H
#include <stdio.h>
#include "../01 SequenceString/SequenceString.c" //**▲04 串**//
/* KMP算法函数列表 */
int Index_KMP(SString S, SString T, int pos, int next[]);
/*━━━━━━━━━━━━━┓
┃(1)算法4.6KMP匹配算法。 ┃
┗━━━━━━━━━━━━━*/
void get_next(SString T, int next[]);
/*━━━━━━━━━━━━━━━━┓
┃(2)算法4.7求next函数值的算法。┃
┗━━━━━━━━━━━━━━━━*/
void get_nextval(SString T, int nextval[]);
/*━━━━━━━━━━━━━━━━━━━━┓
┃(3)算法4.8改进后的求next函数值的算法。┃
┗━━━━━━━━━━━━━━━━━━━━*/
#endif

View File

@@ -0,0 +1,11 @@
关键词 书号索引
algorithms 034
analysi 067
analysis 034050067
computer 005034
data 005010023
design 034
fundamentals 023
introduction 010050
numerical 050067067
structures 005010023

View File

@@ -0,0 +1,7 @@
Êé ºÅ Êé Ãû
005 Computer Data Structures
010 Introduction to Data Structures
023 Fundamentals of Data Structures
034 The Design and Analysis of Computer Algorithms
050 Introduction to Numerical Analysis
067 Numerical Analysis

View File

@@ -0,0 +1,37 @@
/*************************************
* *
* 文件夹: ▲04 串\05 WordIndexTable *
* *
* 内 容: 创建索引表相关函数测试 *
* *
*************************************/
#include <stdlib.h> //提供system原型
#include "WordIndexTable.c" //**▲04 串**//
int main(int argc, char **argv)
{
FILE *fp;
char line[MaxLineLen];
char *bookinfo = "BookInfo.txt"; //书目文件名
char *bookidx = "BookIdx.txt"; //关键词索引文件名
Main(bookinfo, bookidx); //创建索引表
if(fp = fopen(bookidx, "r")) //显示索引表到屏幕
printf("---------索引表生成功!---------\n\n");
while(!feof(fp))
{
fgets(line, MaxLineLen, fp);
printf("%s", line);
}
printf("\n\n-------------------------------\n\n");
Wait(50);
system("start BookInfo.txt"); //打开书目文件
Wait(50);
system("start BookIdx.txt"); //打开上面生成的索引表
return 0;
}

View File

@@ -0,0 +1,245 @@
/*********************************************
* *
* 文件夹: ▲04 串\05 WordIndexTable *
* *
* 文件名: WordIndexTable.c *
* *
* 算 法: 4.9、4.10、4.11、4.12、4.13、4.14 *
* *
*********************************************/
#ifndef WORDINDEXTABLE_C
#define WORDINDEXTABLE_C
#include "WordIndexTable.h" //**▲04 串**//
/*════╗
║ 算法4.9║
╚════*/
void Main(char *bookinfo, char *bookidx)
{
FILE *f, *g;
char b[MaxLineLen]; //书目临时缓存区
IdxListType idxlist; //关键词索引表
int i;
LElemType_E bno; //书号
if(f=fopen(bookinfo, "r")) //以只读模式打开书目文件
{
if(g=fopen(bookidx, "w"))
{
InitIdxList(&idxlist); //初始化索引表
fgets(b, MaxLineLen, f); //跳过文件第一行
while(!feof(f)) //开始读取文件f
{
GetLine(f); //从文件f读入一个书目信息到书目缓冲区gBuf
ExtractKeyWord(&bno); //从gBuf提取关键词到词表gWdList书号存入bno
InsIdxList(&idxlist, bno); //将书号及对应的关键词插入索引表idxlist
}
PutText(g, idxlist); //向g中写入数据
}
}
fclose(g);
fclose(f); //关闭文件
}
void InitIdxList(IdxListType *idxlist)
{
char *chars = "关键词 书号索引"; //作为索引表的表头
StrAssign_H(&((*idxlist).item[0].key), chars);
InitList_E(&((*idxlist).item[0].bnolist));
(*idxlist).num = 0; //表头为第0条信息
}
void GetLine(FILE *f)
{
int len;
fgets(gBuf, MaxLineLen, f); //此处需要反复读取故b必须为字符数组不可为字符指针
len = strlen(gBuf); //包括换行符的长度
gBuf[len-1] = '\0'; //换行符处用'\0'取代
}
void ExtractKeyWord(int *bno)
{
char *title;
FILE *fp;
*bno = strtol(gBuf, &title, 10); //分解字符串gBuf书号存入bno书名存入title
strlwr(title); //大写变小写
fp = tmpfile(); //建立临时文件
fprintf(fp, "%s", title); //把字符串写入临时文件
fseek(fp, 0, SEEK_SET); //文件指针移到起始位置
for(gWdList.last=0; gWdList.last<MaxWordNum; gWdList.last++)
{
gWdList.str[gWdList.last] = (char*)malloc(MaxLineLen*sizeof(char));
if(!gWdList.str[gWdList.last])
exit(OVERFLOW);
//从临时文件中读取字符串
if(fscanf(fp, "%s", gWdList.str[gWdList.last])==EOF) //读取失败则退出循环
break;
}
}
/*═════╗
║ 算法4.10 ║
╚═════*/
Status InsIdxList(IdxListType *idxlist, int bno)
{
int i, j;
Bool boo;
HString wd;
if(!gWdList.last)
return ERROR;
for(i=0; i<gWdList.last; i++)
{
GetWord(i, &wd); //用堆串wd获取关键词词表第i个字符串
if(!isCommonWords(wd)) //如果此关键词不是常用词,则需要进行插入操作
{
j = Locate(*idxlist, wd, &boo);
if(boo==FALSE) //当前关键词不在索引表中,则需插入此关键词
InsertNewKey(idxlist, j, wd);
if(!InsertBook(idxlist, j, bno)) //插入书号
return ERROR;
}
}
return OK;
}
void PutText(FILE *g, IdxListType idxlist)
{
int i, j, m, n;
Link p;
HString S;
ELinkList L;
for(i=0; i<=idxlist.num; i++)
{
S = idxlist.item[i].key; //输出关键词
for(m=0; m<S.length; m++)
fprintf(g, "%c", S.ch[m]);
if(i) //不是表头
{
for(j=1; j<=18-idxlist.item[i].key.length; j++) //输入一定数量的空格隔开
fprintf(g, " ");
L = idxlist.item[i].bnolist; //输出书号索引
for(n=1,p=L.head->next; n<=L.len; n++)
{
fprintf(g, "%03d", p->data);
p = p->next;
if(p)
fprintf(g, "");
}
}
if(i!=idxlist.num) //未到最后一行
fprintf(g, "\n");
}
}
/*═════╗
║ 算法4.11 ║
╚═════*/
void GetWord(int i, HString *wd)
{
char *p = gWdList.str[i];
StrAssign_H(wd, p);
}
/*═════╗
║ 算法4.12 ║
╚═════*/
int Locate(IdxListType idxlist, HString wd, Bool *b)
{
int i, m;
for(i=idxlist.num; i>0 && (m=StrCompare_H(idxlist.item[i].key,wd))>0; i--);
if(m==0 && idxlist.num!=0)
{
*b = TRUE;
return i;
}
else
{
*b = FALSE;
return i+1;
}
}
/*═════╗
║ 算法4.13 ║
╚═════*/
void InsertNewKey(IdxListType *idxlist, int i, HString wd)
{
int j;
for(j=(*idxlist).num; j>=i; j--) //索引项后移
(*idxlist).item[j+1] = (*idxlist).item[j];
StrCopy_H(&((*idxlist).item[i].key), wd); //插入索引项
(*idxlist).num++; //索引书目增一
InitList_E(&((*idxlist).item[i].bnolist));
}
/*═════╗
║ 算法4.14 ║
╚═════*/
Status InsertBook(IdxListType *idxlist, int i, LElemType_E bno)
{
Link p;
if(!MakeNode_E(&p, bno)) //分配失败
return ERROR;
Append_E(&((*idxlist).item[i].bnolist), p); //插入新的书号索引
return OK;
}
Status isCommonWords(HString S) //此处指向的字符串并无空字符结尾
{ //常用词词表中的字符串是有空字符结尾的
int i, len;
char a[S.length+1];
Status flag = FALSE;
for(i=0; i<S.length; i++)
a[i] = S.ch[i];
a[i] = '\0';
for(i=0; i<gWordList.last; i++)
{
if(!strcmpi(a, gWordList.str[i])) //对两字符串进行忽略大小写的比较
{
flag = TRUE;
break; //两字符串相等则退出循环
}
}
return flag;
}
#endif

View File

@@ -0,0 +1,107 @@
/*************************************
* *
* 文件夹: ▲04 串\05 WordIndexTable *
* *
* 文件名: WordIndexTable.h *
* *
* 内 容: 创建索引表相关操作列表 *
* *
*************************************/
#ifndef WORDINDEXTABLE_H
#define WORDINDEXTABLE_H
#include <stdio.h> //提供fopen、fclose、feof、fgets原型
#include <stdlib.h> //提供exit、strtol原型
#include <string.h> //提供strlen、strcmpi、strlwr原型
#include "../../▲01 绪论/Status.h" //**▲01 绪论**//
#include "../../▲02 线性表/09 ExtenLinkedList/ExtenLinkedList.c" //**▲02 线性表**//
#include "../02 HeapString/HeapString.c" //**▲04 串**//
/* 宏定义 */
#define MaxBookNum 1000 //允许的最大书目数
#define MaxKeyNum 2500 //关键词索引表最大容量
#define MaxLineLen 500 //书目串的最大长度
#define MaxWordNum 10 //词表的最大容量
/* 类型定义 */
typedef Status Bool;
typedef struct //词表类型(顺序表)
{
char *str[MaxWordNum]; //字符串数组
int last; //词表的长度
}WordListType;
typedef struct //索引项类型
{
HString key; //索引关键词
ELinkList bnolist; //存放书号索引的链表
}IdxTermType;
typedef struct //索引表类型(有序表)
{
IdxTermType item[MaxKeyNum+1]; //索引表
int num; //索引表中包含的索引项数目
}IdxListType;
/* 全局变量 */
char gBuf[MaxLineLen]; //书目缓存区
WordListType gWordList = {{"a","an","the","of","and","is","to","as","in","for"},10};//常用词词表
WordListType gWdList; //关键词词表(普通词表)
/* 创建索引表函数列表 */
void Main(char *bookinfo, char *bookidx);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(01)算法4.9:读取书目文件,并创建相应的关键词索引表到另一文件。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
void InitIdxList(IdxListType *idxlist);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(02)算法4.10初始化索引表且在idxlist.item[0]设置一表头。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
void GetLine(FILE *f);
/*━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(03)从文件f中读取一条书目信息存入书目缓冲区gBuf。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━*/
void ExtractKeyWord(int *bno);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(04)从gBuf中提取书名关键词到词表gWdList书号存入bno。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status InsIdxList(IdxListType *idxlist, int bno);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(05)将书号bno对应的书名关键词按词典顺序插入索引表idxlist。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
void PutText(FILE *g, IdxListType idxlist);
/*━━━━━━━━━━━━━━━━━━━┓
┃(06)将生成的索引表idxlist输出到文件g。┃
┗━━━━━━━━━━━━━━━━━━━*/
void GetWord(int i, HString *wd);
/*━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(07)算法4.11用wd返回词表gWdList中第i个关键词。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━*/
int Locate(IdxListType idxlist, HString wd, Bool *b);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(08)算法4.12查询在索引表idxlist中是否存在与wd相等的关键词。 ┃
┃ 若存在则返回wd在词表中的位置并置b为TRUE否则返回wd应插入的位置并置b为FALSE。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
void InsertNewKey(IdxListType *idxlist, int i, HString wd);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(09)算法4.13在索引表第i项上插入关键词wd并初始化书号索引的链表为空表。 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status InsertBook(IdxListType *idxlist, int i, int bno);
/*━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃(10)算法4.14在索引表的第i项中插入书号为bno的索引。┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━*/
Status isCommonWords(HString S);
/*━━━━━━━━━━━━━━━━━━┓
┃(11)判断S包含的关键词是否为常用词。 ┃
┗━━━━━━━━━━━━━━━━━━*/
#endif