#include <stdio.h>
#define MAXSIZE 100
#define SIZE sizeof(SEQSTRING)
typedef char ELEMTYPE;
typedef struct //定义顺序串结构
{
ELEMTYPE ch[MAXSIZE];
int length;
}SEQSTRING;
SEQSTRING * INITSEQSTRING(void); //初始化顺序串
SEQSTRING * CREATESTRING(SEQSTRING *); //创建顺序串,当用户输入出现回车时,则认为字符串已经创建创建完成
void PRINTSTRING(SEQSTRING *);//打印顺序串中的数据
int STRLEN(SEQSTRING *);//获取字符串的长度
SEQSTRING * SUBSTR(SEQSTRING *,int,int);//求字串函数
char SUBCHAR(SEQSTRING *,int);//求索引出的字符
int INDEX(SEQSTRING *,SEQSTRING *);//求字串在主串的索引位置
int EQUAL(SEQSTRING *,SEQSTRING *);//求两个字符串是否相等
int DELETE(SEQSTRING *,int,int);//删除操作
int INSERT(SEQSTRING *,int,SEQSTRING *);//插入操作,在index前面插入字符串
void REPLACE(SEQSTRING *,SEQSTRING *,SEQSTRING *);//替换字符串操作
void CONCAT(SEQSTRING *,SEQSTRING *,SEQSTRING *);//连接字符串操作
void CLEAR(SEQSTRING *);//清空字符串操作
void print_enter(void);
void print_tab(void);
void print_str(char *);
void print_info(char *,int);
void print_menu(char *,char *);
void menu(void);
void str_get_length(SEQSTRING *);
SEQSTRING * input_src_str(SEQSTRING *,char *);
int main(int argc,char *argv[])
{
SEQSTRING * str = INITSEQSTRING();
str = CREATESTRING(str);
PRINTSTRING(str);
/*
SEQSTRING * sub = INITSEQSTRING();
int int_sel_item;
menu();
do
{
print_info("select menu:",0);
scanf("%d",&int_sel_item);
switch(int_sel_item)
{
case 0:
menu();
break;
case -1:
break;
case 1:
str_get_length(str);
break;
}
}while(-1 != int_sel_item);
printf("\n%d",EQUAL(str,sub));
printf("length:%d\n",STRLEN(str));
CLEAR(str);
printf("length:%d\n",STRLEN(str));
SEQSTRING * rep = CREATESTRING();
CONCAT(str,sub,rep);
PRINTSTRING(str);
int index,len;
SEQSTRING * str = CREATESTRING();
SEQSTRING * sub = CREATESTRING();
SEQSTRING * rep = CREATESTRING();
REPLACE(str,sub,rep);
PRINTSTRING(str);
printf("%d",STRLEN(str));
PRINTSTRING(str);
print_enter();
PRINTSTRING(sub);
print_enter();
INSERT(str,0,sub);
PRINTSTRING(str);
printf("\nindex:%d",INDEX(str,sub));
printf("\ninput del index,len");
scanf("%d,%d",&index,&len);
printf("del result:%d",DELETE(str,index,len));
PRINTSTRING(str);
printf("\nthe length is :%d",STRLEN(str));
*/
system("pause");
return 0;
}
SEQSTRING * INITSEQSTRING(void)
{
SEQSTRING * seqstr = (SEQSTRING *)malloc(SIZE);
seqstr->length = 0;
return seqstr;
}
SEQSTRING * CREATESTRING(SEQSTRING * str)
{
char c;
do
{
// c = getchar();
// scanf("%s",c);
if ((c= getchar()) == '\n')
{
str->ch[str->length] = '\0'; //添加'\0'结束符号
break;
}
if (str->length < MAXSIZE)
{
str->ch[str->length++] = c;
}
else
{
break; //当顺序串中的数组单元存储满后就跳出
}
}while(1);
return str;
}
void PRINTSTRING(SEQSTRING * str)
{
int len = str->length;
char c;
int i = 0;
while (i < len)
{
c = str->ch[i++];
if ( c != '\0')
{
printf("%c",c);
}
else
{
break;
}
}
}
int STRLEN(SEQSTRING * str)
{
return str->length;
}
SEQSTRING * SUBSTR(SEQSTRING * str,int index,int length)
{
SEQSTRING * result = INITSEQSTRING();
int i = 0,j = index;
if (index > -1 && str->length > 0)
{
while (i < MAXSIZE && i < length)
{
result->ch[i++] = str->ch[j++];
result->length++;
}
if (i < MAXSIZE)
{
result->ch[i] = '\0';
}
}
return result;
}
int INDEX(SEQSTRING * s,SEQSTRING * t)
{
int i,len_s,len_t,flag;
len_s = STRLEN(s);
len_t = STRLEN(t);
flag = 0;
i = 0;
while(i <= len_s - len_t + 1 && (! flag))
{
if (EQUAL(SUBSTR(s,i,len_t),t))
{
flag = 1;
}
else
{
i++;
}
}
if (flag)
{
return i;
}
else
{
return -1;
}
}
int EQUAL(SEQSTRING * s,SEQSTRING * t)
{
int result = 1;
int i = 0;
char ch_s,ch_t;
int len = STRLEN(s);
if (STRLEN(s) == STRLEN(t))
{
do
{
ch_s = SUBCHAR(s,i);
ch_t = SUBCHAR(t,i);
if (ch_s == ch_t)
{
i++;
}
else
{
result = 0;
break;
}
}while(ch_s != '\0' && ch_t != '\0' && i <= len );
}
else
{
result = 0;
}
return result;
}
char SUBCHAR(SEQSTRING * str,int index)
{
char c = ' ';
if (index > - 1 && index < MAXSIZE)
{
c = str->ch[index];
}
return c;
}
int DELETE(SEQSTRING * str,int index,int length)
{
int i,j,result = 0;
int len = str->length;
if (len > 0 & index > -1 && index + length <= len)
{
for(i = index;i < len;i++)
{
str->ch[i] = str->ch[i+length];
}
str->length -= length;
str->ch[str->length] = '\0';
result = 1;
}
return result;
}
int INSERT(SEQSTRING * str,int index,SEQSTRING * str1)
{
int i,j = 0,result = 0;
int len = str->length;
int len1= str1->length;
if(index > -1 && index < len + 1 && len + len1 <= MAXSIZE)
{
for (i = len;i >= index;i--)
{
str->ch[i + len1 - 1] = str->ch[i - 1];
}
for(i = index;i < len1;i++,j++)
{
str->ch[i] = str1->ch[j];
}
str->length += len1;
if (str->length < MAXSIZE)
{
str->ch[str->length] = '\0';
}
result = 1;
}
return result;
}
void REPLACE(SEQSTRING * str,SEQSTRING * str_sub,SEQSTRING * str_rep)
{
int i,j,k,m,len_str,len_sub,len_rep;
len_str = STRLEN(str);
len_sub = STRLEN(str_sub);
len_rep = STRLEN(str_rep);
i = 0;
while(i <= len_str - len_sub + 1)
{
if (EQUAL(SUBSTR(str,i,len_sub),str_sub))
{
if(len_rep < len_sub)
{
k = i;m = i;
for(j = 0; j < len_rep; k++,j++) //将新字符串放在要替换的字符串的位置上
{
str->ch[k] = str_rep->ch[j];
}
for(k = m + len_sub; k < len_str;k++,m++) //将要替换的字符串多余的字符用后面的字符进行替换
{
str->ch[m + len_rep] = str->ch[m + len_sub];
}
str->length = len_str - len_sub + len_rep; //重新计算长度
len_str = STRLEN(str);
i = i - len_sub + len_rep; //重新定位搜索的偏移量
}
else if (len_rep == len_sub)
{
k = i;m = i;
for(j = 0; j < len_rep; k++,j++)
{
str->ch[k] = str_rep->ch[j];
}
i += len_rep; //重新定位搜索的偏移量
}
else if (len_rep > len_sub)
{
k = i;m = i;
for (j = len_str ; j > k + len_sub;j--)
{
str->ch[j - len_sub + len_rep - 1] = str->ch[j - 1];
}
for (j = 0; j < len_rep;j++,m++)
{
str->ch[m] = str_rep->ch[j];
}
str->length = len_str - len_sub + len_rep; //重新计算长度
len_str = STRLEN(str);
i = i - len_sub + len_rep; //重新定位搜索的偏移量
}
else
{
;
}
}
else
{
i++;
}
}
}
void CONCAT(SEQSTRING * str,SEQSTRING * str1,SEQSTRING * str2)
{
int i = 0,j = 0,k = 0;
int len_str1,len_str2;
len_str1 = STRLEN(str1);
len_str2 = STRLEN(str2);
while(i <= MAXSIZE && j < len_str1 )
{
str->ch[i++] = str1->ch[j++];
str->length++;
}
while (i <= MAXSIZE && k < len_str2)
{
str->ch[i++] = str2->ch[k++];
str->length++;
}
}
void CLEAR(SEQSTRING * str)
{
str->length = 0;
}
void print_enter(void)
{
printf("\n");
}
void print_tab(void)
{
printf("\t");
}
void print_str(char * str)
{
while(*str)
{
printf("%c",*str++);
}
}
void print_info(char * str,int isenter)
{
print_tab();
print_str(str);
if(isenter)
{
print_enter();
}
}
void print_menu(char * item,char * desc)
{
print_tab();
print_str(item);
print_tab();
print_str(desc);
print_enter();
}
void menu(void)
{
print_enter();
print_tab();print_info("sequence string manage system",1);print_enter();
print_menu("1","get length");
}
void str_get_length(SEQSTRING * str)
{
int len = 0;
str = input_src_str(str,"input a string:");
len= STRLEN(str);
print_tab();
printf("the length is :%d",len);
print_enter();
PRINTSTRING(str);
}
SEQSTRING * input_src_str(SEQSTRING * str,char * ch)
{
print_info(ch,0);
CLEAR(str);
return CREATESTRING(str);
}
|