Advertisement
Guest User

Untitled

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