用 C++ 编程统计英文文本中的单词数量和出现次数

需求: 统计若干段英文文本中的单词数量,并且统计每个单词出现的次数。

注:

  1. 单词之间以空格(一个或多个空格)为间隔。
  2. 忽略空行或者空格行。

实现方案:

基本版:

  • 统计时,区分字母大小写,且不删除指定标点符号。

进阶版:

  • 统计前,需要从文字中删除指定标点符号 '!', '.', ',', ':', '*', '?', ' '。注意:所谓的删除,就是用一个空格替换掉相应字符。
  • 统计单词时需要忽略单词的大小写。

输入说明:

若干行英文,最后以 '!!!!!' 为结束。

输出说明:

  • 单词数量
  • 出现次数排名前 10 的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

输入样例 1:

failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
          
when you are wondering whether new money it has laid
background because of you then at the heart of the
     
most lax alert and most low awareness and left it

godsend failed
!!!!!

输出样例 1:

46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2

输入样例 2:

Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!!!

输出样例 2:

54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2

代码实现:

请参考以下 C++ 代码实现:

// ... (代码实现)

注意:

  • 请根据需求修改代码实现。
  • 代码示例仅供参考,可能需要根据实际情况进行调整。
C++ 统计英文文本单词数量和出现次数 - 基本版和进阶版

原文地址: https://www.cveoy.top/t/topic/oelN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录