C++的很简单有关问题
C++的很简单问题
[code=C/C++][/code]#include<iostream>
#include<string>
using namespace std;
struct sqstring
{char data[20];
int length;
};
void Enter(sqstring *&t,sqstring *&p) //输入母串和子串
{ t->length=0;
p->length=20;
int i;
cout<<"请输入20个字符的母串"<<endl;
for(i=0;i<20;i++)
cin>>p->data[i];
cout<<"请输入要匹配的子串"<<endl;
string q;
gets(q); i=0;
while(q[i]!='/0')
{t->data[i]=q[i];
t->length++;
i++;
}
void Getnext( sqstring t,int next[]) //得到NEXT
{ next[0]=-1;
int j=0,k=-1;
while(j<t.length)
if(k==-1||t.data[j]==t.data[k])
{j++; k++;
next[j]=k;
}
else
k=next[k];
}
void Inserch(sqstring t,sqstring p) //匹配
{if(t.length>p.length)
{int next[20];
Getnext(t,next);
int i=0,j=0;
while(i<p.length&&j<t.length)
{if(p.data[i]==t.data[j])
{j++;i++}
else
j=next[j];
}
if(j==t.length)
cout<<"位置为:"<<i-j+1<<endl;
else
cout<<"没有相匹配的"<<endl;
}
cout<<"error"<<endl;
}
int main()
{sqstring *t,*p;
Enter(t,p);
Inserch(t,p);
return 0;
}
------解决方案--------------------
请问有什么问题?只有把你的问题说出来别人才知道你有什么问题。
[code=C/C++][/code]#include<iostream>
#include<string>
using namespace std;
struct sqstring
{char data[20];
int length;
};
void Enter(sqstring *&t,sqstring *&p) //输入母串和子串
{ t->length=0;
p->length=20;
int i;
cout<<"请输入20个字符的母串"<<endl;
for(i=0;i<20;i++)
cin>>p->data[i];
cout<<"请输入要匹配的子串"<<endl;
string q;
gets(q); i=0;
while(q[i]!='/0')
{t->data[i]=q[i];
t->length++;
i++;
}
void Getnext( sqstring t,int next[]) //得到NEXT
{ next[0]=-1;
int j=0,k=-1;
while(j<t.length)
if(k==-1||t.data[j]==t.data[k])
{j++; k++;
next[j]=k;
}
else
k=next[k];
}
void Inserch(sqstring t,sqstring p) //匹配
{if(t.length>p.length)
{int next[20];
Getnext(t,next);
int i=0,j=0;
while(i<p.length&&j<t.length)
{if(p.data[i]==t.data[j])
{j++;i++}
else
j=next[j];
}
if(j==t.length)
cout<<"位置为:"<<i-j+1<<endl;
else
cout<<"没有相匹配的"<<endl;
}
cout<<"error"<<endl;
}
int main()
{sqstring *t,*p;
Enter(t,p);
Inserch(t,p);
return 0;
}
------解决方案--------------------
请问有什么问题?只有把你的问题说出来别人才知道你有什么问题。