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. pta 哈利·波特的考试
  2. for-each的使用
  3. Bugku 杂项 隐写2 Welcome_.jpg
  4. 2018 “神策杯”高校算法大师赛 6 强诞生,【招人】进行时……
  5. boost::mp11::mp_assign相关用法的测试程序
  6. 高清、免版权美图资源大全
  7. C/C++字节对齐问题
  8. 1.5编程基础之循环控制 38 计算多项式的导函数
  9. 手把手教你使用Pandas读取结构化数据
  10. iis6 去掉index.php,iis6如何隐藏index.php
  11. RHEL 6.5 中的KVM虚拟化新特性
  12. php array 数组函数,php array数组函数
  13. ActionBar返回键图标怎么搞小点?
  14. android自动化测试抖音,全自动化的抖音启动速度测试
  15. 索尼计算机bios正确设置,索尼bios设置图解教程
  16. 北京市小汽车摇号程序的反编译、算法及存在的问题浅析
  17. 物联网产品的发展简介(二)【产品篇02】
  18. String format格式化
  19. C语言标识符之——“~“
  20. 迅为STM32MP157开发板手册更新记录

热门文章

  1. matlab中legend字体大小设置(一)
  2. python引用库保留字_V2.2.3 python3的35个保留字之import与from
  3. USB总线-Linux内核USB设备驱动之UAC2驱动分析(十)
  4. (转)如何将数据上传到onenet服务器(https://blog.csdn.net/qq_29219435/article/details/78471448)
  5. EXCEL如何实现多级联动下拉菜单
  6. 雷军的宿命:从万籁俱寂到舞台中央的喧嚣
  7. ubuntu16.04安装后的一系列...
  8. 豆豆TXT阅读器1.0发布
  9. 西电学生邮箱收不到软件激活邮件的解决方法
  10. 魔兽发信息给其他服务器的人,魔兽世界怀旧服:公会规矩是给外人定的?网友:真是毛人毛语!...