1) 读入与 Linux 相关的英文文章(每个同学自己从网上下载,
件名称作为参数,统一转换为文本文件),文章不少于 2000 单
词,保留文章的段落结构,可以去掉空行,但读入前不得全部
转化为一行;
2) 统计不同单词在文章中出现的频次,不统计虚词(例如 a、the、
this、of 等。
3) 根据参数 N 按照从高到低顺序格式化输出前 N 个单词出现的
频次及比例,默认前 50 个单词。
源代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <list.h>#define LENGTHMAX 50
#define MAX 2000000typedef struct Wnode{char word[LENGTHMAX];int count;char st;struct list_head list;
}Wnode;int wtotal = 0;char *Init_array(char array[]) //初始化数组
{     FILE *fp;char ch;int i,j;int length = strlen(array);fp = fopen("linux.txt", "r");for(i=0;(ch=fgetc(fp))!=EOF;i++)array[i] = ch;length = i;for(i=0; i<length; i++){if(array[i]=='-'){for(j=i+1; j<length; j++)array[j-1] = array[j];length--;}  else if(array[i]>='A' && array[i]<='Z')array[i] +=32;else if(array[i]>='a' && array[i]<='z')array[i]= array[i];else array[i] = ' ';}array[i] = '\0';} int isFunctionwords(char *str)  //是否为虚词
{   int i ,flag=1;char funcwords[84][20]= {{"of"},{"to"},{"in"},{"and"},{"as"},{"from"},{"for"},{"with"},{"that"},{"have"},{"by"},{"on"},{"upon"},{"about"},{"above"},{"across"},{"among"},{"ahead"},{"after"},{"a"},{"an"},{"although"},{"at"},{"also"},{"along"},{"around"},{"always"},{"away"},{"any"},{"up"},{"under"},{"until"},{"before"},{"between"},{"beyond"},{"behind"},{"because"},{"what"},{"when"},{"would"},{"could"},{"who"},{"whom"},{"whose"},{"which"},{"where"},{"why"},{"without"},{"whether"},{"down"},{"during"},{"despite"},{"over"},{"off"},{"only"},{"other"},{"out"},{"than"},{"the"},{"then"},{"through"},{"throughout"},{"that"},{"these"},{"this"},{"those"},{"there"},{"therefore"},{"till"},{"some"},{"such"},{"since"},{"so"},{"can"},{"many"},{"much"},{"more"},{"may"},{"might"},{"must"},{"ever"},{"even"},{"every"},{"each"}};for(i=0;i<84;i++){   if(!strcmp(str,funcwords[i])){return flag;}}return flag=0;
}char *delmarks(char word[])  //去单词的引号
{    int i;int length = strlen(word);if(word[0]=='\''){for(i=1;word[i]!='\0';i++){word[i-1]=word[i];}word[i-1] = '\0';}if(word[length-1]=='\'')   word[length-1] = '\0';if(word[length-2]=='\'')   word[length-2] = '\0';return word;
} void Extractwords(char array[],struct list_head *head)  //提取单词
{char word[LENGTHMAX];char *pword;int i,num;for(pword=array;*pword!='\0';pword++){num = 0;while(*pword!='\0'&&*pword!=' '){word[num++] = *pword++;}word[num] = '\0';if(num>0){//提取单词成功 if(!isFunctionwords(word)){if(word[0]=='\''||word[num-1]=='\''){strcpy(word,delmarks(word));}Wnode *my_word_node=(Wnode *)malloc(sizeof(Wnode));my_word_node->count = 1;my_word_node->st = 'N'; strcpy(my_word_node->word,word);list_add_tail(&(my_word_node->list),head);wtotal++;memset(word,0,sizeof(word));}}}printf("           提取所有单词后的单词数(不包括虚词): %d\n",wtotal);printf("*****************************************************\n");
} int CountWordnum(struct list_head *head,char word[])  //计算单词出现次数
{    Wnode *tmpword;//标记要统计的单词int count = 0;struct list_head *pos;//遍历链表list_for_each(pos,head){tmpword=list_entry(pos,Wnode,list);if(tmpword->st=='N'){ //判断是否已经访问过 if(!(strcmp(tmpword->word,word))){tmpword->st = 'Y';count++;}}} return count;
} void delword(struct list_head *head)  //删除状态为Y的结点
{Wnode *tmpword;struct list_head *pos;list_for_each(pos,head){tmpword = list_entry(pos,Wnode,list);if(tmpword->st=='Y'){list_del(pos);wtotal--;}}
}void sort(struct list_head *head)  //对单词出现的次数进行排序
{int max,tmpcount;char word[50];Wnode *tmpword,*tmp,*lword,*kword;struct list_head *p,*q,*l,*k;for(p=head->next;p!=head;p=p->next){tmpword=list_entry(p,Wnode,list);max=tmpword->count;for(q=p->next;q!=head;q=q->next){tmp=list_entry(q,Wnode,list);if(max<tmp->count){max = tmp->count;l = q;}}if(max!=tmpword->count){//当前的max不是最大值,最大值要进行交换 kword = list_entry(p,Wnode,list);lword = list_entry(l,Wnode,list);strcpy(word,kword->word);tmpcount = kword->count;strcpy(kword->word,lword->word);kword->count = lword->count;strcpy(lword->word,word);lword->count = tmpcount;}}} void showword(struct list_head *head)  //输出前N个单词
{Wnode *tmpword;struct list_head *p;int i = 0,n;printf("请选择前N个单词的出现次数,默认前50个\n");scanf("%d",&n);printf("\n前%d个单词出现的次数及频率->  \n",n);printf("\n<---------------------------------------------->\n");printf("\n单词                  次数               频率     ");printf("\n<---------------------------------------------->\n");for(p=head->next;p!=head&&i<n;p=p->next,i++){tmpword = list_entry(p,Wnode,list);printf("%-15s        %-5d              %-6.2f",tmpword->word,tmpword->count,(float)tmpword->count/wtotal);printf("\n<---------------------------------------------->\n");}
} int main()
{   Wnode wordlisthead;INIT_LIST_HEAD(&wordlisthead.list);Wnode *tmp;struct list_head *pos;char array[MAX];int count;Init_array(array); //提取文件中的所有字符 printf("                                   文章全文  \n%s\n",array); printf("*****************************************************\n");//提取单词Extractwords(array,&wordlisthead.list);//统计单词出现次数list_for_each(pos,&wordlisthead.list){count = 0;tmp = list_entry(pos,Wnode,list);if(tmp->st=='N'){count = CountWordnum(pos,tmp->word);tmp->count = count;}} delword(&wordlisthead.list);  //删除结点状态为Y的结点sort(&wordlisthead.list);showword(&wordlisthead.list);
}

C语言统计单词出现的频次并排序输出相关推荐

  1. python统计单词个数算法_python 统计单词个数和频次

    开始学习python,习题需要统计单词个数和频次.百度找到的代码好像都有问题.自己写了一个,调试通过. 环境:python: 3.9.1 64bit :  pycharm: 2020.2  电脑 wi ...

  2. python 统计单词个数和频次 和 70篇短文突破中考英语词汇 实用

    开始学习python,习题需要统计单词个数和频次.百度找到的代码好像都有问题.自己写了一个,调试通过. 环境:python: 3.9.1 64bit :  pycharm: 2020.2  电脑 wi ...

  3. c语言 程序统计注释比例,C语言统计单词数量程序 超详解

    /*************************************************************************************************** ...

  4. C语言-统计单词个数

    目录 1 算法思想 2 实现1 3 实现2 1 算法思想 读取输入进来的一个字符串,统计其中单词的个数,由于每个单词字母不一样,长度不一样,所以来依靠识别单词来统计单词数是比较难的,下面观察一个字符串 ...

  5. c语言统计单词字母个数,C语言统计单词个数

    Q:输入一串字符串,输出其中有多少个单词. Eg:Good Wishes A: #include #include #define SIZE 20int main() {    char str[SI ...

  6. PTA c语言 统计单词的长度

    本题目要求编写程序,输入一行字符,统计每个单词的长度.所谓"单词"是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入给出一行字符. 输出格式: 在 ...

  7. [C语言]统计单词:输入一行字符(其中仅包含英文字母和空格),用函数编程统计其中有多少单词。假设单词之间以空格分开。

    输入 输入一行字符(其中仅包含英文字母和空格),并且长度不超过20. 输出 输出单词数量,单词之间以空格分开. 输入示例 I am a student 输出示例 4 数据范围 输入为字符串,并且长度不 ...

  8. c语言编程统计单词的个数,使用c语言如何统计单词个数

    使用c语言如何统计单词个数 发布时间:2020-04-21 13:58:58 来源:亿速云 阅读:207 作者:小新 使用c语言如何统计单词个数?相信有很多人都不太了解,今天小编为了让大家更加了解Go ...

  9. C语言怎么实现单词下落,如何用c语言实现单词统计

    如何用c语言实现单词统计 输入一串字符串,输出其中有多少个单词. 代码如下:#include #include #define SIZE 20 int main(){ char str[SIZE]={ ...

最新文章

  1. JavaScript七种非常经典的创建对象方式
  2. php orm url,PHP ORM使用之
  3. 2015年第四季度全球闪存行业营收同比减少5%
  4. 停车场管理系统c语言程序,c语言程序设计 停车场管理系统 停车场有1-20个车位号,设计一个停车场管理系统,实现停车场管理...
  5. 支持断线重连、永久watcher、递归操作 ZooKeeper 客户端
  6. 99%的人都不知道的鸡兔同笼解法!
  7. YiShaAdmin_项目的默认 XML 命名空间必须为 MSBuild XML 命名空间。如果项目是用 MSBuild 2003 格式---.Net_C#_若依.Net版Web框架使用及改造001
  8. et200sp模块接线手册_格力变频空调模块常见故障处理。
  9. 前端如何提示自己的技术水平
  10. 使用LIstView和自定义Adapter完成列表信息显示
  11. AcWing 847. 图中点的层次(BFS模板)
  12. MAMP Pro for Mac(PHP/MySQL开发环境)
  13. No package ‘libpeas-1.0‘ found/No package ‘libpeas-gtk-1.0‘
  14. 《CLR via C# 》第三版 英文原版电子书下载
  15. 浅谈GRADS气象绘图软件的使用
  16. Android11.0(R) MTK 预置可卸载app恢复出厂不恢复(仿RK方案)
  17. HighCharts生成柏拉图
  18. 直线上最多的点数java
  19. 解决VMware安装后网络连接中没有VMnet虚拟网络适配器
  20. 【Windows】联想win10系统截屏快捷键

热门文章

  1. 10bit灰阶测试图_2K高分10bit色彩 HKC专业制图液晶评测
  2. 苹果x微信语音十秒就断_苹果莫名其妙黑屏怎么办?
  3. mysql 5.6 ibdata1,5.6中删除ibdata1之后的报错
  4. 建立一个将文件扫描为PDF的网页
  5. 插值与多项式逼近 | 牛顿插值多项式
  6. Android初级开发(九)——网络交互—解析JSON格式数据
  7. NETCore WebAPI 如何设置一个统一的路由前缀
  8. BarTender 2016如何导出模板为pdf文件?
  9. Linux服务器格式化磁盘,分区,挂载
  10. 梯度下降优化算法综述,梯度下降法 神经网络