菜鸟学习c++—map使用练习 实现词频统计程序
2015-07-11 09:51
1741 查看
c++练习,使用map实现词频统计程序。
程序实现两个功能:(1)统计文本中各个单词的出现次数 (2)查找指定输入单词在文本中是否出现
需注意:练习比较随意,多使用全局变量,正式使用形参实参传递参数比较好。
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
ifstream is;
vector<string> text;
map<string, size_t> word_count;
int main()
{
string line;
char choice;
void create_vector_map(string line); /* 完成分词工作,将文本变为单个单词存入map函数 */
int search(string word); /* 查询功能实现函数 */
void open_file(ifstream &is);
void print_vector_map();
void result_1();
open_file(is); /* 打开文本 */
create_vector_map(line);
cout<<" 1.show passage"<<endl;
cout<<" ----------------"<<endl;
cout<<" 2.search word "<<endl;
cout<<endl;
cout<<"enter your option:"<<endl;
cin>>choice;
if(choice!='1' && choice!='2')
cout<<"error!"<<endl;
else
switch (choice) {
case '1':
print_vector_map();
break;
case '2':
result_1();
break;
}
}
void create_vector_map(string line)
{
while(is)
{
string unit;
while(getline(is,line))
{
auto beg=line.begin();
auto end=line.end();
auto star=beg;
text.push_back(line);
while(beg!=end+1)
{
while(*beg>='a'&&*beg<='z')
{
++beg;
}
unit.assign(star,beg);
++beg;
star=beg;
++word_count[unit];
}
}
}
cout<<"finish create task"<<endl;
}
int search(string word)
{
int clu;
auto beg=word_count.begin();
auto end=word_count.end();
for (const auto &w : word_count)
{
if (w.first!=word && beg!=end)
{
beg++;
}
}
if(beg==end)
clu=0;
else
clu=1;
return clu;
}
void open_file(ifstream &is)
{
is.open("output.txt"); /* 在project文件夹中建立output.txt存放分析文本*/
if(is)
cout<<"file open success!"<<endl;
else
cout<<"file open faild!"<<endl;
}
void print_vector_map()
{
auto beg=text.begin();
auto end=text.end();
while(beg!=end)
{
cout<<*beg++<<endl;
}
for (const auto &w : word_count)
cout<<w.first<<" occurs "<<w.second<<((w.second>1)?" times":" time")<<endl;
}
void result_1()
{
string word;
int clu;
cout<<"please enter word you want to find(press p to exit):"<<endl;
while(1)
{
cin>>word;
if(word=="p")
break;
clu=search(word);
if(clu==1)
cout<<word<<" exit in output.txt"<<endl;
else
cout<<word<<" not exit in output.txt"<<endl;
}
cout<<"exit success"<<endl;
}
程序实现两个功能:(1)统计文本中各个单词的出现次数 (2)查找指定输入单词在文本中是否出现
需注意:练习比较随意,多使用全局变量,正式使用形参实参传递参数比较好。
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
ifstream is;
vector<string> text;
map<string, size_t> word_count;
int main()
{
string line;
char choice;
void create_vector_map(string line); /* 完成分词工作,将文本变为单个单词存入map函数 */
int search(string word); /* 查询功能实现函数 */
void open_file(ifstream &is);
void print_vector_map();
void result_1();
open_file(is); /* 打开文本 */
create_vector_map(line);
cout<<" 1.show passage"<<endl;
cout<<" ----------------"<<endl;
cout<<" 2.search word "<<endl;
cout<<endl;
cout<<"enter your option:"<<endl;
cin>>choice;
if(choice!='1' && choice!='2')
cout<<"error!"<<endl;
else
switch (choice) {
case '1':
print_vector_map();
break;
case '2':
result_1();
break;
}
}
void create_vector_map(string line)
{
while(is)
{
string unit;
while(getline(is,line))
{
auto beg=line.begin();
auto end=line.end();
auto star=beg;
text.push_back(line);
while(beg!=end+1)
{
while(*beg>='a'&&*beg<='z')
{
++beg;
}
unit.assign(star,beg);
++beg;
star=beg;
++word_count[unit];
}
}
}
cout<<"finish create task"<<endl;
}
int search(string word)
{
int clu;
auto beg=word_count.begin();
auto end=word_count.end();
for (const auto &w : word_count)
{
if (w.first!=word && beg!=end)
{
beg++;
}
}
if(beg==end)
clu=0;
else
clu=1;
return clu;
}
void open_file(ifstream &is)
{
is.open("output.txt"); /* 在project文件夹中建立output.txt存放分析文本*/
if(is)
cout<<"file open success!"<<endl;
else
cout<<"file open faild!"<<endl;
}
void print_vector_map()
{
auto beg=text.begin();
auto end=text.end();
while(beg!=end)
{
cout<<*beg++<<endl;
}
for (const auto &w : word_count)
cout<<w.first<<" occurs "<<w.second<<((w.second>1)?" times":" time")<<endl;
}
void result_1()
{
string word;
int clu;
cout<<"please enter word you want to find(press p to exit):"<<endl;
while(1)
{
cin>>word;
if(word=="p")
break;
clu=search(word);
if(clu==1)
cout<<word<<" exit in output.txt"<<endl;
else
cout<<word<<" not exit in output.txt"<<endl;
}
cout<<"exit success"<<endl;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Python中map()函数浅析
- 关于指针的一些事情
- Android使用Google Map浅谈
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- Erlang中的映射组Map详细介绍
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例