C++统计给定文本中每个单词出现的次数并输出结果到文件

思路:将给定文件按行读入–>去除每行中出现的非字母字符–>以空格分隔读入每个单词–>统计单词出现的次数。

按行读入文件并保存到一个string中
	string tmp, strFile;
	ifstream file("1.txt");
	while (getline(file, tmp))
	{
		strFile.append(tmp);
		tmp.clear();
	}

这样做的结果是将文件中的所有行按顺序保存在了一个string中

去除非字母字符
for (size_t i = 0; i < strFile.length(); i++)
{
	if (ispunct(strFile[i]))
		strFile[i] = ' ';
}

将所有非字母字符以空格替代,这样使得输入文件中的单词间均已空格间隔

统计单词
stringstream ss(strFile);
string word;
while (ss >> word)
{
	number_of_words++;
	unordered_map<string, int>::iterator it = strMap.find(word);
	if (it == strMap.end())
		strMap.insert(unordered_map<string, int>::value_type(word, 1));
	else
		strMap[word]++;
}

首先使用stringstream从strFile中读取string,然后按空格间隔读取每个单词并统计:对于不存在的单词在unordered_map中增加一个键值对,存在则只增加计数。

结果写入文件
	freopen("out.txt", "w", stdout);
	printf("---------------------------------------------------\n");
	printf("%d words be counted! \nDetails are as follow:\n", number_of_words);
	printf("word		times		frequency\n");
	printf("---------------------------------------------------\n");
	unordered_map<string, int>::const_iterator it;

	for (it = strMap.begin(); it != strMap.end(); it++)
	{
		printf("%-20s %-5d %17.2lf%%\n", (it->first).c_str(), it->second, it->second*100.0/number_of_words);
	}

这里使用freopen函数将统计结果写入文件。

完整代码
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <cctype>
using namespace std;

int number_of_words = 0;
unordered_map<string, int> strMap;
void getCount(stringstream &ss);
int main()
{
	//读取文件并存入string
	string tmp, strFile;
	ifstream file("1.txt");
	while (getline(file, tmp))
	{
		strFile.append(tmp);
		tmp.clear();
	}
	//去除非字母字符
	for (size_t i = 0; i < strFile.length(); i++)
	{
		if (ispunct(strFile[i]))
			strFile[i] = ' ';
	}
	//依次读取单词并统计次数
	stringstream ss(strFile);
	getCount(ss);

	freopen("out.txt", "w", stdout);
	printf("---------------------------------------------------\n");
	printf("%d words be counted! \nDetails are as follow:\n", number_of_words);
	printf("word		times		frequency\n");
	printf("---------------------------------------------------\n");
	//打印统计结果
	unordered_map<string, int>::const_iterator it;

	for (it = strMap.begin(); it != strMap.end(); it++)
	{
		printf("%-20s %-5d %17.2lf%%\n", (it->first).c_str(), it->second, it->second*100.0/number_of_words);
	}


	return 0;
}
void getCount(stringstream &ss)
{
	string word;
	while (ss >> word)
	{
		number_of_words++;
		unordered_map<string, int>::iterator it = strMap.find(word);
		if (it == strMap.end())
			strMap.insert(unordered_map<string, int>::value_type(word, 1));
		else
			strMap[word]++;
	}
}
  • 21
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以及每个字母出现次数,并按照字母表顺序输出结果。 以下是一份可能的实现: ```c #include <stdio.h> #include <ctype.h> #define MAX_WORD_LEN 100 int main(void) { FILE *fp; int i, c, word_count = 0; int letter_count[26] = {0}; char word[MAX_WORD_LEN+1]; fp = fopen("text.txt", "r"); if (fp == NULL) { perror("text.txt"); return 1; } while ((c = fgetc(fp)) != EOF) { if (isalpha(c)) { letter_count[toupper(c) - 'A']++; if (word_count == 0) { word[0] = toupper(c); word_count = 1; } else if (word_count < MAX_WORD_LEN) { word[word_count] = tolower(c); word_count++; } } else if (word_count > 0) { word[word_count] = '\0'; printf("%s\n", word); word_count = 0; } } if (word_count > 0) { word[word_count] = '\0'; printf("%s\n", word); } fclose(fp); printf("Letter counts:\n"); for (i = 0; i < 26; i++) { printf("%c: %d\n", 'A' + i, letter_count[i]); } return 0; } ``` 这个程序首先打开一个名为 `text.txt` 的文本文件(可以修改为其他文件名),并在循环逐个读取文件的字符。如果字符是字母(由 `isalpha` 函数判断),则将它的出现次数计入 `letter_count` 数组相应的元素,并将它添加到当前单词单词的最大长度为 `MAX_WORD_LEN`,在超过这个长度时将被截断。当遇到非字母字符时,如果当前有一个单词正在被构建,则将其打印出来,并将单词计数器归零。最后如果当前还有一个单词未被打印,则将其打印出来。 最后,程序输出每个字母的出现次数,按照字母表顺序排列。注意,这里使用了 `toupper` 和 `tolower` 函数将大写字母转换为小写字母,并将第一个字母转换为大写字母。 这个程序仅作为一个简单的示例,实际的文本处理程序可能需要更复杂的逻辑,例如考虑标点符号、数字和其他特殊字符的影响,以及支持更多的文本编码格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值