计算单词出现的频率C语言实现的
2017-01-15 23:18
543 查看
这个就不多说,纯干货:
先看看这个文件的结构吧
具体的代码如下:
add_word.c 加入单词
[cpp] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "word_manage_p.h"
static void shift_array(int index)
{
int src;
for (src = num_of_word -1;src >= index; src--)
{
word_array[src+1] = word_array[src];
}
num_of_word++;
}
static char *my_strdup(char *src)
{
char *dest;
dest = malloc(sizeof(char) * (strlen(src)+1));
strcpy(dest,src);
return dest;
}
void add_word(char *word)
{
int i;
int result;
for(i =0; i< num_of_word; i++)
{
result = strcmp(word_array[i].name,word);
if(result >= 0)
break;
}
if(num_of_word != 0 && result ==0 )
{
word_array[i].count++;
}
else
{
shift_array(i);
word_array[i].name = my_strdup(word);
word_array[i].count = 1;
}
}
dump_word.c 输出结果
[cpp] view
plain copy
print?
include <stdio.h>
#include "word_manage_p.h"
void dump_word(FILE *fp)
{
int i;
for (i = 0; i < num_of_word; i++)
{
fprintf(fp,"%-20s%d\n",word_array[i],word_array[i].count);
}
}
finalize.c
[cpp] view
plain copy
print?
#include <stdlib.h>
#include "word_manage_p.h"
Word word_array[WORD_NUM_MAX];
int num_of_word;
void word_initialize(void)
{
num_of_word = 0;
}
initialize.c
[cpp] view
plain copy
print?
#include <stdlib.h>
#include "word_manage_p.h"
void word_finalize(void)
{
int i;
for(i = 0; i < num_of_word;i++)
{
free(word_array[i].name);
}
num_of_word = 0;
}
main.c
[cpp] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
#include "get_word.h"
#include "word_manage.h"
#define WORD_LEN_MAX (1024)
int main(int argc, char **argv)
{
char buf[WORD_LEN_MAX];
FILE *fp;
if(argc == 1)
{
fp = stdin;
}
else
{
fp = fopen(argv[1],"r");
if( fp == NULL)
{
fprintf(stderr," %s %s can not open.\n",argv[0],argv[1]);
exit(1);
}
}
word_initialize();
while(get_word(buf,WORD_LEN_MAX,fp)!= EOF)
{
add_word(buf);
}
dump_word(stdout);
word_finalize();
return 0;
}
头文件如下:
word_manage.h
[cpp] view
plain copy
print?
#ifndef WORD_MANAGE_H_INCLUDED
#define WORD_MANAGE_H_INCLUDED
#include <stdio.h>
void word_initialize(void);
void add_word(char *word);
void dump_word(FILE *fp);
void word_finalize(void);
#endif
word_manage_p.h
[cpp] view
plain copy
print?
ifndef WORD_MANAGE_P_H_INCLUDED
#define WORD_MANAGE_P_H_INCLUDED
#include "word_manage.h"
typedef struct {
char *name;
int count;
}Word;
#define WORD_NUM_MAX (10000)
extern Word word_array[];
extern int num_of_word;
#endif
get_word.h
[cpp] view
plain copy
print?
#ifndef GET_WORD_H_INCLUDE
#define GET_WORD_H_INCLUDE
int get_word(char *buf, int size, FILE *stream);
#endif
编译上面文件:
gcc *.c -o count
可以生成
count可执行文件
下面用一个文件进行测试
test的内容如下:
Then the programmers add the pragmas in source code to direct the compiler explicitly. The action of compiler can be controlled by programmers to generate ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
运行程序如下:
先看看这个文件的结构吧
具体的代码如下:
add_word.c 加入单词
[cpp] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "word_manage_p.h"
static void shift_array(int index)
{
int src;
for (src = num_of_word -1;src >= index; src--)
{
word_array[src+1] = word_array[src];
}
num_of_word++;
}
static char *my_strdup(char *src)
{
char *dest;
dest = malloc(sizeof(char) * (strlen(src)+1));
strcpy(dest,src);
return dest;
}
void add_word(char *word)
{
int i;
int result;
for(i =0; i< num_of_word; i++)
{
result = strcmp(word_array[i].name,word);
if(result >= 0)
break;
}
if(num_of_word != 0 && result ==0 )
{
word_array[i].count++;
}
else
{
shift_array(i);
word_array[i].name = my_strdup(word);
word_array[i].count = 1;
}
}
dump_word.c 输出结果
[cpp] view
plain copy
print?
include <stdio.h>
#include "word_manage_p.h"
void dump_word(FILE *fp)
{
int i;
for (i = 0; i < num_of_word; i++)
{
fprintf(fp,"%-20s%d\n",word_array[i],word_array[i].count);
}
}
finalize.c
[cpp] view
plain copy
print?
#include <stdlib.h>
#include "word_manage_p.h"
Word word_array[WORD_NUM_MAX];
int num_of_word;
void word_initialize(void)
{
num_of_word = 0;
}
initialize.c
[cpp] view
plain copy
print?
#include <stdlib.h>
#include "word_manage_p.h"
void word_finalize(void)
{
int i;
for(i = 0; i < num_of_word;i++)
{
free(word_array[i].name);
}
num_of_word = 0;
}
main.c
[cpp] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
#include "get_word.h"
#include "word_manage.h"
#define WORD_LEN_MAX (1024)
int main(int argc, char **argv)
{
char buf[WORD_LEN_MAX];
FILE *fp;
if(argc == 1)
{
fp = stdin;
}
else
{
fp = fopen(argv[1],"r");
if( fp == NULL)
{
fprintf(stderr," %s %s can not open.\n",argv[0],argv[1]);
exit(1);
}
}
word_initialize();
while(get_word(buf,WORD_LEN_MAX,fp)!= EOF)
{
add_word(buf);
}
dump_word(stdout);
word_finalize();
return 0;
}
头文件如下:
word_manage.h
[cpp] view
plain copy
print?
#ifndef WORD_MANAGE_H_INCLUDED
#define WORD_MANAGE_H_INCLUDED
#include <stdio.h>
void word_initialize(void);
void add_word(char *word);
void dump_word(FILE *fp);
void word_finalize(void);
#endif
word_manage_p.h
[cpp] view
plain copy
print?
ifndef WORD_MANAGE_P_H_INCLUDED
#define WORD_MANAGE_P_H_INCLUDED
#include "word_manage.h"
typedef struct {
char *name;
int count;
}Word;
#define WORD_NUM_MAX (10000)
extern Word word_array[];
extern int num_of_word;
#endif
get_word.h
[cpp] view
plain copy
print?
#ifndef GET_WORD_H_INCLUDE
#define GET_WORD_H_INCLUDE
int get_word(char *buf, int size, FILE *stream);
#endif
编译上面文件:
gcc *.c -o count
可以生成
count可执行文件
下面用一个文件进行测试
test的内容如下:
Then the programmers add the pragmas in source code to direct the compiler explicitly. The action of compiler can be controlled by programmers to generate ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
运行程序如下:
相关文章推荐
- 计算单词出现的频率C语言实现的
- Spark Streaming实现实时WordCount,DStream的使用,updateStateByKey(func)实现累计计算单词出现频率
- C语言通过二叉树实现单词出现频率的统计
- 用C语言实现了对英文文章中单词频率的统计,得到出现最多的前十个!
- hadoop案例实现之WordCount (计算单词出现的频数)
- 一篇文章中求出现频率最高的10个单词(C++实现tanglanting)
- c语言实现识别单词并输出出现次数最多的十个单词
- PHP:计算文件或数组中单词出现频率
- C语言实现输入字符串计算单词个数
- 用c语言实现 计算一个字符串中单词的个数
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。
- 计算单词出现的频率
- 统计文件中各单词出现的频率(二叉排序树实现)
- 计算出一段英文中出现频率最高的单词(第一次面试时没做出来,现在都记忆深刻)
- C语言 -- 计算一个数组中每个元素出现的频率
- AC自动机应用 多模式匹配 多个单词在文章中出现的次数-C语言实现
- 统计一段文字中出现频率最高的10个单词(c语言)
- 有关统计单词频率的算法c语言实现
- 有关统计单词频率的算法c语言实现
- 关于统计ascii码文本中英语单词的出现频率的c++实现