【软件工程】统计文章字数和出现频率较高词汇的程序实现

代码功能:

能够实现统计所给任意段落单词的总数和排序频率较高词汇的功能。

文章字数不限,高频词汇取前十个。

 

编程语言:c语言

具体代码:

功能实现:

为了方便调试,选择从文件读取相应的段落。先在工程项目目录下建立:字数统计.txt。输入编译测试需要的段落文章。

这里我随机从电脑里找的英语文章段落:

Of the forces shaping higher education none is more sweeping than the movement across borders. Over the past three decades the number of students leaving home each year to study abroad has grown at an annual rate of 3.9 percent, from 800,000 in 1975 to 2.5 million in 2004. Most travel from one developed nation to another, but the flow from developing to developed countries is growing rapidly. The reverse flow, from developed to developing countries, is on the rise, too. Today foreign students earn 30 percent of the doctoral degrees awarded in the United States and 38 percent of those in the United Kingdom. And the number crossing borders for undergraduate study is growing as well, to 8 percent of the undergraduates at America’s best institutions and 10 percent of all undergraduates in the U.K. In the United States, 20 percent of the newly hired professors in science and engineering are foreign-born, and in China many newly hired faculty members at the top research universities received their graduate education abroad 

准备工作就绪后,编译测试就可以。

我们可以从第一个开始依次往后对,如果单词相同,则对比的单词结构体int 值k相应加1.直到对比完成。依次对比,首先出现的单词和后面出现的同一个单词相比,次数统计的最完善。

例如:my name is ren guo qing. what is your name/p>

这里name出现两次,我们按照上面的方法排序后第一个name结构体的k=2,第二个k=1.is的类似。

这样好像找到了最大次数,但是新的问题出现了。

例如:my name is ren guo qing. what is your namehis name is zhang san.

这里name的k值从左到右依次是3、2、1.等到排序后发现name=2的值排在了第二位,这又是重复了。name只需要一个k=3即可,其他的不需要出现。所以我们需要改变除了第一个出现的单词的k以外的其他所有同个单词的k值。我们可以倒序对比,然后将他们置为1.这样就实现了只有一个最大值,在排序后不会出现重复了。

接下来就是选择一种排序方法,按照k的值,排序输出前十个高频词汇。这里选择的是冒泡排序。

 

 

到这里为止,貌似大功告成了。但是还有一些缺陷。

①我们以空格符分析单词的时候,没有考虑标点符 ,这会引起误差。单词与符 组合和多个空格出现的情况。

②文件最后必须多加一个空格符才能正确读取到文件结尾,这个不符合用户习惯。

  =====================2014/2/26  19:50=========================

如果考虑标点符 ,那么就需要对单词的每个字符都要做到精确选择。所以我们在读取每个字符的时候,进行判断是否需要加入单词。

这里我们只选取0-9、a-z、A-Z的字符,如果是其他的字符,则说明不属于单词的一部分,并且到此可以读取出一个单词。并且此时我们也解决了最后字符的问题。因为一篇文章最后总是以符 结尾,问 、感叹 或者句 等等。这样我们输出的时候就不需要最后是否需要输入空格键了。

另外也对打印结果进行了重新的布局,单词左对齐,出现次数右对齐。

修改后代码:

打印结果:

例如:Over the past three decades the number of students leaving home each year to study abroad has grown at an annual rate of 3.9 percent, from 800,000 in 1975 to 2.5 million in 2004.

这一句子里,3.9,800,000和2.5都因为标点符 的加入,读取的时候,程序将其分为两个单词计算。我们就要想办法解决数字的问题。

 =========================2014.2.28  15_53===========================

数字问题怎么解决啊。(⊙o⊙)…

=========================2014.3.2 22.25============================

文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览113477 人正在系统学习中 相关资源:下拉通刷词软件v3.1.zip-其它代码类资源-CSDN文库

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2014年1月23日
下一篇 2014年1月23日

相关推荐