• 大小: 1.11MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-20
  • 语言: 其他
  • 标签: KMP  c++  算法  

资源简介

该程序主要功能可分为 1.为用户在本地建立一个文本文件,并且支持录入换行功能,字符序列构成且区分大小写 2.统计给定单词在文本文件中出现的次数 3.检索输出某个单词某个单词出现在文本中的行号、在改行中出现的次数以及位置。

资源截图

代码片段和文件信息

#include
#include
#include
#define MaxStrSize 100
using namespace std;

class Word
{
public:
Word(){text[0]=‘\0‘;}
int Getlength()const;
void PlaceVocabulary();
char text[MaxStrSize];

};

void GetFailure(const Word &patint f[])
{
int j=0k=-1;
f[0]=-1;
while(j if(k==-1||pat.text[k]==pat.text[j])
f[++j]=++k;
else
k=f[k];
}

int KMP_find(Word obWord patint p=0)//p是用于下次匹配的开始检索位置
{
int *f=new int[pat.Getlength()];
GetFailure(patf);
int i=pj=0;
while(i {
if(j==-1||pat.text[j]==ob.text[i])
{
i++;
j++;
}
else
j=f[j];
}
delete []f;
if(j return -1;  
else
return i-j;

}
void WordCount()
{
Word obpat;
char filename[10];
ifstream infile;
int i=0jk;
cout<<“Place input flie name:“;
cin>>filename;

cout<<“Place input vocabulary:“;
cin>>pat.text;
infile.open(filename);
while(infile)
{
infile.getline(ob.textMaxStrSize);
k=0;
while(k {
j=KMP_find(obpatk);
if(j<0) break;
else
{
i++;
k=j+ob.Getlength();
}
}

}
infile.close();
cout< cout<<“在文本文件:“<}
void WordRetrieval()
{
Word obpat;
char filename[20];
int ijkl=0m;
int wz[20];
ifstream infile;
cout<<“Place text file name:“;
cin>>filename;
cout<<“Place you need retrieval:“;
cin>>pat.text;
infile.open(filename);
while(infile)
{
infile.getline(ob.textMaxStrSize);
l++;k=0;i=0;
while(k {
j=KMP_find(obpatk);
if(j<0) break;
else
{
i++;
wz[i]=j;
k=j+ob.Getlength();
}
}
if(i>0)
{
cout<<“所在行:“< cout<<“在该行出现的位置分别是:“;
for(m=1;m<=i;m++) cout< cout< }
}
infile.close();

}
void Word::PlaceVocabulary()
{
cout<<“Place input text:“< cin>>text;
}

int Word::Getlength() const
{
int i;
i=0;

while(text[i]!=‘\0‘)
{
i++;
}

return i;
}

void Menu()
{
cout<<“=======================================“< cout<<“文本文件单词的检索和计数系统“< cout<<“=======================================“< cout<<“1.建立文本文件“< cout<<“2.统计单词出现次数“< cout<<“3.检索单词“< cout<<“4.退出“< cout<<“请输入你的选择<1234>:“;
}
void CreateText()
{
Word Textend;
char TextName[100];
end.text[0]=‘/‘;
end.text[1]=‘e‘;
end.text[2]=‘n‘;
end.text[3]=‘d‘;
end.text[4]=‘\0‘;
cout<<“请输入文件名:“;
cin>>TextName;
ofstream outfile(TextNameios::out);
if(!outfile)
{
cerr<<“open error!“< }
cout<<“请输入文本内容:(写完后用/end结束输入)“<
while(KMP_find(Textend)==-1)
{
cin>>Text.text;
if(KMP_find(Textend)==-1) 
for(int i=0;i outfile<<‘\n‘;
}
outfile.close();
cout<<“T

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-06-29 18:59  Work\
     目录           0  2017-06-29 18:57  Work\Debug\
     文件      581687  2017-06-30 09:44  Work\Debug\StringKMP.exe
     文件      826920  2017-06-30 09:44  Work\Debug\StringKMP.ilk
     文件      381160  2017-06-30 09:44  Work\Debug\StringKMP.obj
     文件     2089208  2017-06-30 09:44  Work\Debug\StringKMP.pch
     文件     1147904  2017-06-30 09:44  Work\Debug\StringKMP.pdb
     文件      115712  2017-06-30 09:44  Work\Debug\vc60.idb
     文件      118784  2017-06-30 09:44  Work\Debug\vc60.pdb
     文件        3414  2017-06-30 10:04  Work\StringKMP.cpp
     文件        3437  2017-06-30 09:44  Work\StringKMP.dsp
     文件         524  2017-06-30 10:04  Work\StringKMP.dsw
     文件       50176  2017-06-30 10:04  Work\StringKMP.ncb
     文件       48640  2017-06-30 10:04  Work\StringKMP.opt
     文件        1269  2017-06-30 09:44  Work\StringKMP.plg
     文件          28  2017-06-30 09:59  Work\asd.txt

评论

共有 条评论