Facebook
From anonymous, 1 Month ago, written in C.
Embed
Download Paste or View Raw
Hits: 139
  1. #ifndef CMPR_H
  2. #define CMPR_H
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define BUFSIZE 1024
  8.  
  9. #define ASCII_CHARS 126
  10.  
  11. #define PERROR(e, r)
  12.     perror(e);
  13.     return r;
  14.  
  15. typedef struct char_freq {
  16.     char character;
  17.     int frequency;
  18. } Char_freq;
  19.  
  20. typedef struct freq_table {
  21.     Char_freq freqs[ASCII_CHARS];
  22.     size_t size;
  23. } Freq_table;
  24.  
  25. // parses the file name from the list of command line arguments
  26. // returns error
  27. int parse_file_name(int argc, char *argv[], FILE **fd);
  28.  
  29. // reads a file into a buffer
  30. // returns error
  31. int read_file(char buf[BUFSIZE], FILE *fd);
  32.  
  33. // reads the text and adds the frequecy of each character to a table
  34. // freq table: char **
  35. int determine_char_freqs(FILE *fd);
  36.  
  37. // get frequency per each buffer iteration
  38. void buf_freqs(char buf[], Freq_table *table);
  39.  
  40. #endif
  41. -------------------------------------------------------------
  42.  
  43.  
  44.  
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include "cmpr.h"
  48.  
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.     FILE *fd = NULL;  
  53.     int err = 0;
  54.  
  55.     if ((err = parse_file_name(argc, argv, &fd;)) == -1) {
  56.         PERROR("open file", EXIT_FAILURE);
  57.     }  
  58.  
  59.     determine_char_freqs(fd);
  60.    
  61.     return 0;
  62. }
  63.  
  64. int parse_file_name(int argc, char *argv[], FILE **fd)
  65. {
  66.     *fd = fopen(argv[argc-1], "r");
  67.     if (*fd == NULL) {
  68.         return -1;
  69.     }
  70.     return 0;
  71. }
  72.  
  73. int read_file(char buf[BUFSIZE], FILE *fd)
  74. {
  75.     size_t read;
  76.     if ((read = fread(buf, sizeof(char), BUFSIZE-1, fd)) == EOF || read == 0) {
  77.         return -1;
  78.     }
  79.     return 0;
  80. }
  81.  
  82. int determine_char_freqs(FILE *fd)
  83. {
  84.     char buf[BUFSIZE] = {0};
  85.     Freq_table ftable = {0};
  86.     ftable.size = ASCII_CHARS;
  87.  
  88.     for (size_t i = 32; i <= ftable.size; i++)
  89.         ftable.freqs[i].character = (char)i;
  90.  
  91.  
  92.     // count text and fill table
  93.     while(read_file(buf, fd) != -1) {
  94.         buf_freqs(buf, &ftable;);
  95.     }
  96.  
  97.     /**
  98.     for (size_t i = 32; i <= ftable.size; i++) {
  99.         if (isprint((unsigned char)ftable.freqs[i].character))
  100.             printf("%cn", ftable.freqs[i].character);
  101.     }
  102.     **/
  103.  
  104.     for (size_t i = 32; i <= ftable.size; i++) {
  105.         printf("%c : %dn", ftable.freqs[i].character, ftable.freqs[i].frequency);
  106.     }
  107.     return 0;
  108. }
  109.  
  110.  
  111. void buf_freqs(char buf[], Freq_table *table)
  112. {
  113.     while(*buf != '�') {
  114.         for(size_t c = 32; c <= table->size; c++) {
  115.             if (*buf == table->freqs[c].character) {
  116.                 table->freqs[c].frequency++;
  117.                 break;
  118.             }
  119.         }
  120.         buf++;
  121.     }
  122. }
  123.