common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
//Status Equal(int a, int b);
#endif
HeapString.h
#ifndef _HEAPSTRING_H_
#define _HEAPSTRING_H_
//串的堆分配存储表示
typedef struct
{
char *ch; //若是非空串,则按串长分配存储区,否则ch为NULL.
int length; //串长度
}HString;
Status InitString(HString *T);
//初始化串*T
int StrLength(HString *T);
//返回指定串的长度
Status StrAssign(HString *T, char *chars);
//生成一个其值等于串常量chars的串*T
Status StrDisp(HString *T);
//显示指定的串中的所有字符
int StrCompare(HString *S, HString *T);
//比较串S和T,若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0
Status ClearString(HString *S);
//将*S清为空串
Status Concat(HString *T, HString S1, HString S2);
//用T返回由S1和S2联接而成的新串
Status SubString(HString *Sub, HString S, int pos, int len);
//用Sub返回串S的第pos个字符起长度为len的子串。其中1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1
#endif
HeapString.c
#include "common.h"
#include "HeapString.h"
#include "stdio.h"
#include "stdlib.h"
/*
函数功能:初始化串*T
*/
Status InitString(HString *T)
{
T->ch = NULL;
return OK;
}
/*
函数功能:返回指定串的长度
*/
int StrLength(HString *T)
{
return(T->length);
}
/*
输入参数:*T 待初始化的串*T
*chars 待判断的串常量
返回参数:OVERFLOW 存储空间不够用
OK 操作成功
函数功能:生成一个其值等于串常量chars的串*T
说明:上面是课本的描述,其实说“将串常量chars中的字符序列复制到串*T中去”岂不是更好吗,因为*T的存储空间虽是在StrAssign()函数中分配的,但却是在外部定义的。(其实也不用太纠结)
另外一点:我的本意是将要处理的串放在StrAssign()外部定义,当然也可以将其放在StrAssign()函数内部定义,完了返回串的地址即可。
*/
Status StrAssign(HString *T, char *chars)
{
int i = 0, k;
char *c = chars;
if(T->ch)
free(T->ch);
while(*c != '\0') //求chars的长度,结果存于i
{
i++;
c++;
}
if(!i) //如果i=0(即chars长度为0),则不必将chars复制到*T中
{
T->ch = NULL;
T->length = 0;
}
else //否则将chars中的字符串复制到*T
{
if(!(T->ch = (char *)malloc(i*sizeof(char))))
exit(OVERFLOW);
for(k=0; k<i; k++)
{
T->ch[k] = chars[k];
}
T->length = i;
}
return OK;
}
/*
函数功能:显示整个串
*/
Status StrDisp(HString *T)
{
int i = 0,len = StrLength(T);
printf("\n>>>>>>>>>>>>>>> output start >>>>>>>>>>>>>>>\n");
printf("The current string length is %d.\n",len);
while(len--)
{
printf("%c",T->ch[i++]);
}
//printf("|");
printf("\n<<<<<<<<<<<<<<< output end <<<<<<<<<<<<<<<\n");
return OK;
}
/*
函数功能:比较串S和T,若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0
*/
int StrCompare(HString *S, HString *T)
{
int i, S_len, T_len;
S_len = StrLength(S);
T_len = StrLength(T);
for(i=0; i<S_len && i<T_len; ++i)
{
if(S->ch[i] != T->ch[i])
return(S->ch[i] - T->ch[i]);
}
return(S_len - T_len);
}
/*
函数功能:将*S清为空串
*/
Status ClearString(HString *S)
{
if(S->ch)
{
free(S->ch);
S->ch = NULL;
}
S->length = 0;
return OK;
}
/*
函数功能:用T返回由S1和S2联接而成的新串
*/
Status Concat(HString *T, HString S1, HString S2)
{
int i,j,len;
if(T->ch)
free(T->ch);
if(!(T->ch = (char *)malloc((S1.length + S2.length)*sizeof(char))))
exit(OVERFLOW);
len = StrLength(&S1); //求串的长度的方法有:方法1:用函数StrLength(),方法2:直接S1.length,但在这里这两个方法其实都差不多,并没有多大意义。
for(i=0; i<len; i++)
T->ch[i] = S1.ch[i];
T->length = S1.length + S2.length;
len = T->length;
for(j=0; i<len; i++,j++)
T->ch[i] = S2.ch[j];
return OK;
}
/*
函数功能:用Sub返回串S的第pos个字符起长度为len的子串。其中1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1
*/
Status SubString(HString *Sub, HString S, int pos, int len)
{
int i;
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 = NULL;
Sub->length = 0;
}
else
{
Sub->ch = (char *)malloc(len*sizeof(char));
for(i=0; i<len; i++)
{
Sub->ch[i] = S.ch[pos-1+i];
}
Sub->length = len;
}
return OK;
}
main.c
#include "common.h"
#include "HeapString.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
int main()
{
HString str1,str2,T;
InitString(&str1);
StrAssign(&str1,"beijing.");
StrDisp(&str1);
InitString(&str2);
StrAssign(&str2,"beijing_|_|_.");
StrDisp(&str2);
printf("Comparative result:%d\n",StrCompare(&str1,&str2));
_getch(); //从键盘接收一个字符,但并不回显
printf("\n\n\nsecond\n");
StrAssign(&str1,"shanghai come from hai shang.");
StrDisp(&str1);
InitString(&str2);
StrAssign(&str2,"shanghai is a mega city.");
StrDisp(&str2);
printf("Comparative result:%d\n",StrCompare(&str1,&str2));
InitString(&T);
Concat(&T, str1, str2);
StrDisp(&T);
SubString(&T, str1, 3, 4);
StrDisp(&T);
}
运行结果如下: