Guest User

Untitled

a guest
Nov 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4.  
  5. #define FTR "file.txt"
  6. #define FTW "output.txt"
  7. #define NODES 511
  8.  
  9. int nodesused = 256;
  10.  
  11. struct htree{
  12.     unsigned freq;
  13.     int left;
  14.     int right;
  15.     int parent;
  16. } array[NODES];
  17.  
  18. struct low{
  19.     short low1;
  20.     short low2;
  21. } lowsym;
  22.  
  23. void buildTree(int, int);
  24. int findLow();
  25. void encode(FILE *);
  26. void write_bits(FILE *, int, int);
  27.  
  28. int main(){
  29.     int i = 0;
  30.     char symbol[1];
  31.     int f1, f2, codeTable[511];
  32.     FILE * input = fopen(FTR, "rb");
  33.     FILE * output = fopen(FTW, "wb");
  34.  
  35.     memset(array, 0, sizeof(array));
  36.     memset(symbol, 0, sizeof(symbol));
  37.  
  38.     while(fread(symbol, 1, 1, input))
  39.         array[symbol[0]].freq++;
  40.     fclose(input);
  41.  
  42.     while(i < 256){
  43.         fwrite(&array[i].freq, 1, sizeof(array[i].freq), output);
  44.         ++i;
  45.     }
  46.  
  47.     while(findLow() >= 0){
  48.         buildTree(lowsym.low1, lowsym.low2);
  49.     }
  50.  
  51.     array[lowsym.low1].parent = -1;
  52.     encode(output);
  53.     fclose(output);
  54.  
  55.     getchar();
  56.     return 0;
  57. }
  58.  
  59. int findLow(){
  60.     int i, count = 0;
  61.     lowsym.low1 = -1;
  62.     lowsym.low2 = -1;
  63.  
  64.     for(i = 0; i < NODES; i++)
  65.         if(array[i].freq){
  66.             count++;
  67.             if(lowsym.low1 < 0 || array[i].freq < array[lowsym.low1].freq)
  68.                 lowsym.low1 = i;
  69.         }
  70.  
  71.     if(count > 1)
  72.         for(i = 0; i < NODES; i++)
  73.             if(array[i].freq && i != lowsym.low1)
  74.                 if(lowsym.low2 < 0 || array[i].freq < array[lowsym.low2].freq)
  75.                     lowsym.low2 = i;
  76.        
  77.     return lowsym.low2;
  78. }
  79.  
  80. void buildTree(int s1, int s2){
  81.     int node = nodesused++;
  82.    
  83.     array[node].freq = array[s1].freq + array[s2].freq;
  84.     array[node].left = s1;
  85.     array[node].right = s2;
  86.     array[s1].parent = node;
  87.     array[s2].parent = node;
  88.  
  89.     array[s1].freq = 0;
  90.     array[s2].freq = 0;
  91. }
  92.  
  93. void encode(FILE * output){
  94.     unsigned i;
  95.     char buff[512], symbol[1], bufp = 0;
  96.     FILE * input = fopen(FTR, "rb");
  97.  
  98.     memset(buff, 0, sizeof(buff));
  99.     memset(symbol, 0, sizeof(symbol));
  100.  
  101.  
  102.  
  103.     while(fread(symbol, 1, 1, input)){
  104.         i = symbol[0];
  105.         memset(buff, 0, sizeof(buff));
  106.  
  107.         if(array[array[i].parent].left == i)
  108.             buff [bufp++] = 0 ;
  109.         else if(array[array[i].parent].right == i)
  110.             buff [bufp++] = 1 ;
  111.  
  112.         while(array[i].parent >= 0){
  113.             i = array[i].parent;
  114.             if(array[array[i].parent].left == i)
  115.                 buff [bufp++] = 0 ;
  116.             else if(array[array[i].parent].right == i)
  117.                 buff [bufp++] = 1 ;
  118.         }
  119.        
  120.         while (bufp) {
  121.             write_bits (output,buff [--bufp],0) ;
  122.         }
  123.     }
  124.     write_bits (output,0,1) ;
  125. }
  126.  
  127. void write_bits(FILE * output, int bit, int flush){
  128.     static count_bits = 0;
  129.     static char buff = 0;
  130.  
  131.     if(bit)
  132.         buff |= 1 << count_bits;
  133.  
  134.     if(++count_bits >= 8){
  135.         fwrite(&buff, 1, sizeof(buff), output);
  136.         buff = 0;
  137.         count_bits = 0;
  138.     }
  139.  
  140.     if(flush && count_bits > 0){
  141.         fwrite(&buff, 1, sizeof(buff), output);
  142.         buff = 0;
  143.         count_bits = 0;
  144.         return;
  145.     }
  146. }
Add Comment
Please, Sign In to add comment