资源简介
该程序主要功能可分为
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
相关资源
- 无线传感器网络中节点定位算法 硕士
- 水库优化调度poa算法实现
- 算法导论答案完全版(中英文)
- 基于遗传算法的机房排课系统的论文
- 基于匹配追踪算法的时频滤波去噪方
- NachOS线程调度_基于优先级和Round Rob
- Marching cubes算法实现对切片集的三维重
- Visual Studio中使用开源二维码QR库libq
- 关灯游戏求解算法含代码
- ANN: Approximate Nearest Neighbors近似最近邻
- VC++开发的仓库管理系统设计文档和
- dianyajiance.rar
- 遗传算法的介绍,英文版
- 华为边缘计算核心板开发帮助手册
- 我的第一本算法书.zip
- 点云配准4pcs算法
- 智能优化算法及其应用王凌著.pdf
- 算法分析与设计东北大学
- 基于数字图像处理技术的缺陷检测算
- 算法导论全套PPT含答案讲义
- 算法导论的教学配套ppt——中科大
- 组合最优化与算法第四版
- 分子模拟——从算法到应用pdf中译本
- 计算机视觉中的多视图几何 Multiple
- Algorithms Illuminated Part 1 和 Part 2
- 计算机算法设计与分析第四版课后答
- 基于QT的网络视频直播软件
- 数据结构与算法分析中文版第二版
- libcurl+图灵机器人api编写的只能聊天系
- 算法导论 第三版 全书完整
评论
共有 条评论