英语单词词频统计
2017-06-20 17:35
295 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NUM 100000
struct//单词结构体
{
int num; //单词频数
char ch[20]; //每一个单词
}word[MAX_NUM];
void read_e(char *essay,int *total_ch,char *ch)//获取文章信息
{
FILE *fp;
int i;
fp=fopen("data.txt","r");
if(!fp)
{
printf("\n打开文件data.txt失败!");
exit(0);
}
//获取文章及总字符数
(*ch)=fgetc(fp); //ch为文章第一个字符,判断文章文件是否为空。
if(*ch!=EOF)
{
essay[0]=(*ch);
for(i=1;!feof(fp);i++)
essay[i]=fgetc(fp); //获取文章,将文章中的各个字符放到essay数组中
essay[i]='\0'; //在读取文章时,并没有将字符串的结束标志读进去,为了后面使用strtok等字符串的相关函数,加上’\0’。
(*total_ch)=strlen(essay);
}
fclose(fp);
}
void show_e(char *essay,int *total_ch,char *ch)
//显示文章
{
if((*ch)==EOF)
printf("\n文件为空,请选择文章续写!");
else
printf("%s",essay);
printf("\n");
}
void sort(int *kind) //按照字典排序
{
int n,j,t;
char tempt[MAX_NUM];
for(n=0;n<(*kind)-1;n++)
for(j=0;j<(*kind)-n-1;j++)
{
if(strcmp(word[j].ch,word[j+1].ch)==1)
{
strcpy(tempt,word[j].ch); //交换单词
strcpy(word[j].ch,word[j+1].ch);
strcpy(word[j+1].ch,tempt);
t=word[j].num; //交换单词频数
word[j].num=word[j+1].num;
word[j+1].num=t;
}
}
}
void dep_show(char *essay,char *b)
{
int j,i=0;//i记录有多少不同的单词,n记录相同单词出现的次数
char *tok;
bool judge=false;
int sum=0;
FILE *fp;
for(tok=strtok(essay,b);tok!=NULL;tok=strtok(NULL,b))
{
//利用strtok函数将文章中的单词分离开。
sum++;//单词总数
strcpy(word[i].ch,tok);
(word[i].num)=1;
/************************************************
思想:
统计单词频数主要是在
函数void dep_show(char *essay,char *b)里实现。
每分离一个单词将其复制到单词的结构体数组中
并和之前统计的单词比较,若不相同,继续分离单词,
将分离的单词复制到单词的结构体数组下一个元素中;
若相同,则该单词的频数加1,继续分离单词,
将分离的单词复制到单词的结构体数组上一次所得到
的元素中,即覆盖了重复的单词。
*************************************************/
for(j=0;j<i;j++)
if(strcmp(word[i].ch,word[j].ch)==0)
{
(word[j].num)++;
judge=true;
}
if(judge)
i--;
i++;
judge=false;
}
// i=i-1;
sort(&i);
printf("单词总数为:%d\n",sum);
fp=fopen("count.txt","w");
if(!fp)
{
printf("\n打开文件count.txt失败!");
exit(0);
}
fprintf(fp,"单词总数为:%d\n",sum); //将单词总数读到文件中
for(j=0;j<i;j++)
{
fprintf(fp,"%-16s\t%d\t\n",word[j].ch,word[j].num);
//将单词、单词频数读到文件中
printf("%-16s\t%d\t\n",word[j].ch,word[j].num);
//将单词、单词频数打印到屏幕上
}
fclose(fp);
}
void add(char *essay,int *total_ch,char *ch)
{
char essay_write[MAX_NUM];
FILE *fp;
char str=' ';
getchar();
fp=fopen("data.txt","a+");
if(!fp)
{
printf("\n打开文件data.txt失败!");
exit(0);
}
printf("请输入:");
gets(essay_write);
if((*ch)!=EOF)
fputc(str,fp);
fputs(essay_write,fp); //将新增的文章读到文件上
strcat(essay,essay_write);
read_e(essay,total_ch,ch);
printf("添加成功");
printf("\n");
fclose(fp);
}
void menu(int *select)
{
int item,total_ch=0;//total_ch记录文章中的总字符数
char b[]={',','.','?',':','-',' ',' '},essay[2*MAX_NUM],ch;
printf("\n");
printf("----------------------------------------------------------------------\n");
printf(" 英语文本词频统计
\n");
printf(" 1.文章续写 2.显示英文文本 \n");
printf(" 3.显示单词总数、全部单词及频数 4.退出 \n");
printf("-----------------------------------------------------------------------\n");
printf("请选择1~4:");
scanf("%d",&item);
printf("\n");
read_e(essay,&total_ch,&ch);
switch(item)
{
case 1:add(essay,&total_ch,&ch);break;
case 2:show_e(essay,&total_ch,&ch);break;
case 3:dep_show(essay,b);break;
case 4:select=0;break;
default:printf("请在1~4之间选择\n");break;
}
}
void main()
{ int select=1;
while(select)
{
menu(&select);
}
}
#include <string.h>
#include <stdlib.h>
#define MAX_NUM 100000
struct//单词结构体
{
int num; //单词频数
char ch[20]; //每一个单词
}word[MAX_NUM];
void read_e(char *essay,int *total_ch,char *ch)//获取文章信息
{
FILE *fp;
int i;
fp=fopen("data.txt","r");
if(!fp)
{
printf("\n打开文件data.txt失败!");
exit(0);
}
//获取文章及总字符数
(*ch)=fgetc(fp); //ch为文章第一个字符,判断文章文件是否为空。
if(*ch!=EOF)
{
essay[0]=(*ch);
for(i=1;!feof(fp);i++)
essay[i]=fgetc(fp); //获取文章,将文章中的各个字符放到essay数组中
essay[i]='\0'; //在读取文章时,并没有将字符串的结束标志读进去,为了后面使用strtok等字符串的相关函数,加上’\0’。
(*total_ch)=strlen(essay);
}
fclose(fp);
}
void show_e(char *essay,int *total_ch,char *ch)
//显示文章
{
if((*ch)==EOF)
printf("\n文件为空,请选择文章续写!");
else
printf("%s",essay);
printf("\n");
}
void sort(int *kind) //按照字典排序
{
int n,j,t;
char tempt[MAX_NUM];
for(n=0;n<(*kind)-1;n++)
for(j=0;j<(*kind)-n-1;j++)
{
if(strcmp(word[j].ch,word[j+1].ch)==1)
{
strcpy(tempt,word[j].ch); //交换单词
strcpy(word[j].ch,word[j+1].ch);
strcpy(word[j+1].ch,tempt);
t=word[j].num; //交换单词频数
word[j].num=word[j+1].num;
word[j+1].num=t;
}
}
}
void dep_show(char *essay,char *b)
{
int j,i=0;//i记录有多少不同的单词,n记录相同单词出现的次数
char *tok;
bool judge=false;
int sum=0;
FILE *fp;
for(tok=strtok(essay,b);tok!=NULL;tok=strtok(NULL,b))
{
//利用strtok函数将文章中的单词分离开。
sum++;//单词总数
strcpy(word[i].ch,tok);
(word[i].num)=1;
/************************************************
思想:
统计单词频数主要是在
函数void dep_show(char *essay,char *b)里实现。
每分离一个单词将其复制到单词的结构体数组中
并和之前统计的单词比较,若不相同,继续分离单词,
将分离的单词复制到单词的结构体数组下一个元素中;
若相同,则该单词的频数加1,继续分离单词,
将分离的单词复制到单词的结构体数组上一次所得到
的元素中,即覆盖了重复的单词。
*************************************************/
for(j=0;j<i;j++)
if(strcmp(word[i].ch,word[j].ch)==0)
{
(word[j].num)++;
judge=true;
}
if(judge)
i--;
i++;
judge=false;
}
// i=i-1;
sort(&i);
printf("单词总数为:%d\n",sum);
fp=fopen("count.txt","w");
if(!fp)
{
printf("\n打开文件count.txt失败!");
exit(0);
}
fprintf(fp,"单词总数为:%d\n",sum); //将单词总数读到文件中
for(j=0;j<i;j++)
{
fprintf(fp,"%-16s\t%d\t\n",word[j].ch,word[j].num);
//将单词、单词频数读到文件中
printf("%-16s\t%d\t\n",word[j].ch,word[j].num);
//将单词、单词频数打印到屏幕上
}
fclose(fp);
}
void add(char *essay,int *total_ch,char *ch)
{
char essay_write[MAX_NUM];
FILE *fp;
char str=' ';
getchar();
fp=fopen("data.txt","a+");
if(!fp)
{
printf("\n打开文件data.txt失败!");
exit(0);
}
printf("请输入:");
gets(essay_write);
if((*ch)!=EOF)
fputc(str,fp);
fputs(essay_write,fp); //将新增的文章读到文件上
strcat(essay,essay_write);
read_e(essay,total_ch,ch);
printf("添加成功");
printf("\n");
fclose(fp);
}
void menu(int *select)
{
int item,total_ch=0;//total_ch记录文章中的总字符数
char b[]={',','.','?',':','-',' ',' '},essay[2*MAX_NUM],ch;
printf("\n");
printf("----------------------------------------------------------------------\n");
printf(" 英语文本词频统计
\n");
printf(" 1.文章续写 2.显示英文文本 \n");
printf(" 3.显示单词总数、全部单词及频数 4.退出 \n");
printf("-----------------------------------------------------------------------\n");
printf("请选择1~4:");
scanf("%d",&item);
printf("\n");
read_e(essay,&total_ch,&ch);
switch(item)
{
case 1:add(essay,&total_ch,&ch);break;
case 2:show_e(essay,&total_ch,&ch);break;
case 3:dep_show(essay,b);break;
case 4:select=0;break;
default:printf("请在1~4之间选择\n");break;
}
}
void main()
{ int select=1;
while(select)
{
menu(&select);
}
}
相关文章推荐
- 个人和结对项目 - 英语单词词频统计
- python:统计历年英语四六级试卷单词词频
- 使用单词树进行词频统计算法
- Trie树统计词频和指定前缀的单词个数
- python读取文件里的单词,统计词频,输出到文件
- 使用jdk 1.7的新api来统计jdk中英语单词的分布情况
- 【第二周】Java实现英语文章词频统计(改进1)
- 利用多种方式来统计词频(单词个数)
- Java 进行词频统计,并按单词顺序顺序排序
- C#统计单词词频
- linux shell 下载维基百科特色条目并统计单词词频
- 统计一篇英语文章每个单词出现的频率
- 统计并输出英语短文中单词出现的次数
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 关于统计ascii码文本中英语单词的出现频率的c++实现
- 英文单词词频统计
- [置顶] 【spark 词频统计】spark单词进行计数升级版
- 统计单词个数及词频(C++实现)
- map与set的简单综合应用,统计单词词频