Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. class Program
  6. {
  7. static void ShakerSort(List<KeyValuePair<double, char>> List)
  8. {
  9. int beg, end;
  10. int count = 0;
  11.  
  12. for (int i = 0; i < List.Count / 2; i++)
  13. {
  14. beg = 0;
  15. end = List.Count - 1;
  16.  
  17. do
  18. {
  19. count += 2;
  20. if (List[beg].Key < List[beg + 1].Key)
  21. Swap(List, beg, beg + 1);
  22. beg++;
  23.  
  24.  
  25. if (List[end - 1].Key < List[end].Key)
  26. Swap(List, end - 1, end);
  27. end--;
  28.  
  29. }
  30. while (beg <= end);
  31. }
  32. }
  33.  
  34. static void Swap(List<KeyValuePair<double, char>> List, int i, int j)
  35. {
  36. KeyValuePair<double, char> temp;
  37. temp = List[i];
  38. List[i] = List[j];
  39. List[j] = temp;
  40. }
  41.  
  42. static void WriteArray(List<KeyValuePair<double, char>> List)
  43. {
  44. foreach (KeyValuePair<double, char> a in List)
  45. Console.WriteLine("{0}: {1}", a.Value, a.Key);
  46. Console.WriteLine("\n\n\n");
  47. }
  48.  
  49. static int Dividing(List<KeyValuePair<double, char>> List, int L, int R)
  50. {
  51. double HalfProb = 0, PieceProb = 0;
  52. for (int j = L; j <= R; j++)
  53. {
  54. HalfProb += List[j].Key;
  55. }
  56.  
  57. HalfProb /= 2;
  58.  
  59. int i = 0;
  60. for (i = L; PieceProb < HalfProb && i <= R; i++)
  61. {
  62. PieceProb += List[i].Key;
  63. }
  64.  
  65. return i - 1;
  66. }
  67.  
  68. static void Fano(List<KeyValuePair<double, char>> Pairs, Dictionary<char, string> Codes, int L, int R)
  69. {
  70. try
  71. {
  72. if (R - L > 0)
  73. {
  74. int index = Dividing(Pairs, L, R);
  75.  
  76. for (int i = L; i <= index; i++)
  77. {
  78. if (Codes.ContainsKey(Pairs[i].Value))
  79. Codes[Pairs[i].Value] += "1";
  80. else
  81. Codes.Add(Pairs[i].Value, "1");
  82. }
  83.  
  84. for (int i = index + 1; i <= R; i++)
  85. {
  86. if (Codes.ContainsKey(Pairs[i].Value))
  87. Codes[Pairs[i].Value] += "0";
  88. else
  89. Codes.Add(Pairs[i].Value, "0");
  90. }
  91.  
  92. Fano(Pairs, Codes, L, index);
  93. Fano(Pairs, Codes, index + 1, R);
  94. }
  95. }
  96. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  97. }
  98.  
  99. static void Main(string[] args)
  100. {
  101. Console.WriteLine("Введите Сообщение:");
  102. string message = Console.ReadLine();
  103. Dictionary<char, int> CharsAndProbs = new Dictionary<char, int>(); //считаем сюда сколько раз попадается каждый символ
  104. List<KeyValuePair<double, char>> List = new List<KeyValuePair<double, char>>();//Массив с парами вероятность&символ
  105. Dictionary<char, string> Codes = new Dictionary<char, string>(); //Тут результат работы алгоритма
  106.  
  107. int MessageLength = 0;
  108. foreach (char a in message)
  109. {
  110. MessageLength++;
  111. if (CharsAndProbs.ContainsKey(a))
  112. CharsAndProbs[a]++;
  113. else
  114. CharsAndProbs.Add(a, 1);
  115. }
  116.  
  117. foreach (KeyValuePair<char, int> a in CharsAndProbs)
  118. {
  119. List.Add(new KeyValuePair<double, char>(a.Value, a.Key));
  120. }
  121.  
  122. WriteArray(List);
  123. ShakerSort(List);
  124.  
  125. WriteArray(List);
  126. Fano(List, Codes, 0, List.Count - 1);
  127.  
  128. foreach (KeyValuePair<char, string> a in Codes)
  129. Console.WriteLine("{0}: {1}", a.Key, a.Value);
  130.  
  131. string Code = "";
  132. foreach (char a in message)
  133. {
  134. Code += Codes[a];
  135. }
  136. Console.WriteLine("\nКод:\n" + Code);
  137.  
  138. string Decoded = "";
  139. for (int i = 0; i < Code.Length; i++)
  140. {
  141. foreach (KeyValuePair<char, string> a in Codes)
  142. {
  143. try
  144. {
  145. if (Code.Substring(i, a.Value.Length) == a.Value)
  146. {
  147. Decoded += a.Key;
  148. i += a.Value.Length - 1;
  149. break;
  150. }
  151. }
  152. catch { }
  153. }
  154. }
  155.  
  156. Console.WriteLine("\nДекодировка:\n" + Decoded);
  157. Console.ReadKey();
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement