Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct wezel_t
  5. {
  6. struct wezel_t *left, *right;
  7. int freq;
  8. char c;
  9. } *wezel;
  10.  
  11. struct wezel_t pool[256] = {{0}};
  12. wezel kolejka[255], *q = kolejka - 1;
  13. int n_wezly=0, k_koniec=1;
  14. char *code[128] = {0}, buf[1024];
  15.  
  16. wezel new_node(int freq, char c, wezel a, wezel b)
  17. {
  18. wezel n = pool + n_wezly++;
  19. if (freq)
  20. n->c = c, n->freq = freq;
  21. else
  22. {
  23. n->left = a, n->right = b;
  24. n->freq = a->freq + b->freq;
  25. }
  26. return n;
  27. }
  28.  
  29. void k_dodaj(wezel n)
  30. {
  31. int j, i = k_koniec++;
  32. while ((j = i / 2))
  33. {
  34. if (q[j]->freq <= n->freq)
  35. break;
  36. q[i] = q[j], i = j;
  37. }
  38. q[i] = n;
  39. }
  40.  
  41. wezel k_usun()
  42. {
  43. int i, l;
  44. wezel n = q[i = 1];
  45.  
  46. if (k_koniec < 2)
  47. return 0;
  48. k_koniec--;
  49. while ((l = i * 2) < k_koniec)
  50. {
  51. if (l + 1 < k_koniec && q[l + 1]->freq < q[l]->freq)
  52. l++;
  53. q[i] = q[l], i = l;
  54. }
  55. q[i] = q[k_koniec];
  56. return n;
  57. }
  58.  
  59. void kodowanie(wezel n, char *s, int len)
  60. {
  61. static char *out = buf;
  62. if (n->c)
  63. {
  64. s[len] = 0;
  65. strcpy(out, s);
  66. code[n->c] = out;
  67. out += len + 1;
  68. return;
  69. }
  70.  
  71. s[len] = '0';
  72. kodowanie(n->left, s, len + 1);
  73. s[len] = '1';
  74. kodowanie(n->right, s, len + 1);
  75. }
  76.  
  77. void start(const char *s)
  78. {
  79. int i, freq[128] = {0};
  80. char c[16];
  81.  
  82. while (*s)
  83. freq[(int)*s++]++;
  84.  
  85. for (i = 0; i < 128; i++)
  86. if (freq[i])
  87. k_dodaj(new_node(freq[i], i, 0, 0));
  88.  
  89. while (k_koniec > 2)
  90. k_dodaj(new_node(0, 0, k_usun(), k_usun()));
  91.  
  92. kodowanie(q[1], c, 0);
  93. }
  94.  
  95. void kodowanie1(const char *s, char *out)
  96. {
  97. while (*s)
  98. {
  99. strcpy(out, code[*s]);
  100. out += strlen(code[*s++]);
  101. }
  102. }
  103.  
  104. void dekodowanie(const char *s, wezel t)
  105. {
  106. wezel n = t;
  107. while (*s)
  108. {
  109. if (*s++ == '0')
  110. n = n->left;
  111. else
  112. n = n->right;
  113.  
  114. if (n->c)
  115. putchar(n->c), n = t;
  116. }
  117.  
  118. putchar('\n');
  119. if (t != n)
  120. printf("zle dane\n");
  121. }
  122.  
  123. int main(void)
  124. {
  125. int a;
  126. puts("----- KOMPRESJA HUFFMANA -----");
  127.  
  128. char str[2048] = { 0 }, buf[1024];
  129. printf("Podaj tekst: ");
  130. scanf(" %[^\n]s", str);
  131.  
  132. puts("Kodowanie: 1");
  133. puts("Dekodowanie: 2");
  134. scanf("%d", &a);
  135.  
  136. start(str);
  137. for (int i = 0; i < 128; i++)
  138. if (code[i])
  139. printf("%c: %s\n", i, code[i]);
  140.  
  141. if(a==1)
  142. {
  143. kodowanie1(str, buf);
  144. printf("zakodowane: %s\n", buf);
  145. dekodowanie(buf, q[1]);
  146. }
  147.  
  148. if(a==2)
  149. {
  150. kodowanie1(str, buf);
  151. printf("odkodowane: ");
  152. dekodowanie(buf, q[1]);
  153. }
  154.  
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement