我想问一下,这个程序应该怎么改才可以得到正确的答案?弄不明白,请各位高手看一看。
#include"iostream"#define Max 256
using namespace std;
typedef struct
{
char ch[Max];
int lenght;
}
SeqString;
void InitString(SeqString *s)
{
char ch;
int i=0;
while((ch=getchar())!='\n'&&i<Max)
s->ch[i++]=ch;
s->lenght=i;
}
int Strlen(SeqString *s)
{
return (s->lenght);
}
SeqString *substr(SeqString *s,int pos,int len,SeqString *sub)
{
int i;
if(pos<1||pos>s->lenght||len<1||len>s->lenght)
{
cout<<"error!!"<<endl;
return NULL;
}
for(i=0;i<len;i++)
sub->ch[i]=s->ch[pos+i-1];
sub->lenght=len;
return (sub);
}
int strcmp(SeqString *s,SeqString *t)
{
int i,j;
for(i=1,j=1;i<s->lenght,j<t->lenght;)
{
if(s->ch[i]==t->ch[j])
{
i++;
j++;
}
else
{
i=i-j+2;
j=1;
}
}
if(j>t->lenght)
return (i-t->lenght+1);
else
return 0;
}
void main()
{
int i,k;
SeqString *s,*t,*r;
cout<<"构造一个串s!!"<<endl;
InitString(s);
cout<<"构造一个串t!!"<<endl;
InitString(t);
cout<<"请输入子串在主串中位置:";
cin>>i;
k=strcmp(substr(s,i,Strlen(t),r),t);
cout<<k;
}
编译时出现这样的警告:
C:\VC++6.0简体中文版 编程工具\cv++6.0\MSDev98\MyProjects\program46\2011.4.29.cpp(62) : warning C4700: local variable 's' used without having been initialized
C:\VC++6.0简体中文版 编程工具\cv++6.0\MSDev98\MyProjects\program46\2011.4.29.cpp(64) : warning C4700: local variable 't' used without having been initialized
C:\VC++6.0简体中文版 编程工具\cv++6.0\MSDev98\MyProjects\program46\2011.4.29.cpp(67) : warning C4700: local variable 'r' used without having been initialized
2011.4.29.obj - 0 error(s), 3 warning(s)
帮忙看一下,谢谢啦。