急求统计文件中的单词个数的代码(c++)!急……

qzjzjq 2010-10-11 09:12:37
本人从未接触过c++,但因任务需要,需要我一星期内完成这个任务,急求相关代码……
主要功能就是可以选择某个文件或文件夹,在这个文件或文件夹中统计单词个数并分别显示出每个单词的个数。
请大侠们指教一二……
...全文
577 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingshaofengbinbin 2010-10-11
  • 打赏
  • 举报
回复
無_1024 2010-10-11
  • 打赏
  • 举报
回复
貌似还要考虑是不是数字啊 要排除数字啊
petewong 2010-10-11
  • 打赏
  • 举报
回复
从文件开始找空格、标点符号,遇到这些便是一个单词
selooloo 2010-10-11
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <map>
#include <string>

bool isdigit(char ch)
{
return (ch>=' '&&ch<='9');
}

using namespace std;

int main(void)
{
map<string,int> words;
ifstream fin("a.txt");//a是要统计的文件
ofstream fout("b.txt");//结果存放在b中
string str;
int count=0;

if(!fin || !fout)
{
cout<<"open failed!"<<endl;
exit(1);
}
while(fin.good())
{
fin>>str;
words[str]++;
}
fin.close();
for(map<string,int>::iterator mit=words.begin();mit!=words.end();++mit)
{
if(!isdigit((mit->first)[0]))
{
fout<<mit->first<<": "<<mit->second<<endl;
++count;
}
}
fout<<"total: "<<count<<endl;
fout.close();
return 0;
}
qzjzjq 2010-10-11
  • 打赏
  • 举报
回复
恩 ,统计英文单词,字母也算是单词,怎么做啊~大虾们~在线等~
qf17331733 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yyg990441 的回复:]
C/C++ code

#include<string>
#include<map>
#include<fstream>
#include<iostream>
using namespace std;
#define FILE_NAME "E:\\data.txt"
int main(){
ifstream in(FILE_NAME);
map<string,i……
[/Quote]
不对吧~~~~
统计英文单词 而不是 字母,好有挑战感
qzjzjq 2010-10-11
  • 打赏
  • 举报
回复
楼上统计所有的文本,我只想如下:文本文件中只包含英文单词 和 数字 ,只统计单词的总数,和各个单词出现的个数。并且把结果写入到一个文件中。
yyg990441 2010-10-11
  • 打赏
  • 举报
回复

#include<string>
#include<map>
#include<fstream>
#include<iostream>
using namespace std;
#define FILE_NAME "E:\\data.txt"
int main(){
ifstream in(FILE_NAME);
map<string,int> words;
string temp;
//输入
while(in>>temp)
++words[temp];
//输出
cout<<"单词总个数:"<< words.size() <<endl;

for(map<string,int>::iterator it = words.begin();it!=words.end();++it)
cout<<it->first<<":"<<it->second<<endl;
}

测试文件:
E:\\data.txt内容:
This a simple a
This a simple b
This a simple c
This a simple d
This a simple e
This a simple f
yyg990441 2010-10-11
  • 打赏
  • 举报
回复

#include<string>
#include<map>
#include<fstream>
#include<iostream>
#include<sstream>
using namespace std;
#define FILE_NAME "E:\\data.txt"
int main(){
ifstream in(FILE_NAME);
map<string,int> words;
string temp;
//输入
while(in>>temp){
istringstream istrm(temp);
double d;
if(!(istrm>>d)) ++words[temp];
}
//输出
cout<<"单词总个数:"<< words.size() <<endl;

for(map<string,int>::iterator it = words.begin();it!=words.end();++it)
cout<<it->first<<":"<<it->second<<endl;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧