Chinaunix首页 | 论坛 | 博客
  • 博客访问: 377915
  • 博文数量: 62
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:04
文章分类

全部博文(62)

文章存档

2014年(1)

2013年(61)

分类: C/C++

2013-12-19 14:57:37

要求:

1. 用链表队列实现
2. 文件file_list.txt:为由每行均为文件名的文件

如:file_list.txt:

test_1.txt
test_2.txt
test_3.txt

3. 从file_list.txt中:读取每行的文件名,并统计这些文件中的每个单词的总的出现次数
4. 并把结果写入到一个文件中:show_result.txt


点击(此处)折叠或打开

  1. //queue.h

  2. #ifndef _QUEUE_H_
  3. #define _QUEUE_H_

  4. typedef struct node
  5. {
  6.     void *data;
  7.     struct node *next;
  8. }node_t;

  9. typedef struct queue
  10. {
  11.    node_t *head;
  12.    node_t *tail;
  13. }que_t;

  14. void init_que(que_t *que);
  15. void in_queue(que_t *que, void *p);
  16. node_t *out_queue(que_t *que);
  17. bool is_empty(que_t *que);

  18. #endif //_QUEUE_H_

点击(此处)折叠或打开

  1. //queue.c

  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include "queue.h"

  5. void init_que(que_t *que)
  6. {
  7.     que->head = NULL;
  8.     que->tail = NULL;
  9. }


  10. void in_queue(que_t *que, void *p)
  11. {
  12.    node_t *cur = p;

  13.    if(que->head == NULL)
  14.    {
  15.        que->head = cur;
  16.        que->tail = que->head;
  17.    }
  18.    else
  19.    {
  20.         que->tail->next = cur;
  21.         que->tail = cur;
  22.    }
  23. }

  24. node_t *out_queue(que_t *que)
  25. {
  26.    node_t *save;

  27.    save = que->head;
  28.    que->head = que->head->next;
  29.    
  30.    return save;
  31. }

  32. bool is_empty(que_t *que)
  33. {
  34.    return que->head == NULL;
  35. }

点击(此处)折叠或打开

  1. //main.c

  2. /*
  3. 文件file_list.txt:为由每行均为文件名的文件
  4. 从file_list.txt中:读取每行的文件名,并统计这些文件中的每个单词的总的出现次数
  5. 并把结果写入到一个文件中
  6. */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. #include <assert.h>
  12. #include <ctype.h>
  13. #include "queue.h"

  14. typedef struct node_word
  15. {
  16.     int counter;
  17.     char *word;
  18. }node_word_t;

  19. node_t *make_file_node(char *p)
  20. {
  21.    node_t *cur;

  22.    cur = malloc(sizeof(node_t));
  23.    assert(cur);
  24.    cur->data = malloc(128);
  25.    assert(cur->data);

  26.    strcpy((char *)cur->data, p);
  27.    cur->next = NULL;
  28.     
  29.    return cur;
  30. }


  31. node_word_t *make_word_node(char *w)
  32. {
  33.    node_word_t *cur;

  34.    cur = malloc(sizeof(node_word_t));
  35.    assert(cur);

  36.    cur->counter = 1;
  37.    cur->word = malloc(strlen(w) + 1);
  38.    strcpy(cur->word, w);

  39.    return cur;
  40. }

  41. node_t *make_node_node(node_word_t *p)
  42. {
  43.    node_t *cur;

  44.    cur = malloc(sizeof(node_t));
  45.    assert(cur);
  46.    cur->data = malloc(128);
  47.    assert(cur->data);

  48.    cur->data = p;
  49.    cur->next = NULL;
  50.     
  51.    return cur;
  52. }

  53. void get_file(que_t *que_file, char *file)
  54. {
  55.    FILE *fp;
  56.    char buffer[1024];
  57.    node_t *cur;

  58.    fp = fopen(file, "r");
  59.    assert(fp);

  60.    while((fgets(buffer, sizeof(buffer), fp)) != NULL)
  61.    {
  62.        buffer[strlen(buffer) - 1] = 0;

  63.        cur= make_file_node(buffer);
  64.        in_queue(que_file, cur);
  65.    }

  66.    fclose(fp);
  67. }


  68. bool compare(const void *f, const void *s)
  69. {
  70.     return ! strcmp((char *)f, (char *)s);
  71. }

  72. void *queue_find_word(que_t *que, char *token, bool (*compare)(const void *, const void *) )
  73. {
  74.    node_t *cur;
  75.    node_word_t *cur_word;

  76.    for(cur = que->head; cur != NULL; cur = cur->next)
  77.    {
  78.        cur_word = cur->data;
  79.        if( compare(cur_word->word, token))
  80.        {
  81.             return cur;
  82.        }
  83.    }

  84.    return NULL;
  85. }


  86. char *filter(char *str)
  87. {
  88.     int i;

  89.     for(i = 0; str[i]; i++)
  90.     {
  91.         if(! isalpha(str[i]))
  92.             str[i] = ' ';
  93.     }

  94.     return str;
  95. }

  96. void parser(que_t *que, que_t *que_file)
  97. {
  98.     node_t *cur;
  99.     FILE *fp;
  100.     char buffer[1024];
  101.     char *p;

  102.     for(cur = que_file->head; cur != NULL; cur = cur->next)
  103.     {
  104.         node_t *save_node;

  105.         save_node = out_queue(que_file);
  106.         p = (char *)(save_node->data);

  107.         fp = fopen(p, "r");
  108.         assert(fp);

  109.         while((fgets(buffer, sizeof(buffer), fp)) != NULL)
  110.         {
  111.             p = filter(buffer);

  112.             char *token, *savep;
  113.             for(; ; p = NULL)
  114.             {
  115.                 token = strtok_r(p, " ", &savep);
  116.                 
  117.                 if(token == NULL)
  118.                     break;
  119.     
  120.                 node_t *cur;
  121.                 if((cur = queue_find_word(que, token, compare)) != NULL)
  122.                 {
  123.                     node_word_t *tmp_word_node = cur->data;
  124.                     tmp_word_node->counter ++;
  125.                 }
  126.                 else
  127.                 {
  128.                     node_t *tmp_node;

  129.                     tmp_node = make_node_node(make_word_node(token));
  130.                     in_queue(que, tmp_node);

  131.                 }
  132.             }
  133.         }

  134.         fclose(fp);
  135.         free(save_node);
  136.     }
  137. }



  138. void write_result(que_t *que, char *result)
  139. {
  140.     node_t *cur;
  141.     node_word_t *cur_word_node;
  142.     
  143.     FILE *fp = fopen(result, "w");
  144.     fprintf(fp, "%s", "");
  145.     fclose(fp);

  146.     fp = fopen(result, "a");
  147.     while(! is_empty(que))
  148.     {
  149.         cur = out_queue(que);
  150.         cur_word_node = cur->data;

  151.         fprintf(fp, "%d : %s\n", cur_word_node->counter, cur_word_node->word);
  152.         free(cur_word_node->word);
  153.         free(cur_word_node);
  154.         free(cur);
  155.     }
  156.     fclose(fp);
  157. }


  158. int main(int argc, const char *argv[])
  159. {
  160.     que_t que_file, que;

  161.     init_que(&que_file);
  162.     init_que(&que);

  163.     get_file(&que_file, "file_list.txt");
  164.     
  165.     parser(&que, &que_file);
  166.     write_result(&que, "show_result.txt");
  167.     return 0;
  168. }

阅读(2039) | 评论(0) | 转发(0) |
0

上一篇:变参函数 _0229

下一篇:动态库的生成 _0303

给主人留下些什么吧!~~