请各位高手帮帮忙,我这个程序改两天了,还是不知道哪儿错了,先谢过谢过。。。
#include<iostream.h>#include<string.h>
class wang
{
public:
friend int main();
wang();
wang(wang&s);
wang operator=(wang&s);
wang operator==(wang&s);
wang operator<(wang&s);
wang operator>(wang&s);
wang operator+(wang&s);
chuanchang(wang&s);
char *charu(char *s1,char *s2,int a);
shanchu(wang&s);
zichuan(wang&s);
void GetNext(char *t,int *next);
private:
char s;
}
wang operator==(wang&s1,wang&s2)
{
s1=s;
cout<<"请输入要比较的字符串s2:"<<endl;
cin>>s2;
if((s2)==0)
return true;
else
return false;
}
wang operator<(wang&s1,wang&s2)
{
s1=s;
cout<<"请输入要比较的字符串s2:"<<endl;
cin>>s2;
if((s2)<0)
return true;
else
return false;
}
wang operator>(wang&s1,wang&s2)
{
s1=s;
cout<<"请输入要比较的字符串s2:"<<endl;
cin>>s2;
if((s2)>0)
return true;
else
return false;
}
wang operator+(wang&s1,wang&s2)
{
s1=s;
cout<<"请输入想要连接的字符串:"<<endl;
cin>>s2;
char*temp;
temp=new char[strlen(s1)+strlen(s2)+1];
strcpy(temp,s1);
strcat(temp,s2);
wang result(temp);
return result;
}
chuanchang(wang&s)
{
s1=s;
cout<<"该字符串的长度是:"<<strlen(s1)<<endl;
}
char *charu(char *s1,char *s2,int a)
{
int a;
s1=s;
cout<<"请输入要插入的字符串:"<<endl;
cin>>s2;
cout<<"输入插入处的下标:"<<endl;
cin>>a;
for(int m = 0;s1[m] != '\0';m++);
for(int n = 0;s2[n] != '\0';n++);
char *ch = new char [m + n];
int i,j;
for(i = 0;i <= a;i++)
ch[i] = s1[i];
for(j = 0;s2[j] != '\0';j++,i++)
ch[i] = s2[j];
for(j = a + 1;s1[j] != '\0';i++,j++)
ch[i] = s1[j];
ch[i] = '\0';
char *p = charu(s1,s2,a);
cout<<"插入后的字符串为:"<<p<<endl;
}
shanchu(wang&s)
{
s1=s;
int i,j;
for(i=0,j=0;s[i]!=0;i++,j++)if(s[j]>='0'&&s[j]<'10')
i--;elses[i]=s[j];cout<<"字符串删除后输出是:"<<s<<endl;}
zichuan(wang&s)
{
s1=s;
int start,n,s3;
cout<<"请输入字串的起始位置:"<<endl;
cin>>start;
cout<<"请输入子串的长度:"<<endl;
cin>>n;
s3=s1.assign(s1,start-1,n);
cout<<s3.data()<<endl;
}
void GetNext(char *s2,int *next)
{
s1=s;
cout<<"请输入s2串:"<<endl;
cin>>s2;
int i = 1,j = 0;//设置初始下标i和j
int n = strlen(s2);//s2串的长度为n
next[0] = -1;
next[1] = 0;
while(i < n)
{
if((j == -1) || (s2[j] == s2[i]))
{next[++i] = ++j;}
else
{j = next[j];}
int KMP(char *s1,char *s2)
{
int n = strlen(s1); //输入s1串的长度n
int m = strlen(s2); //输入s2串的长度m
int *next = new int[m];
GetNext(s2,next);
for(int i=0,j=0;i<n&&j<m;)//循环直到串s1中所剩字符长度小于s2的长度或s2中所有字符均比较完;
{
if((j == -1)||(s1[i] == s2[j]))
{
i++;
j++;
}
else
{j = next[j];}
}
return (j==m?i-j:-1);
}
int pos;//中间量,判断是否在s1串中找到s2串
pos = KMP(s1,s2); //调用kmp算法
if(pos != -1)//pos等于-1,说明没有在s1串中找到s2串
{
int i = 0;
cout<<"匹配成功!"<<endl;
cout<<"找到s2串:";
while(i < strlen(s2))
{
cout<<s1[pos+i];
i++;
}
cout<<endl;
}
else
{
cout<<"在串s1中没有找到s2串!"<<endl;
}
return 0;
}
}
void main()
{
int num=0,s;
cout<<"请输入一个字符串s:"<<endl;
cin>>s;
while(num!=10)
{
cout<<"请选择功能序号:"<<endl;
cout<<"1>比较是否相等 2>比较是否小于 3>比较是否大于 4>连接 5>求串长 6>串插入 7>串删除 8>求子串 9>串匹配 10>退出"<<endl;
cin>>num;
if(num==1)bool operator==(wang&s1,wang&s2);
if(num==2)bool operator<(wang&s1,wang&s2);
if(num==3)bool operator>(wang&s1,wang&s2);
if(num==4)bool operator+(wang&s1,wang&s2);
if(num==5)chuanchang(wang&s);
if(num==6)char *charu(char *s1,char *s2,int a);
if(num==7)shanchu(wang&s);
if(num==8)zichuan(wang&s);
if(num==9)void GetNext(char *t,int *next);
}
}
[ 本帖最后由 概率 于 2011-6-21 10:07 编辑 ]