在已完成单词次数统计的C++程序的基础上,按词频降序排列输出
已有程序:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<map>
#include<set>
using namespace std;
int main()
{
ifstream myfile("文本.txt", ios::in);
string str;
if (!myfile.is_open())
{
cout << "Some errors" << endl;
}
while (getline(myfile, str))
{
//把大写字母变成小写字母
for (int a = 0; a < str.length(); a++)
{
if (str[a] >= 'A' && str[a] <= 'Z')
{
str[a] = str[a] + 32;
}
}
//把文章中的标点符号变成空格
for (int b = 0; b < str.length(); b++)
{
if (ispunct(str[b]))
str[b] = ' ';
}
//统计单词个数
stringstream ss(str);
string s;
vector<string>wordcount;
while (getline(ss, s, ' '))
{
wordcount.push_back(s);
}
map<string, int>m1;
for (int i = 0; i < wordcount.size(); i++)
{
m1[wordcount[i]] += 1;
}
map<string, int>::iterator word;
cout << "单词次数统计:" << endl;
for (word = m1.begin(); word != m1.end(); word++)
{
cout << word->first << "=" << word->second << endl;
}
}
myfile.close();
return 0;
}
文本附件:
Our best estimates show there are over 7 billion people on the planet and 300 billion stars in the Milky Way galaxy. By comparison, the adult human body contains 37 trillion cells. To determine the function and relationship among these cells is a monumental undertaking. Many areas of human health would be impacted if we better understand cellular activity. A problem with this much data is a great match for the Kaggle community.Just as the Human Genome Project mapped the entirety of human DNA, the Human BioMolecular Atlas Program is a major endeavor. Sponsored by the National Institutes of Health , HuBMAP is working to catalyze the development of a framework for mapping the human body at a level of glomeruli functional tissue units for the first time in history. Hoping to become one of the world's largest collaborative biological projects, HuBMAP aims to be an open map of the human body at the cellular level.This competition, "Hacking the Kidney," starts by mapping the human kidney at single cell resolution.