C语言,高手进
发布网友
发布时间:2022-05-19 23:12
我来回答
共2个回答
热心网友
时间:2023-10-19 23:54
#include <map>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstddef>
#include <iomanip>
using namespace std;
int main()
{
string fileName;
cout << "请输入要读入文件的名称: ";
cin >> fileName;
ifstream inFile(fileName.c_str());
if(!inFile)
throw;
cout << "请输入要存入文件的名称: ";
cin >> fileName;
ofstream outFile(fileName.c_str());
if(!outFile)
throw;
string contents;
inFile.unsetf(ios_base::skipws);
char c;
while(inFile >> c)
{
contents.push_back(c);
}
inFile.clear();
inFile.seekg(ios_base::beg);
while(contents.find_first_of(".,!?:{}\"':></+=|\\_-)(@#$%^&*0123456789") != string::npos)
{
contents.erase(contents.find_first_of(".,!?:{}\"':></+=|\\_-)(@#$%^&*0123456789"), 1);
}
map<string, size_t> words;
istringstream sstrm(contents);
contents.clear();
string word;
while(sstrm >> word)
if(!words.insert(map<string, size_t>::value_type(word, 1)).second)
++words[word];
string line;
while(getline(inFile, line))
{
if(line.size() != 0)
contents += line;
}
map<string, size_t> lines;
line.clear();
sstrm.seekg(ios_base::beg);
sstrm.clear();
sstrm.str(contents);
sstrm.unsetf(ios_base::skipws);
while(sstrm >> c)
{
if(c != '"')
line.push_back(c);
if(c == '.' || c == '?' || c == '!')
{
line.erase(0, line.find_first_not_of(' '));
if(!lines.insert(map<string, size_t>::value_type(line, 1)).second)
++lines[line];
cout << line << '\n';
outFile << line << '\n';
line.clear();
}
}
system("pause");
cout << "单词统计: \n\n";
size_t totalWords = 0;
for(map<string, size_t>::iterator it = words.begin(); it != words.end(); ++it)
{
cout << setw(15) << it->first << " : " << setw(10) << it->second << "次\n";
totalWords += it->second;
}
cout << "句子统计: \n\n";
size_t totalLines = 0;
for(map<string, size_t>::iterator it = lines.begin(); it != lines.end(); ++it)
{
cout << it->first << " : " << setw(10) << it->second << "次\n";
totalLines += it->second;
}
cout << "一共有单词: " << totalWords << "个\n";
cout << "一共有句子: " << totalLines << "条" << endl;
}
in.txt 内容:
"Yes, that's true."
"When Neil Armstrong set foot on the moon, he said 'One small step for man, one giant leap for mankind. With these words he summed up how it felt to be the first man to set foot on the moon, drawing with him all the people who had lived before him. It was not his merit alone, obviously.
"In our own time we also have completely new problems to face. The most serious are those of the environment. A central philosophical direction in the twentieth century is therefore ecophilosophy or ecosophy, as one of its founders the Norwegian philosopher Arne Naess has called if. Many ecophilosophers in the western world have warned that western civilization as a whole is on a fundamentally wrong track, racing toward a head-on collision with the limits of what our planet can tolerate. They have tried to take soundings that go deeper than the concrete effects of pollution and environmental destruction. There is something basically wrong with western thought, they claim."
"I think they are right."
out.txt 内容:
Yes, that's true.
When Neil Armstrong set foot on the moon, he said 'One small step for man, one giant leap for mankind.
With these words he summed up how it felt to be the first man to set foot on the moon, drawing with him all the people who had lived before him.
It was not his merit alone, obviously.
In our own time we also have completely new problems to face.
The most serious are those of the environment.
A central philosophical direction in the twentieth century is therefore ecophilosophy or ecosophy, as one of its founders the Norwegian philosopher Arne Naess has called if.
Many ecophilosophers in the western world have warned that western civilization as a whole is on a fundamentally wrong track, racing toward a head-on collision with the limits of what our planet can tolerate.
They have tried to take soundings that go deeper than the concrete effects of pollution and environmental destruction.
There is something basically wrong with western thought, they claim.
I think they are right.
测试:
请输入要读入文件的名称: in.txt
请输入要存入文件的名称: out.txt
Yes, that's true.
When Neil Armstrong set foot on the moon, he said 'One small step for man, one giant leap for mankind.
With these words he summed up how it felt to be the first man to set foot on the moon, drawing with him all the people who had lived before him.
It was not his merit alone, obviously.
In our own time we also have completely new problems to face.
The most serious are those of the environment.
A central philosophical direction in the twentieth century is therefore ecophilosophy or ecosophy, as one of its founders the Norwegian philosopher Arne Naess has called if.
Many ecophilosophers in the western world have warned that western civilization as a whole is on a fundamentally wrong track, racing toward a head-on collision with the limits of what our planet can tolerate.
They have tried to take soundings that go deeper than the concrete effects of pollution and environmental destruction.
There is something basically wrong with western thought, they claim.
I think they are right.
请按任意键继续. . .
单词统计:
A : 1次
Armstrong : 1次
Arne : 1次
I : 1次
In : 1次
It : 1次
Many : 1次
Naess : 1次
Neil : 1次
Norwegian : 1次
One : 1次
The : 1次
There : 1次
They : 1次
When : 1次
With : 1次
Yes : 1次
a : 3次
all : 1次
alone : 1次
also : 1次
and : 1次
are : 2次
as : 2次
basically : 1次
be : 1次
before : 1次
called : 1次
can : 1次
central : 1次
century : 1次
civilization : 1次
claim : 1次
collision : 1次
completely : 1次
concrete : 1次
deeper : 1次
destruction : 1次
direction : 1次
drawing : 1次
ecophilosophers : 1次
ecophilosophy : 1次
ecosophy : 1次
effects : 1次
environment : 1次
environmental : 1次
face : 1次
felt : 1次
first : 1次
foot : 2次
for : 2次
founders : 1次
fundamentally : 1次
giant : 1次
go : 1次
had : 1次
has : 1次
have : 3次
he : 2次
headon : 1次
him : 2次
his : 1次
how : 1次
if : 1次
in : 2次
is : 3次
it : 1次
its : 1次
leap : 1次
limits : 1次
lived : 1次
man : 2次
mankind : 1次
merit : 1次
moon : 2次
most : 1次
new : 1次
not : 1次
obviously : 1次
of : 4次
on : 3次
one : 2次
or : 1次
our : 2次
own : 1次
people : 1次
philosopher : 1次
philosophical : 1次
planet : 1次
pollution : 1次
problems : 1次
racing : 1次
right : 1次
said : 1次
serious : 1次
set : 2次
small : 1次
something : 1次
soundings : 1次
step : 1次
summed : 1次
take : 1次
than : 1次
that : 2次
thats : 1次
the : 10次
therefore : 1次
these : 1次
they : 2次
think : 1次
those : 1次
thought : 1次
time : 1次
to : 4次
tolerate : 1次
toward : 1次
track : 1次
tried : 1次
true : 1次
twentieth : 1次
up : 1次
warned : 1次
was : 1次
we : 1次
western : 3次
what : 1次
who : 1次
whole : 1次
with : 3次
words : 1次
world : 1次
wrong : 2次
句子统计:
A central philosophical direction in the twentieth century is therefore ecophilosophy or ecosophy, as one of its founders the Norwegian philosopher Arne Naess has called if. : 1次
I think they are right. : 1次
In our own time we also have completely new problems to face. : 1次
It was not his merit alone, obviously. : 1次
Many ecophilosophers in the western world have warned that western civilization as a whole is on a fundamentally wrong track, racing toward a head-on collision with the limits of what our planet can tolerate. : 1次
The most serious are those of the environment. : 1次
There is something basically wrong with western thought, they claim. : 1次
They have tried to take soundings that go deeper than the concrete effects of pollution and environmental destruction. : 1次
When Neil Armstrong set foot on the moon, he said 'One small step for man, one giant leap for mankind. : 1次
With these words he summed up how it felt to be the first man to set foot on the moon, drawing with him all the people who had lived before him. : 1次
Yes, that's true. : 1次
一共有单词: 174个
一共有句子: 11条
请按任意键继续. . .
热心网友
时间:2023-10-19 23:55
单词就是找各空格的位置。句子就是找小数点的位置。从头一句一句地编太累了,而且做好的程序没有可重用性,以后也用不上了太浪费。用以前的可重用的模块来编估计这个程序总量要超过1万行了,可能你也看不太懂了。