Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct tree {
  6. unsigned char letter;
  7. struct tree * left;
  8. struct tree * right;
  9. struct tree * up;
  10. char direct;
  11. };
  12.  
  13. struct tree * create_l (char letter) {
  14. struct tree * cur;
  15. cur = (struct tree *) malloc (sizeof(struct tree));
  16. cur->up = NULL;
  17. cur->left = NULL;
  18. cur->right = NULL;
  19. cur->letter = letter;
  20. cur->direct = '0';
  21. return cur;
  22. }
  23.  
  24. struct tree * add (int mas1, int mas2, struct tree * cur1, struct tree * cur2) {
  25. struct tree * par;
  26. par = (struct tree *) malloc (sizeof(struct tree));
  27. if (mas1 < mas2) {
  28. par->right = cur1;
  29. par->left = cur2;
  30. cur1->direct = '1';
  31. cur2->direct = '0';
  32. } else {
  33. par->right = cur2;
  34. par->left = cur1;
  35. cur1->direct = '0';
  36. cur2->direct = '1';
  37. }
  38. cur1->up = par;
  39. cur2->up = par;
  40. par->up = NULL;
  41. par->letter = 0;
  42. return par;
  43. }
  44.  
  45. int main (void) {
  46. int n = 256;
  47. FILE *in, * tabl, *codes, *decode, *in_dec;
  48. in = fopen ("in.txt", "r");
  49. tabl = fopen ("in.txt", "r");
  50. codes = fopen ("codes.txt", "w");
  51. in_dec = fopen ("codes.txt", "r");
  52. decode = fopen ("decode.txt", "w");
  53. if ((in == NULL) || (tabl == NULL) || (codes == NULL) || (decode == NULL) || (in_dec == NULL)) {
  54. printf ("Файл не найден\n");
  55. return 0;
  56. }
  57. int freq[n]; //создание и заполнение массива частот
  58. unsigned char freq_l[n];
  59. for (int i = 0; i < n; i++) {
  60. freq[i] = 0;
  61. freq_l[i] = 0;
  62. }
  63. unsigned char c;
  64. while (fscanf (tabl, "%c", &c) != EOF) {
  65. freq[(int)c]++;
  66. freq_l[(int)c] = c;
  67. } //массив частот создан и заполнен
  68. struct tree * list[n], * temp[n]; //создание дерева для декодирования + для выбора кодов
  69. struct tree * root;
  70. for (int i = 0; i < n; i++) {
  71. list[i] = NULL;
  72. temp[i] = NULL;
  73. }
  74. int k = 0, flag = 0;
  75. for (int i = 0; i < n; i++) {
  76. if (freq[i] != 0) {
  77. k++;
  78. }
  79. }
  80. if (k == 1) {
  81. for (int i = 0; i < n; i++) {
  82. if (freq[i] != 0) {
  83. list[i] = create_l (freq_l[i]);
  84. root = list[i];
  85. flag++;
  86. c = freq_l[i];
  87. }
  88. }
  89. }
  90. while (k > 1) {
  91. int min, m_min, numb1 = 257, numb2 = 257;
  92. for (int i = 0; i < n; i++) {
  93. if (freq[i] != 0) {
  94. if (numb1 >= freq[i]) {
  95. min = m_min;
  96. numb2 = numb1;
  97. m_min = i;
  98. numb1 = freq[i];
  99. } else {
  100. if (numb2 >= freq[i]) {
  101. min = i;
  102. numb2 = freq[i];
  103. }
  104. }
  105. }
  106. }
  107. if (freq_l[min] != 0) {
  108. list[min] = create_l (freq_l[min]);
  109. temp[min] = list[min];
  110. }
  111. if (freq_l[m_min] != 0) {
  112. list[m_min] = create_l (freq_l[m_min]);
  113. temp[m_min] = list[m_min];
  114. }
  115. freq_l[min] = 0;
  116. freq_l[m_min] = 0;
  117. list[min] = add (freq[min], freq[m_min], list[min], list[m_min]);
  118. list[m_min] = list[min];
  119. freq[min] += freq[m_min];
  120. freq[m_min] = 0;
  121. k--;
  122. if (k == 1) {
  123. root = list[min];
  124. }
  125. }
  126. char * sh[n];
  127. for (int i = 0; i < n; i++) {
  128. sh[i] = (char *) malloc (sizeof(char)*256);
  129. }
  130. if (flag == 1) {
  131. sh[(int)c][0] = '0';
  132. sh[(int)c][1] = '\0';
  133. } else {
  134. for (int i = 0; i < n; i++) {
  135. if (temp[i] != NULL) {
  136. int j = 0;
  137. c = temp[i]->letter;
  138. while (temp[i]->up != NULL) {
  139. sh[(int)c][j] = temp[i]->direct;
  140. temp[i] = temp[i]->up;
  141. j++;
  142. }
  143. sh[(int)c][j] = '\0';
  144. }
  145. }
  146. }
  147. while (fscanf(in, "%c", &c) != EOF) { //кодирование
  148. int j = 0;
  149. while (sh[(int)c][j] != '\0') {
  150. j++;
  151. }
  152. for (int i = j - 1; i > -1; i--) {
  153. fprintf (codes, "%c", sh[(int)c][i]);
  154. }
  155. //fprintf (codes, "-");
  156. }
  157. fclose (in);
  158. fclose (tabl);
  159. fclose (codes);
  160. struct tree * cur = root; //декодирование
  161. while (fscanf (in_dec, "%c", &c) != EOF) {
  162. if (cur->letter == 0) {
  163. if (c == '0') {
  164. cur = cur->left;
  165. } else {
  166. cur = cur->right;
  167. }
  168. } else {
  169. fprintf (decode, "%c", cur->letter);
  170. if (c == '0') {
  171. cur = root->left;
  172. } else {
  173. cur = root->right;
  174. }
  175. }
  176. }
  177. if (cur->letter != 0) {
  178. fprintf (decode, "%c", cur->letter);
  179. }
  180. fclose (in_dec);
  181. fclose (decode);
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement