要求有头文件、main文件、函数源文件!!!
代码:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<map>
using namespace std;
int main()
{
int n = 0;
ifstream myfile("文本.txt");
ifstream infile("文本1.txt");
string str;
string str1[2000];
//定义比较类
struct ValueComparator
{
bool operator()(const pair<string, int>& x, const pair<string, int>& y)
{
return x.second > y.second;
}
} comparator;
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] = ' ';
}
//1.统计单词个数
stringstream ss(str);
string s;
vector<string>wordcount;
while (getline(ss, s, ' '))
{
if (s.length() > 0)
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;
}
cout << " " << endl;
//2.按词频输出
// 把 map 里的键和值对存到 vector 里,然后按值排序后输出
vector<pair<string, int> > pairs;
for (auto& it : m1)
{
pairs.push_back(it);
}
sort(pairs.begin(), pairs.end(), comparator);
cout << "词频降序输出:" << endl;
for (auto it = pairs.begin(); it != pairs.end(); ++it)
{
cout << it->first << "=" << it->second << endl;
}
cout << " " << endl;
}
//3.倒序输出
cout << "倒序输出:" << endl;
if (!infile.is_open())
{
cout << "Some errors" << endl;
}
while (getline(infile, str))
{
//把文章中的标点符号变成空格
for (int b = 0; b < (str.length()); b++)
{
if (ispunct(str[b]))
str[b] = ' ';
}
//逆置整个文章
reverse(str.begin(), str.end());
auto start = str.begin();
//找空格,逆置每个单词
while (start != str.end())
{
auto end = start;
while (end != str.end() && *end != ' ')
end++;
reverse(start, end);
if (end == str.end())
{
start = end;
}
else
{
start = end + 1;
}
}
str1[n] = str;
n++;
}
for (int s = n - 1; s >= 0; s--)
cout << str1[s] << endl;
}
资源文件:
文本:
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.
文本1:
Our best estimates show thOur 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.