查找出现频率最多的字
2014-03-03 12:34
295 查看
#include<iostream.h> #include<string.h> #include<fstream.h> struct word{ char name[30]; int num; struct word *next; }; void readfile(struct word*&head) { struct word*fp; if((fp=fopen("in.txt","r"))==NULL) { cout<<"无法打开此文件!<<endl; break; } char ch; char temp[30]; struct word *p; while(!feof(fp)) { int i=0; ch=fgetc(fp); temp[0]=' '; while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||temp[0]==' ') { if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') { temp[i]=ch; i++; } ch=fgetc(fp); if(feof(fp)) break; } temp[i]='\0'; p=head->next; while(p) { if(!_stricmp(temp,p->name)) { p->num++; break; } p=p->next; } if(!p&&temp[0]!='\0') { p=new word; strcpy(p->name,temp); p->num=1; p->next=head->next; head->next=p; } } } void puts(struct word*&head) { struct word *q; q=q->name; } void sort(struct word*&head) { struct word *ptr; int a[10],i; for(i=0;i<10;i++) a[i]=0; cout<<"文章中出现频率最高的十个单词如下:<<endl; for(i=0;i<10;i++) { ptr=head; while(ptr!=NULL) { if(ptr->num>a[i]) a[i]=ptr->num; else ptr=ptr->next; } ptr=head; while(ptr!=NULL) { if(a[i]==ptr->num) { ptr->num=0; cout<<"出现频率:cout<<a[i]<<endl; puts(ptr->name); break; } else ptr=ptr->next; } } } void main() { struct word *head; head=new word; head->next=NULL; readfile(head); sort(head); }
相关文章推荐
- Top K 算法(从n多个数据中查找出现频率最多的前m个的问题)
- 提供任意字符串,请构造一个函数能够查找字符串中出现最多的次数和个数
- c#面试题 查找整型数组中重复出现次数最多的数字,次数相同数字按从小到大排序
- 查找字符串中出现最多的字符和个数?
- 分组查找出现最多的元素hive自定义UDAF
- 大数据处理之哈希表(二)--出现频率最多的top xxx 位
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- 【编程题】1000个(0-9)数字的数组中查找出现次数最多的3个数字,输出这3个数字和出现的次数。
- PHP统计数值数组中出现频率最多的10个数字的方法
- Python查找一个文章里出现次数最多的10的单词
- 查找字符串中出现最多的字符和它出现的次数和查找某个字符在字符串中出现的次数
- POJ 3368 Frequent values(RMQ 区间出现频率最多数字次数)
- 在一亿个数中寻找出现频率最多的4个
- 查找文本中n个出现频率最高的单词
- web前端面试题(查找字符串中出现最多的字符)
- 查找文本中n个出现频率最高的单词
- 软件工程作业------分析文本文档,统计出现频率最多的十个单词
- matlab之求向量中出现最多的元素和频率
- 查找出字符串中出现最多的字符及其出现的次数
- 【二叉树】二分查找树,中序遍历,统计出现次数最多的节点【Add to List 501. Find Mode in Binary Search Tree】