统计一个词在txt文件中出现的次数
Count number of times a word is found in txt file
目前正在编写这个简单的代码,试图弄清楚为什么它不是每次都计算特定的单词。
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int t = 0;
int a1 = 0;
string a[100];
ifstream food;
food.open("food.txt");
if (food.fail())
{
cout << "File can't open" << endl;
}
else
cout << "File successfully opened" << endl;
int i = 0;
while (!food.eof())
{
// Tomato
food >> a[i];
if (a[i] == "tomato")
{
t = t + 1;
}
i++;
// Apple
food >> a[i];
if (a[i] == "apple")
{
a1 = a1 + 1;
}
i++;
}
cout << "Amount of Tomatos: " << t << endl;
cout << "Amount of Apples: " << a1 << endl;
}
我正在使用的文本文件:
apple
apple
tomato
apple
tomato
tomato
输出:
File successfully opened
Amount of Tomatoes: 2
Amount of Apples: 2
目的是找出列表中每种食物的数量。我目前只使用两种食物,但还会有更多。
您的代码有几个问题。
在循环中错误地使用了 eof()
。在先执行读操作之前不能检查eof()
。
使用没有边界检查的数组。就此而言,您根本不需要数组。
跳过单词,这就是为什么您没有计算您期望的所有内容。让我们来看看第一行,apple
。因为它不是 "tomato"
,所以你跳过它并读取文件中的下一个单词,即 "apple"
,所以你计算它。但是你根本没有把第一个apple
算进去。
你需要做更多像这样的事情:
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int tomatoes = 0;
int apples = 0;
string s;
ifstream food;
food.open("food.txt");
if (!food.is_open())
{
cout << "File can't open" << endl;
return 0;
}
cout << "File successfully opened" << endl;
while (food >> s)
{
// Tomato
if (s == "tomato")
++tomatoes;
// Apple
else if (s == "apple")
++apples;
}
cout << "Amount of Tomatos: " << tomatoes << endl;
cout << "Amount of Apples: " << apples << endl;
return 0;
}
或者,正如@user463035818 在评论中提到的,您可以使用 std::map
代替:
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> foods;
string s;
ifstream food;
food.open("food.txt");
if (!food.is_open())
{
cout << "File can't open" << endl;
return 0;
}
cout << "File successfully opened" << endl;
while (food >> s) {
foods[s]++;
}
for (map<string, int>::iterator iter = foods.begin(); iter != foods.end(); ++iter) {
cout << "Amount of " << iter->first << ": " << iter->second << endl;
}
/* or, if you are using C++11 or later...
for (auto &item : foods) {
cout << "Amount of " << item.first << ": " << item.second << endl;
}
*/
return 0;
}
目前正在编写这个简单的代码,试图弄清楚为什么它不是每次都计算特定的单词。
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int t = 0;
int a1 = 0;
string a[100];
ifstream food;
food.open("food.txt");
if (food.fail())
{
cout << "File can't open" << endl;
}
else
cout << "File successfully opened" << endl;
int i = 0;
while (!food.eof())
{
// Tomato
food >> a[i];
if (a[i] == "tomato")
{
t = t + 1;
}
i++;
// Apple
food >> a[i];
if (a[i] == "apple")
{
a1 = a1 + 1;
}
i++;
}
cout << "Amount of Tomatos: " << t << endl;
cout << "Amount of Apples: " << a1 << endl;
}
我正在使用的文本文件:
apple
apple
tomato
apple
tomato
tomato
输出:
File successfully opened
Amount of Tomatoes: 2
Amount of Apples: 2
目的是找出列表中每种食物的数量。我目前只使用两种食物,但还会有更多。
您的代码有几个问题。
在循环中错误地使用了
eof()
。在先执行读操作之前不能检查eof()
。使用没有边界检查的数组。就此而言,您根本不需要数组。
跳过单词,这就是为什么您没有计算您期望的所有内容。让我们来看看第一行,
apple
。因为它不是"tomato"
,所以你跳过它并读取文件中的下一个单词,即"apple"
,所以你计算它。但是你根本没有把第一个apple
算进去。
你需要做更多像这样的事情:
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int tomatoes = 0;
int apples = 0;
string s;
ifstream food;
food.open("food.txt");
if (!food.is_open())
{
cout << "File can't open" << endl;
return 0;
}
cout << "File successfully opened" << endl;
while (food >> s)
{
// Tomato
if (s == "tomato")
++tomatoes;
// Apple
else if (s == "apple")
++apples;
}
cout << "Amount of Tomatos: " << tomatoes << endl;
cout << "Amount of Apples: " << apples << endl;
return 0;
}
或者,正如@user463035818 在评论中提到的,您可以使用 std::map
代替:
#include "pch.h"
#include <fstream>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> foods;
string s;
ifstream food;
food.open("food.txt");
if (!food.is_open())
{
cout << "File can't open" << endl;
return 0;
}
cout << "File successfully opened" << endl;
while (food >> s) {
foods[s]++;
}
for (map<string, int>::iterator iter = foods.begin(); iter != foods.end(); ++iter) {
cout << "Amount of " << iter->first << ": " << iter->second << endl;
}
/* or, if you are using C++11 or later...
for (auto &item : foods) {
cout << "Amount of " << item.first << ": " << item.second << endl;
}
*/
return 0;
}