#include
#include
#include
#include
#include
#define Maxsize 200
enum returninfo{success,fail,overflow,underflow,range_error};//定义返回信息清单
class cstring
{
private:
char name[Maxsize];
public:
cstring();
~cstring();
bool strcreat(char *string);//串建立
bool randow();//随机产生字符串
void strtraverse();//串遍历
int strlength();//求串长度
cstring & operator =(char *string);//串赋值
cstring & operator (char *string);//串连接
char *substr(int locate,int len);//求子串
bool strcompare(char *string);//串比较
int strindex(char *string);//子串定位
returninfo strinsert(int locate,char *string);//串插入
returninfo strdelete(int locate,int len);//串删除
returninfo strmodify(int locate,int len,char *string);//修改字符串
bool strreplace(char *string1,char *string2);//串替换
void strresolve();//串的分解
void statistics();//字符的统计
bool compare(int num,char data);
};
cstring::cstring()
{
name[0]='\0';
}
cstring::~cstring(){}
//串建立
bool cstring::strcreat(char *string)
{
int i=0;
if(strlen(string)>=Maxsize)
return false;
else
{
strcpy(name,string);//调用库函数
return true;
}
}
//随机产生字符串
bool cstring:: randow()
{
int number;
srand((unsigned int)time(NULL));
cout<<"请输入字符串的长度: ";
cin>>number;
if(number>=Maxsize)
return false;
else{
for(int i=0;i
{
name[i 1]='\0';
name[i]=rand()� 33;//产生随机字符的ASCII
}
return true;
}
}
//串遍历
void cstring::strtraverse()
{
if(name[0]!='\0')
cout<<name<<endl;
else
cout<<"字符串为空!"<<endl;
}
//求串长度
int cstring::strlength()
{
return strlen(name);
}
//串赋值
cstring &cstring::operator =(char *string)//定义重载字符 =
{
strcpy(name,string);//调用库函数
return (*this);
}
//串连接
cstring &cstring:: operator (char *string)//定义重载字符
{
strcat(name,string);
return (*this);
}
//求子串
char *cstring::substr(int locate,int len)
{
char *temp;
int n=0,i;
temp=new char(len 1);
if(name[0]!='\0')
{
for(i=locate-1;(i-locate)<(len-1);i )
{
temp[n 1]='\0';
temp[n ]=name[i];
}
return temp;
}
else
return NULL;
}
//串比较
bool cstring::strcompare(char *string)
{
if(strcmp(name,string)==0)
return true;
else
return false;
}
//子串定位
int cstring::strindex(char *string)
{
int n=0,locate=0,i;
char *searchp;
for(searchp=name;(*searchp)!='\0';searchp )
{
locate ;
n=0;
for(i=0;i<(int)strlen(string);i )
if(*(string i)==*(searchp i))
n ;
if(n==(int)strlen(string))
break;
}
if((*searchp)=='\0')
locate=-1;
return locate;
}
//串插入
returninfo cstring::strinsert(int locate,char *string)
{
int i,n;
char *searchp=string;
n=strlen(string);
if(strlen(name) n>=Maxsize)
return overflow;
else if(locate>(cstring::strlength() 1)||locate<0)
return range_error;
else
{
for(i=(cstring::strlength());i>=locate-1;i--)//先后移
name[i n]=name[i];
for(i=(locate-1);(i-locate)再覆盖
name[i]=*(searchp );
return success;
}
}
//串删除
returninfo cstring::strdelete(int locate,int len)
{
if(len>=Maxsize)
return overflow;
else if(locate>=cstring::strlength()||locate<0)
return range_error;
else
{
for(int i=(locate-1);i<=cstring::strlength();i )//直接覆盖
name[i]=name[i len];
return success;
}
}
//串替换
bool cstring::strreplace(char *string1,char *string2)
{
int n=0;
char *searchp=name,*temp,*followp;
while(*searchp!='\0')
{
followp=searchp;
if(*followp==(*string1))
{
for(temp=string1;*temp!='\0';temp )
if(*temp!=*(followp )) break;
if(*temp=='\0')
{
n ;
cstring::strdelete((searchp-name 1),strlen(string1));//先删除
cstring::strinsert((searchp-name 1),string2);//再插入
searchp=searchp strlen(string2)-1;
if((searchp-name)>=strlen(name))
break;
continue;
}
}
searchp ;
}
if(n>0)
return true;
else
return false;
}
//修改字符串
returninfo cstring::strmodify(int locate,int len,char *string)
{
if(len strlen(name)>=Maxsize)
return overflow;
else if(locate>=cstring::strlength()||locate<0)
return range_error;
else
{
cstring::strdelete(locate,len);//先删除
cstring::strinsert(locate,string);//再插入
return success;
}
}
//串的分解
void cstring::strresolve()
{
int sum=0,n=0;
char temp[50],*searchp;
if(name[0]!='\0')
{
for(searchp=name;(*searchp)!='\0';searchp )
{
if(*searchp>='0'&&(*searchp)<='9')
sum=sum*10 (*searchp)-'0';
else{
temp[n 1]='\0';
temp[n ]=*searchp;
}
}
cout<<"分解的整数为: "<<sum<<endl;
cout<<"分解的字符为: "<<temp<<endl;
}
else
cout<<"字符串为空!"<<endl;
}
//字符的统计
bool cstring::compare(int num,char data)
{
for(int i=0;i
if(name[i]==data)
break;
if(i==num)
return 1;
else
return 0;
}
void cstring::statistics()
{
int m=0;
if(name[0]!='\0')
{
for(int i=0;i
{
m=0;
for(int j=0;j
if(name[j]==name[i]&&compare(i,name[i]))
m ;
if(m>0)
cout<<"字符 "<<name[i]<<"的个数为: "<<m<<endl;
}
}
else
cout<<"字符串为空!"<<endl;
}
void show_menu()
{
cout<<"=============菜单============="<<endl
<<"=============================="<<endl
<<" 1.创建字符串"<<endl
<<" 2.遍历字符串"<<endl
<<" 3.求串长度"<<endl
<<" 4.覆盖刷新"<<endl
<<" 5.串连接"<<endl
<<" 6.求子串"<<endl
<<" 7.串比较"<<endl
<<" 8.子串定位"<<endl
<<" 9.串插入"<<endl
<<" a.串删除"<<endl
<<" b.替换"<<endl
<<" c.修改"<<endl
<<" d.分解"<<endl
<<" e.统计"<<endl
<<" 0.退出"<<endl
<<"=========================="<<endl
<<"请输入选择: ";
}
int main()
{
char choice,temp[20],newchar[20];
int locate,length,choose;
returninfo infor;
cstring newstring;
while(1)
{
show_menu();
cin>>choice;
switch(choice)
{
case '1':
cout<<endl<<"1.手工输入 2.随机产生"<<endl;
cin>>choose;
bool tf;
if(choose==1)
{
cout<<"请输入字符串: ";
cin>>temp;
tf=newstring.strcreat(temp);
}
else
tf=newstring.randow();
if(tf)
cout<<"创建成功!"<<endl;
else
cout<<"创建失败!"<<endl;
break;
case '2':
cout<<"字符串为: ";
newstring.strtraverse();
break;
case '3':
cout<<"字符串长度为: ";
cout<<newstring.strlength()<<endl;
break;
case '4':
cout<<"请输入新的字符串: ";
cin>>temp;
if(strlen(temp)>=Maxsize)
cout<<"长度过大,赋值失败!"<<endl;
else
{
newstring=temp;
cout<<"赋值成功!"<<endl;
}
break;
case '5':
cout<<"请输入需要连接的字符串: ";
cin>>temp;
if((newstring.strlength() strlen(temp))>=Maxsize)
cout<<"长度过大,连接失败!"<<endl;
else
{
newstring temp;
cout<<"连接成功!"<<endl;
}
break;
case '6':
cout<<"请输入子串的位置: ";
cin>>locate;
cout<<"请输入子串的长度: ";
cin>>length;
if(locate>0&&locate
{
strcpy(temp,newstring.substr(locate,length));
if(temp!=NULL)
cout<<"子串为: "<<temp<<endl;
else
cout<<"主字符串为空!"<<endl;
}
else
cout<<"位置有误!"<<endl;
break;
case '7':
cout<<"请输入要比较的字符串: ";
cin>>temp;
if(newstring.strcompare(temp))
cout<<"两个字符串相同!"<<endl;
else
cout<<"两个字符串不相同!"<<endl;
break;
case '8':
cout<<"请输入子串: ";
cin>>temp;
locate=newstring.strindex(temp);
if(locate==-1)
cout<<"主字符串中不存在这样的子串!"<<endl;
else
cout<<"子串在主字符串的位置为:"<<locate<<endl;
break;
case '9':
cout<<"请输入要插入的字符串: ";
cin>>temp;
cout<<"请输入插入字符串的位置: ";
cin>>locate;
infor=newstring.strinsert(locate,temp);
if(infor==overflow)
cout<<"字符串过长,插入失败!"<<endl;
else if(infor==range_error)
cout<<"位置不正确,插入失败!"<<endl;
else
cout<<"插入成功!"<<endl;
break;
case 'a':
cout<<"请输入要删除的字符串的起始位置: ";
cin>>locate;
cout<<"请输入要删除的字符串的长度: ";
cin>>length;
infor=newstring.strdelete(locate,length);
if(infor==overflow)
cout<<"字符串过长,删除失败!"<<endl;
else if(infor==range_error)
cout<<"位置不正确,删除失败!"<<endl;
else
cout<<"删除成功!"<<endl;
break;
case 'b':
cout<<"请输入要替换的字符串: ";
cin>>temp;
cout<<"请输入新的字符串: ";
cin>>newchar;
if(newstring.strreplace(temp,newchar))
cout<<"替换成功!"<<endl;
else
cout<<"替换失败!"<<endl;
break;
case 'c':
cout<<"请输入要修改的字符串的起始位置: ";
cin>>locate;
cout<<"请输入要修改的字符串的长度: ";
cin>>length;
cout<<"请输入新的字符串: ";
cin>>temp;
infor=newstring.strmodify(locate,length,temp);
if(infor==overflow)
cout<<"字符串过长,修改失败!"<<endl;
else if(infor==range_error)
cout<<"位置不正确,修改失败!"<<endl;
else
cout<<"修改成功!"<<endl;
break;
case 'd':
newstring.strresolve();
break;
case 'e':
newstring.statistics();
break;
case '0':
exit(0);
default:
cout<<"输入错误!"<<endl;
}
cout<<endl<<"请按任意键继续。。。。"<<endl;
getch();
system("cls");
}
return 0;
}