• 小项目 : 计算库函数中单词的个数第30天


     1 #ifndef _FILE_H_
     2 #define _FILE_H_
     3 
     4 #include <string.h>
     5 #include <strings.h>
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include <errno.h>
     9 #include <stdbool.h>
    10 #include "file.h"
    11 #include "show.h"
    12 
    13 #define MAX 4096
    14 #define MAX_WORD 520
    15 
    16 typedef struct word
    17 {
    18     char *str;
    19     int counter;
    20 }word_t;
    21 
    22 extern void write_queue(queue_t *, char *);
    23 extern void deal_one_file(queue_t *, char *);
    24 extern void deal_one_line(queue_t *, char *);
    25 
    26 void process_word(queue_t *, char*);
    27 word_t *mk_word(char *);
    28 
    29 int str_compare(const void*, const void *);
    30 #endif//_FILE_H_
    File头文件
     1 #ifndef _SHOW_H_
     2 #define _SHOW_H_
     3 
     4 #include "file.h"
     5 #include "queue.h"
     6 
     7 void show_result(queue_t *);
     8 void destroy_result(queue_t *);
     9 void *output_queue(queue_t *, int);
    10 
    11 void destroy_word(void *);
    12 int word_compare(const void *, const void *);
    13 
    14 
    15 #endif//_SHOW_H_
    show头文件
      1 //file.c
      2 #include "file.h"
      3 #include "queue.h"
      4 
      5 void write_queue(queue_t *s, char *file_list)
      6 {
      7     char buffer[MAX];
      8     FILE *fp_read_list;
      9 
     10     if(NULL == (fp_read_list = fopen(file_list, "r"))) {
     11         printf("Fail fopen%s<file>: %d : %s.
    ", file_list, __LINE__, strerror(errno));
     12         return ;
     13     }
     14 
     15     while(fgets(buffer, MAX, fp_read_list))
     16     {
     17         if('
    ' == buffer[strlen(buffer) - 1]) {
     18             buffer[strlen(buffer) - 1] = '';
     19         }
     20         printf("Parse file: %s 
    ", buffer);
     21         deal_one_file(s, buffer);
     22     }
     23 
     24     fclose(fp_read_list);
     25 }
     26 
     27 void deal_one_file(queue_t *s, char *file_one)
     28 {
     29     char buffer[MAX];
     30     FILE *fp_file_one;
     31 
     32     if(NULL == (fp_file_one = fopen(file_one, "r"))) {
     33         printf("Fail fopen%s<file>: %d : %s.
    ", file_one, __LINE__, strerror(errno));
     34         return ;
     35     }
     36 
     37     while(fgets(buffer, MAX, fp_file_one))
     38     {
     39         deal_one_line(s, buffer);
     40     }
     41 
     42     fclose(fp_file_one);
     43 }
     44 
     45 void deal_one_line(queue_t *s, char *line_one)
     46 {
     47     int i = 0;
     48     //char buffer[MAX_WORD];
     49     char *buffer;
     50 
     51     buffer = calloc(1, MAX_WORD);
     52     for(i = 0; *(line_one + i); i ++)
     53     {
     54         if(! ((*(line_one + i) >= 'a' && *(line_one + i) <= 'z') || 
     55                 (*(line_one + i) >= 'A' && *(line_one + i) <= 'Z'))) {
     56             *(line_one + i) = ' ';
     57         }
     58     }
     59 
     60     while((buffer = strsep(&line_one, " 
    ")) != NULL)
     61     {
     62         if(strlen(buffer) > 2) {
     63             process_word(s, buffer);    
     64         }
     65     }
     66     free(buffer);
     67     buffer = NULL;
     68 }
     69 
     70 void process_word(queue_t *s, char *str)
     71 {
     72     word_t *ret;
     73 
     74     ret = queue_search(s, str, str_compare);
     75 
     76     if(ret) {
     77         ret->counter ++;
     78     }
     79     else {
     80         queue_enqueue(s, mk_word(str));
     81     }
     82 }
     83 
     84 word_t *mk_word(char *str)
     85 {
     86     word_t *w;
     87 
     88     w = calloc(1, sizeof(word_t));
     89     w->str = calloc(100, sizeof(char *));
     90 
     91     strcpy(w->str, str);
     92     w->counter = 1;
     93 
     94     return w;
     95 }
     96 
     97 int str_compare(const void * first, const void *second)
     98 {
     99     const char *f = first;
    100     const word_t *s = second;
    101 
    102     return strcasecmp(f, s->str);
    103 }
    文件操作
     1 #ifndef _QUEUE_H_
     2 #define _QUEUE_H_
     3 
     4 #include "file.h"
     5 #include "show.h"
     6 
     7 typedef struct node 
     8 {
     9     void *data;
    10     struct node *next;
    11 }node_t;
    12 
    13 typedef struct 
    14 {
    15     struct node *head;
    16     struct node *tail;
    17 }queue_t;
    18 
    19 extern void queue_init(queue_t *);
    20 extern void queue_destroy(queue_t *, 
    21         void (*destroy)(void *));
    22 extern void queue_enqueue(queue_t *, void *);
    23 extern void    *queue_dequeue(queue_t *);
    24 extern bool queue_is_full(queue_t *);
    25 extern bool queue_is_empty(queue_t *);
    26 extern void *queue_search(queue_t *,
    27         const void *key,
    28         int (*compare)(const void *, const void *));
    29 
    30 extern void queue_sort(queue_t *,
    31         int (*compare)(const void *, const void *));
    32 extern node_t *find_min_queue(queue_t *, 
    33         int (*compare)(const void *, const void *));
    34 extern void delete_queue(queue_t *, node_t *);
    35 extern void _queue_enqueue(queue_t *, node_t *);
    36 
    37 extern void *queue_retriver(queue_t *, int idx);
    38 
    39 extern node_t *mk_node(void *);
    40 
    41 #endif//_QUEUE_H_
    队列头文件
      1 //quue.c
      2 #include "file.h"
      3 #include "queue.h"
      4 
      5 void queue_sort(queue_t *queue,
      6         int (*compare)(const void*, const void *))
      7 {
      8     node_t *min;
      9     queue_t s_new;
     10 
     11     queue_init(&s_new);
     12 
     13     while(! queue_is_empty(queue)) 
     14     {
     15         min = find_min_queue(queue, compare);
     16         delete_queue(queue, min);
     17         min->next = NULL;
     18         _queue_enqueue(&s_new, min);
     19     }
     20     queue->head = s_new.head;
     21     queue->tail = s_new.tail;
     22 }
     23 
     24 //-----------------------------find_min_queue-----------------------------
     25 node_t *find_min_queue(queue_t *queue, 
     26         int (*compare)(const void *, const void *))
     27 {
     28     node_t *min, *find;
     29 
     30     find = queue->head;
     31     min = queue->head;
     32 
     33     while(find && find->next)
     34     {
     35         if(compare(find->next->data, min->data) < 0) {
     36             min = find->next;
     37         }
     38         find = find->next;
     39     }
     40     return min;
     41 }
     42 
     43 void delete_queue(queue_t *queue, node_t *min)
     44 {
     45     node_t *delete;
     46 
     47     delete = queue->head;
     48 
     49     if(delete == min) {
     50         queue->head = delete->next;
     51 
     52         return ;
     53     }
     54 
     55     while(delete && delete->next)
     56     {
     57         if(delete->next == min) {
     58             if(delete->next == queue->tail) {
     59                 queue->tail = delete;
     60             }
     61             delete->next = delete->next->next;
     62         }
     63         delete = delete->next;
     64     }
     65 }
     66 
     67 void _queue_enqueue(queue_t *queue, node_t *data)
     68 {
     69     if(! queue->head) {
     70         queue->head = data;
     71         queue->tail = data;
     72     }
     73     else {
     74         queue->tail->next = data;
     75         queue->tail = data;
     76     }
     77 }
     78 //-----------------------------find_min_queue-----------------------------
     79 
     80 void *queue_search(queue_t *queue,
     81         const void *key,
     82         int (*compare)(const void*, const void *))
     83 {
     84     node_t *find;
     85 
     86     find = queue->head;
     87 #if 0
     88     while(find && find->next) 
     89     {
     90         for(; find; find = find->next)
     91         {
     92             if(0 == compare(key, find->data)) {
     93                 return find->data;
     94             }
     95         }
     96     }
     97 #endif
     98     while(find) {
     99         if(compare(key, find->data) == 0) {
    100             return find->data;
    101         }
    102         find = find->next;
    103     }
    104 
    105     return NULL;
    106 }
    107 
    108 bool queue_is_empty(queue_t *queue)
    109 {
    110     return queue->head == NULL;
    111 }
    112 
    113 bool queue_is_full(queue_t *queue)
    114 {
    115     return false;
    116 }
    117 
    118 void *queue_dequeue(queue_t *queue)
    119 {
    120     node_t *delete = NULL;
    121     void *data;
    122 
    123     delete = queue->head;
    124     queue->head = delete->next;
    125 
    126     data = delete->data;
    127     free(delete);
    128     
    129     return data;
    130 }
    131 
    132 void queue_enqueue(queue_t *queue, void *n)
    133 {
    134     node_t *en;
    135 
    136     en = mk_node(n);
    137     if(! queue->head) {
    138         queue->head = en;
    139         queue->tail = en;
    140     }
    141     else {
    142         queue->tail->next = en;
    143         queue->tail = en; 
    144     }
    145 }
    146 
    147 node_t *mk_node(void *n)
    148 {
    149     node_t *mk;
    150     
    151     mk = calloc(1, sizeof(node_t));
    152     mk->data = n;
    153     mk->next = NULL;
    154 
    155     return mk;
    156 }
    157 
    158 void queue_init(queue_t *queue)
    159 {
    160     queue->head = NULL;
    161     queue->tail = NULL;
    162 }
    163 
    164 void queue_destroy(queue_t *queue,
    165         void (*destroy)(void *))
    166 {
    167     if(destroy) {
    168         destroy(queue_dequeue(queue));
    169     }
    170     else {
    171         queue_dequeue(queue);
    172     }
    173 }
    队列操作
     1 //main.c
     2 #include "file.h"
     3 #include "show.h"
     4 #include "queue.h"
     5 
     6 int main(int argc, char *argv[])
     7 {
     8     int i = 0;
     9     queue_t s, *p;
    10     p = &s;
    11     p = NULL;
    12 
    13     
    14     queue_init(&s);
    15     for(i = 1; i < argc; i ++)
    16     {
    17         write_queue(&s, argv[i]);
    18     }
    19 
    20     show_result(&s);
    21 
    22     destroy_result(&s);
    23 
    24     return 0;
    25 }
    26 
    27 void destroy_result(queue_t *queue)
    28 {
    29     queue_destroy(queue, destroy_word);
    30 }
    31 
    32 void destroy_word(void *str)
    33 {
    34     word_t *w;
    35 
    36     free(w->str);
    37     free(w);
    38 }
    39 
    40 //-----------------------show------------------------
    41 
    42 void show_result(queue_t *queue)
    43 {
    44     word_t *w;
    45     int i = 0;
    46     
    47     printf("Being sort ...
    ");
    48     queue_sort(queue, word_compare);
    49 
    50     while((w = output_queue(queue, i)))
    51     {
    52         printf("%d : %s
    ", w->counter, w->str);
    53         i ++;
    54     }
    55 }
    56 
    57 int word_compare(const void *first, const void *second)
    58 {
    59     const word_t *f = first;
    60     const word_t *s = second;
    61 
    62     return f->counter - s->counter;
    63 }
    64 
    65 void *output_queue(queue_t *queue, int idex)
    66 {
    67     node_t *w;
    68     int i = 0;
    69 
    70     w = queue->head;
    71 #if 1
    72     for( ; i < idex; i ++)
    73     {
    74         w = w->next;
    75     }
    76     if(w) {
    77         return w->data;    
    78     }
    79 #else
    80     while(w && i <idex)
    81     {
    82         i ++;
    83         w = w->next;
    84     }
    85 
    86     if(w) {
    87         return w->data;
    88     }
    89 #endif
    90     return NULL;
    91 }
    92 
    93 //-----------------------show------------------------
    main函数

      1.功能 : 计算文件单词出现的次数,并按照从小到大排序, 

      2. 需求 : 需要自己提供库文件,以及需要读取的文件列表文件。

  • 相关阅读:
    leetcode Remove Nth Node From End of List
    leetcode Plus One
    leetcode climbing stairs
    leetcode Merge Two Sorted Lists
    leetcode Maximum Subarray
    leetcode Binary Tree Level Order Traversal I II
    leetcode Pascal's Triangle II
    leetcode pascal's triangle
    leetcode valid parentheses
    leetcode Path Sum
  • 原文地址:https://www.cnblogs.com/cxw825873709/p/3297254.html
Copyright © 2020-2023  润新知