⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.txt

📁 字符数统计 字符数统计 字符数统计
💻 TXT
字号:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include <map>

using namespace std;

//#include <string.h>
//#include <stdio.h> #include <stdlib.h>

int main()
{
ifstream ifile("test.txt");
map<string, int>strmap; // 定义map
map<string, int>::iterator it;// 定义map迭代器
string str, w;
int line=0, words=0, i;

/* 构造单词字符集*/
string pum("_");
for(i='A'; i<'Z'+1; i++) pum+=i;
for(i='a'; i<'z'+1; i++) pum+=i;
for(i='0'; i<'9'+1; i++) pum+=i;

string::size_type index, end;
while(!ifile.eof())
{
getline(ifile, str);
line++;
index=str.find_first_of(pum); //寻找单词起点
while(index != string::npos)
{
end = str.find_first_not_of(pum, index); //寻找单词终点
words++; //++
w = str.substr(index, end-index); //截取单词
it = strmap.find(w);
if(it == strmap.end())
strmap.insert(make_pair(w, 1));
else it->second++;

if(end == string::npos)break; //如果行结束, 则break跳出
else
index=str.find_first_of(pum, end); //否则寻找下一个起点
}
}

cout<<"Result:"<<endl
<<"Total line(s) is: "<<line<<endl
<<"Total word(s) is: "<<words<<endl
<<"Count word \"if\":"<<strmap["if"]<<endl
<<"Count word \"while\":"<<strmap["while"]<<endl
<<"Count word \"case\":"<<strmap["case"]<<endl;

cout<<endl<<"There are such word(s) in this file:"<<endl;
for(it = strmap.begin(); it!=strmap.end(); it++)
cout<<"Word:\""<<it->first<<"\", Count:"<<it->second<<endl; 
system("pause");
return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -