Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct {
  6. char c;
  7. int db;
  8. int apa;
  9. }Item1;
  10. typedef struct {
  11. int db;
  12. char* kod;
  13. }Item2;
  14. int apatlanminindex(Item1* h, int n);
  15.  
  16. int main()
  17. {
  18. Item2 stat[256] = { 0 };
  19. int n = 0;
  20. FILE* be;
  21. be = fopen("bemenet.txt", "rt");
  22. if (!be) { return 0; }
  23. char x;
  24. while (fscanf(be,"%c",&x)!=EOF)
  25. {
  26. ++stat[x].db;
  27. if (stat[x].db == 1) { ++n; }
  28. }
  29. Item1* h;
  30. h = (Item1*)calloc(2 * n - 1, sizeof(Item1));
  31. int j = 0;
  32. for (int i = 0; i < 256; ++i)
  33. {
  34. if (stat[i].db)
  35. {
  36. h[j].db = stat[i].db;
  37. h[j].c = i;
  38. ++j;
  39. }
  40. }
  41. int m = n - 1;//ennyi apa lesz
  42. int y;
  43. for (int i = 0; i < m; ++i)
  44. {
  45. y = apatlanminindex(h, n);
  46. h[y].apa = (-1) * n;
  47. h[n].db += h[y].db;
  48. y = apatlanminindex(h, n);
  49. h[y].apa = n;
  50. h[n].db += h[y].db;
  51. ++n;
  52. }
  53. for (int i = 0; i < n; ++i)
  54. {
  55. printf("%i ", h[i].db);
  56. }
  57. printf("\n");
  58. n -= m;
  59. int kod;
  60. for (int i = 0; i < n; ++i)
  61. {
  62. generalkod(h, stat, h[i].c, i);
  63. }
  64. }
  65.  
  66. int apatlanminindex(Item1* h, int n)
  67. {
  68. int min = INT_MAX;
  69. int poz=0;
  70. for (int i = 0; i < n; ++i)
  71. {
  72. if (h[i].apa) { continue; }
  73. else
  74. {
  75. if (min > h[i].db) { min = h[i].db; poz = i; }
  76. }
  77. }
  78. return poz;
  79. }
  80. void generalkod(Item1* h, Item2* stat, char betu, int i)
  81. {
  82. int hosz;
  83. if (h[i].apa < 0)
  84. {
  85. generalkod(h,stat,betu, -h[i].apa);
  86. hosz = strlen(stat[betu].kod);
  87. stat[betu].kod = realloc(stat[betu].kod, (hosz + 1) * sizeof(char));
  88. stat[betu].kod[hosz] = "0";
  89. }
  90. else
  91. {
  92. if (h[i].apa > 0) { generalkod(h, stat, betu, h[i].apa); }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement