要求:
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
- //queue.h
- #ifndef _QUEUE_H_
- #define _QUEUE_H_
- typedef struct node
- {
- void *data;
- struct node *next;
- }node_t;
- typedef struct queue
- {
- node_t *head;
- node_t *tail;
- }que_t;
- void init_que(que_t *que);
- void in_queue(que_t *que, void *p);
- node_t *out_queue(que_t *que);
- bool is_empty(que_t *que);
- #endif //_QUEUE_H_
- //queue.c
- #include <stdio.h>
- #include <stdbool.h>
- #include "queue.h"
- void init_que(que_t *que)
- {
- que->head = NULL;
- que->tail = NULL;
- }
- void in_queue(que_t *que, void *p)
- {
- node_t *cur = p;
- if(que->head == NULL)
- {
- que->head = cur;
- que->tail = que->head;
- }
- else
- {
- que->tail->next = cur;
- que->tail = cur;
- }
- }
- node_t *out_queue(que_t *que)
- {
- node_t *save;
- save = que->head;
- que->head = que->head->next;
-
- return save;
- }
- bool is_empty(que_t *que)
- {
- return que->head == NULL;
- }
- //main.c
- /*
- 文件file_list.txt:为由每行均为文件名的文件
- 从file_list.txt中:读取每行的文件名,并统计这些文件中的每个单词的总的出现次数
- 并把结果写入到一个文件中
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <assert.h>
- #include <ctype.h>
- #include "queue.h"
- typedef struct node_word
- {
- int counter;
- char *word;
- }node_word_t;
- node_t *make_file_node(char *p)
- {
- node_t *cur;
- cur = malloc(sizeof(node_t));
- assert(cur);
- cur->data = malloc(128);
- assert(cur->data);
- strcpy((char *)cur->data, p);
- cur->next = NULL;
-
- return cur;
- }
- node_word_t *make_word_node(char *w)
- {
- node_word_t *cur;
- cur = malloc(sizeof(node_word_t));
- assert(cur);
- cur->counter = 1;
- cur->word = malloc(strlen(w) + 1);
- strcpy(cur->word, w);
- return cur;
- }
- node_t *make_node_node(node_word_t *p)
- {
- node_t *cur;
- cur = malloc(sizeof(node_t));
- assert(cur);
- cur->data = malloc(128);
- assert(cur->data);
- cur->data = p;
- cur->next = NULL;
-
- return cur;
- }
- void get_file(que_t *que_file, char *file)
- {
- FILE *fp;
- char buffer[1024];
- node_t *cur;
- fp = fopen(file, "r");
- assert(fp);
- while((fgets(buffer, sizeof(buffer), fp)) != NULL)
- {
- buffer[strlen(buffer) - 1] = 0;
- cur= make_file_node(buffer);
- in_queue(que_file, cur);
- }
- fclose(fp);
- }
- bool compare(const void *f, const void *s)
- {
- return ! strcmp((char *)f, (char *)s);
- }
- void *queue_find_word(que_t *que, char *token, bool (*compare)(const void *, const void *) )
- {
- node_t *cur;
- node_word_t *cur_word;
- for(cur = que->head; cur != NULL; cur = cur->next)
- {
- cur_word = cur->data;
- if( compare(cur_word->word, token))
- {
- return cur;
- }
- }
- return NULL;
- }
- char *filter(char *str)
- {
- int i;
- for(i = 0; str[i]; i++)
- {
- if(! isalpha(str[i]))
- str[i] = ' ';
- }
- return str;
- }
- void parser(que_t *que, que_t *que_file)
- {
- node_t *cur;
- FILE *fp;
- char buffer[1024];
- char *p;
- for(cur = que_file->head; cur != NULL; cur = cur->next)
- {
- node_t *save_node;
- save_node = out_queue(que_file);
- p = (char *)(save_node->data);
- fp = fopen(p, "r");
- assert(fp);
- while((fgets(buffer, sizeof(buffer), fp)) != NULL)
- {
- p = filter(buffer);
- char *token, *savep;
- for(; ; p = NULL)
- {
- token = strtok_r(p, " ", &savep);
-
- if(token == NULL)
- break;
-
- node_t *cur;
- if((cur = queue_find_word(que, token, compare)) != NULL)
- {
- node_word_t *tmp_word_node = cur->data;
- tmp_word_node->counter ++;
- }
- else
- {
- node_t *tmp_node;
- tmp_node = make_node_node(make_word_node(token));
- in_queue(que, tmp_node);
- }
- }
- }
- fclose(fp);
- free(save_node);
- }
- }
- void write_result(que_t *que, char *result)
- {
- node_t *cur;
- node_word_t *cur_word_node;
-
- FILE *fp = fopen(result, "w");
- fprintf(fp, "%s", "");
- fclose(fp);
- fp = fopen(result, "a");
- while(! is_empty(que))
- {
- cur = out_queue(que);
- cur_word_node = cur->data;
- fprintf(fp, "%d : %s\n", cur_word_node->counter, cur_word_node->word);
- free(cur_word_node->word);
- free(cur_word_node);
- free(cur);
- }
- fclose(fp);
- }
- int main(int argc, const char *argv[])
- {
- que_t que_file, que;
- init_que(&que_file);
- init_que(&que);
- get_file(&que_file, "file_list.txt");
-
- parser(&que, &que_file);
- write_result(&que, "show_result.txt");
- return 0;
- }
阅读(1471) | 评论(0) | 转发(3) |