#include<iostream.h>
const int max=100;
struct seqstring
{
char ch[max];
int curlen;
};
void push(seqstring &T,seqstring &S)
{
char x,y;
cout<<"请输入T串:"<<endl;
cin>>x;int p=1;
while(x!='#')
{
T.ch[p]=x;
cin>>x;
T.curlen++;
}
cout<<"请输入S串:"<<endl;
cin>>y;int j=1;
while(y!='#')
{
S.ch[j]=y;
cin>>y;
S.curlen++;
}
}
void insert(seqstring &T,int i,seqstring S)
{
if(T.curlen+S.curlen>max)
cout<<"存储已满";
else
{
for(int j=T.curlen;j>=i;j--)
T.ch[j+S.curlen]=T.ch[j];
for(j=1;j<S.curlen;j++)
T.ch[j+i-1]=S.ch[j];
T.curlen=T.curlen+S.curlen;
}
}
void print(seqstring T,seqstring S)
{
for(int i=1;i<=T.curlen;i++)
cout<<T.ch;
for(int j=1;j<=S.curlen;j++)
cout<<S.ch[j]<<endl;
}
int index(seqstring T,seqstring S)
{
int i=1,j=1;
while ((i<S.curlen)&&(j<=T.curlen))
{
if(T.ch[i]==S.ch[j])
{i++,j++;}
else
{i=i-j+2;j=1;}
}
if(j>S.curlen)
return (i-S.curlen);
else return(0);
}
void main()
{
seqstring T,S;
int j,k;
do
{
cout<<"\n\n\n\n";
cout<<"\t\t\t 串的运算\n";
cout<<"\t\t*****************************************\n";
cout<<"\t\t 1---输入数据 *\n";
cout<<"\t\t 2---插入数据 *\n";
cout<<"\t\t 3---子串定位 *\n";
cout<<"\t\t 4---显示数据 *\n";
cout<<"\t\t 0---返 回 *\n";
cout<<"\t\t*****************************************\n";
cout<<"\t\t 请选择菜单项0-4:";
cin>>k;
switch(k)
{
case 1:
push(T,S);break;
case 2:
cout<<"请输入要插入的位置:"<<endl;cin>>j;
insert(T,j,S);break;
case 3:
index(T,S);break;
case 4:
cout<<"数据有:"<<endl;
print(T,S);
}
}while(k!=0);
}