**数据结构>串## 标题**
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 20
#define ok 1
typedef int Status;
typedef struct String{
char ch[maxsize];
Status length;
}String, *string;
Status initString(string S)
{
S->length = 0;
return ok;
}
Status strAssign(string S, char str[])
{
int i;
for(i = 1; i < (strlen(str))+1; i++)
{
S->ch[i] = str[i-1];
}
S->length = strlen(str);
S->ch[0] = S->length;
return ok;
}
//遍历串
Status out(string S)
{
int i;
printf("长度为:%d\t", S->ch[0]);
printf("字符串为:\t");
for (i = 1; i<S->ch[0]+1; i++)
{
printf("%c", S->ch[i]);
}
printf("\n");
return ok;
}
//串连接
Status contact(string T, string S1, string S2)
{
Status i;
if(S1->ch[0] + S2->ch[0] <= maxsize)
{
for(i = 1; i <= S1->ch[0]; i++)
{
T->ch[i] = S1->ch[i];
T->ch[S1->ch[0] + i] = S2->ch[i];
}
T->ch[0] = S1->ch[0] + S2->ch[0];
}
else if(S1->ch[0] <= maxsize)
{
for(i = 1; i < S1->ch[0]; i++)
{
T->ch[i] = S1->ch[i];
}
for(i = 1; i < maxsize-S1->ch[0]; i++)
{
T->ch[S1->ch[0] + i] = S2->ch[i];
}
T->ch[0] = maxsize;
}
else {
for(i = 1; i <= maxsize; i++)
{
T->ch[i] = S1->ch[i];
}
}
return ok;
}
//比较函数
Status StrCompare(string S1, string S2)
{
Status i;
for(i = 0; i < S1->length&&i < S2->length; ++i);
if(S1->ch[i] != S2->ch[i])
return S1->length - S2->length;
}
//取子串
Status SubString(string Sub,string S,Status pos,int len)
{
Status i;
if(pos < 1 || pos > S->length || len < 0 || len > S->length-pos+1)
return NULL;
if(Sub->ch) free(Sub->ch);
if(!len)
{
Sub = NULL;
Sub->length = 0;
}
else {
for(i = 1; i < len+1; i++)
{Sub->ch[i] = S->ch[pos-1+i]; }
Sub->length = len;
Sub->ch[0] = Sub->length;
}
return 0;
}
//定位函数(依个比较)
Status Index(string S1,string S2)
{
Status i, j;
i = 1; j = 1;
while(i <= S1->ch[0] && j <= S2->ch[0])
{
if(S1->ch[i] == S2->ch[j])
{++i; ++j;}
else
{i = i-j+2;
j = 1; }
if(j > S2->ch[0])
return i-S2->ch[0];
}
return 0;
}
//定位函数(依次取串比较)
Status index(string S1, string S2)
{
Status i = 1;
String Sub;
initString(&Sub);
while(i <= S1->length - S2->length)
{
SubString(&Sub, S1, i, S2->length);
if(StrCompare(&Sub, S2) == 0)
++i;
else return i;
}
return 0;
}
//求next[i]函数
Status get_next(string S2, Status next[])
{
Status i, j;
i = 1; j = 0;
next[1] = 0;
while(i < S2->ch[0])
{
if(j == 0 || S2->ch[i] == S2->ch[j])
{i++; j++; next[j] = j; }
else j = next[j];
}
}
//定位函数(kmp算法)
Status indexkmp(string S1, string S2)
{
Status i, j, next[100];
i=1; j=1; next[0]=0;
while(i <= S1->ch[0] && j <= S2->ch[0])
{
if(j==0 || S1->ch[i] == S2->ch[j])
{i++; j++; }
else
j = next[j]; }
if(j > S2->ch[0])
return i - S2->ch[0];
return 0;
}
//主函数
int main()
{
Status flag, length, next[100];
String S1, S2, Sub;
initString(&S1);
initString(&S2);
initString(&Sub);
char str[100] = {'\0'};
do{
printf("\n");
printf("__________________________________________________\n");
printf("| 1.输入字符串S1 |\n");
printf("| 2.输入字符串S2 |\n");
printf("| 3.遍历输出s |\n");
printf("| 4.S1和S2连接 |\n");
printf("| 5.比较 |\n");
printf("| 6.取子串 |\n");
printf("| 7.定位S2在S1的位置(依次取串比较) |\n");
printf("| 8.定位S2在S1的位置(依个比较) |\n");
printf("| 9.定位S2在S1的位置(kmp算法) |\n");
printf("| 0.退出 |\n");
printf("|_________________________________________________|\n");
printf("\n");
printf("输入数字选择:> ");
scanf("%d", &flag);
system("cls");
printf("\n");
switch(flag)
{
case 1:printf("请输入一串字符串:> "); scanf("%s", str); strAssign(&S1, str); break;
case 2:printf("请输入一串字符串:>"); scanf("%s", str); strAssign(&S2, str); break;
case 3:printf("遍历输出S1:\n "); out(&S1); printf("\n遍历输出S2:\n"); out(&S2); break;
case 4:contact (&Sub, &S1, &S2); out(&Sub); break;
case 5:printf("S1比S2长:%d", StrCompare(&S1, &S2)); break;
case 6:Status pos, len;
printf("请输入需要取的长度起始位置pos和长度len:>\t");
scanf("%d %d", &pos, &len);
SubString(&Sub, &S1, pos, len);
printf("原子串:"); out(&S1);
printf("取到的子串:");
out(&Sub); break;
case 7:index(&S1, &S2); out(&S1); out(&S2); printf("%d", index(&S1, &S2)); break;
case 8:Index(&S1 ,&S2); out(&S1); out(&S2); printf("%d", Index(&S1, &S2)); break;
case 9:indexkmp(&S1, &S2); out(&S1); out(&S2); printf("%d", indexkmp(&S1, &S2)); break;
default:system("cls"); printf("非法输入!\n"); break;
}
}
while(flag != 0);
return 0;
return 0;
}
数据结构>串
最新推荐文章于 2024-11-10 13:26:50 发布