資源簡介
該程序主要功能可分為
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
評論
共有 條評論