注册 登录
编程论坛 C++教室

有没有高手在线?

概率 发布于 2011-06-19 10:25, 937 次点击
我想在线请教高手一个程序的错误问题。在线等。谢哈!
12 回复
#2
try_catch2011-06-21 05:54
直接发出来。。或者提问把
#3
pangding2011-06-21 07:49
嗯,同意二楼。在线等效率也不是很高。
#4
概率2011-06-21 09:15
这个程序是这样的,我改了N久,也请教了身边的同学,还是有两个错改不出来,希望各位大侠帮帮忙,看看还有救不?
#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);
}
}
#5
kelas2011-06-21 11:07
既然都贴了、为什么不连同错误信息都贴出来呢
#6
概率2011-06-21 14:10
不好意思啊,情急就忘了,错误是这样的
Compiling...
15.cpp
C:\Users\Administrator\Desktop\新建文件夹\15.cpp(20) : error C2143: syntax error : missing ';' before '=='
C:\Users\Administrator\Desktop\新建文件夹\15.cpp(20) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

15.obj - 2 error(s), 0 warning(s)
麻烦各位帮帮忙哈
#7
lz10919149992011-06-21 15:20
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;
}; // 这里要有分号
#8
概率2011-06-21 16:16
不对诶,我加分号以后错误更多了,还请各位弄到c++里面运行下,感激不尽!
#9
specilize2011-06-21 20:28
看了一下,错了一大堆啊,举个例子,wang operator==(wang&s1,wang&s2)这个函数又不是成员函数,你这样写s1=s;肯定是不对的啊,而且我发现楼主返回void类型都不写的,这样不太好吧,而且为什么要用<iostream.h>不用<iostream>,楼主还是慢慢看着提示的错误改正吧
#10
概率2011-06-21 20:33
这么长的程序我改不来啊,所以才请教的。。。。。。这个程序已经摧残我几天了,希望大家耐心的指点指点咯。。。。。。
#11
specilize2011-06-21 21:11
回复 10楼 概率
这是你自己写的吗,好像挺多语法错误的,要有耐心
#12
概率2011-06-21 21:17
也不算是,因为这个程序要的急,所以我是一边参考别人的程序一边自己写的,最后就成这样了,你们哪位能帮我改改。。。
#13
jbd05132011-06-24 16:53
学习的路还长啊,只能帮你顶下
1