计算单词出现的频率C语言实现的

来源:互联网 发布:数据库系统概论电子书 编辑:程序博客网 时间:2024/05/17 04:35

这个就不多说,纯干货:

先看看这个文件的结构吧

具体的代码如下:

add_word.c 加入单词

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. #include "word_manage_p.h"  
  6.   
  7. static void shift_array(int index)  
  8. {  
  9.         int src;  
  10.         for (src = num_of_word -1;src >= index; src--)  
  11.         {  
  12.                 word_array[src+1] = word_array[src];  
  13.         }  
  14.         num_of_word++;  
  15. }  
  16.   
  17.   
  18. static char *my_strdup(char *src)  
  19. {  
  20.         char *dest;  
  21.         dest = malloc(sizeof(char) * (strlen(src)+1));  
  22.         strcpy(dest,src);  
  23.         return dest;  
  24. }  
  25.   
  26. void add_word(char *word)  
  27. {  
  28.         int i;  
  29.         int result;  
  30.   
  31.         for(i =0; i< num_of_word; i++)  
  32.         {  
  33.                 result = strcmp(word_array[i].name,word);  
  34.                 if(result >= 0)  
  35.                 break;  
  36.         }  
  37.         if(num_of_word != 0 && result ==0 )  
  38.         {  
  39.                 word_array[i].count++;  
  40.         }  
  41.         else  
  42.         {  
  43.                 shift_array(i);  
  44.                 word_array[i].name = my_strdup(word);  
  45.                 word_array[i].count = 1;  
  46.         }  
  47. }  
dump_word.c 输出结果

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. include <stdio.h>  
  2. #include "word_manage_p.h"  
  3.   
  4. void dump_word(FILE *fp)  
  5. {  
  6.         int i;  
  7.         for (i = 0; i < num_of_word; i++)  
  8.         {  
  9.                 fprintf(fp,"%-20s%d\n",word_array[i],word_array[i].count);  
  10.         }  
  11. }  
 finalize.c
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdlib.h>  
  2. #include "word_manage_p.h"  
  3.   
  4.   
  5. Word word_array[WORD_NUM_MAX];  
  6. int num_of_word;  
  7.   
  8. void word_initialize(void)  
  9. {  
  10.         num_of_word = 0;  
  11. }  
initialize.c

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdlib.h>  
  2. #include "word_manage_p.h"  
  3.   
  4. void word_finalize(void)  
  5. {  
  6.         int i;  
  7.         for(i = 0; i < num_of_word;i++)  
  8.         {  
  9.                 free(word_array[i].name);  
  10.         }  
  11.         num_of_word = 0;  
  12. }  
main.c

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "get_word.h"  
  4. #include "word_manage.h"  
  5.   
  6. #define WORD_LEN_MAX (1024)  
  7.   
  8. int main(int argc, char **argv)  
  9. {  
  10.         char buf[WORD_LEN_MAX];  
  11.         FILE *fp;  
  12.         if(argc == 1)  
  13.         {  
  14.                 fp = stdin;  
  15.         }  
  16.         else  
  17.         {  
  18.                 fp = fopen(argv[1],"r");  
  19.                 if( fp == NULL)  
  20.                 {  
  21.                         fprintf(stderr," %s %s can not open.\n",argv[0],argv[1]);  
  22.                         exit(1);  
  23.                 }  
  24.         }  
  25.         word_initialize();  
  26.         while(get_word(buf,WORD_LEN_MAX,fp)!= EOF)  
  27.         {  
  28.                 add_word(buf);  
  29.         }  
  30.   
  31.         dump_word(stdout);  
  32.         word_finalize();  
  33.         return 0;  
  34. }  
头文件如下:

word_manage.h

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef WORD_MANAGE_H_INCLUDED  
  2. #define WORD_MANAGE_H_INCLUDED  
  3.   
  4. #include <stdio.h>  
  5.   
  6. void word_initialize(void);  
  7. void add_word(char *word);  
  8. void dump_word(FILE *fp);  
  9. void word_finalize(void);  
  10.   
  11. #endif  
word_manage_p.h
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. ifndef WORD_MANAGE_P_H_INCLUDED  
  2. #define  WORD_MANAGE_P_H_INCLUDED  
  3. #include "word_manage.h"  
  4.   
  5. typedef struct {  
  6.         char *name;  
  7.         int count;  
  8. }Word;  
  9.   
  10. #define WORD_NUM_MAX (10000)  
  11.   
  12. extern Word word_array[];  
  13. extern int num_of_word;  
  14.   
  15. #endif  
get_word.h

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef GET_WORD_H_INCLUDE  
  2. #define GET_WORD_H_INCLUDE  
  3.   
  4. int get_word(char *buf, int size, FILE *stream);  
  5.   
  6. #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

运行程序如下

0 0
原创粉丝点击