Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Фраза
  8. {
  9. class Program
  10. {
  11. struct Pair // СТруктура буква - вероятность - код
  12. {
  13. public char letter;
  14. public double prob;
  15. public string code;
  16. public Pair (char letter, double prob, string code)
  17. {
  18. this.letter = letter;
  19. this.prob = prob;
  20. this.code = code;
  21. }
  22. }
  23. public class PairProbFirst : Comparer<Pair>
  24. {
  25.  
  26. }
  27. static void Main(string[] args)
  28. {
  29. Console.WriteLine("Введите фразу: ");
  30. string line = Console.ReadLine();
  31. line = line.ToLower();
  32. Dictionary<char, double> letters = new Dictionary<char, double>();
  33. int count = 0;
  34. for (int i = 0; i < line.Length; i++)//добавление всех встречающихся символов кроме пробелов в словарь
  35. {
  36. char temp = line[i];
  37. if (temp != ' ')
  38. {
  39. if (letters.ContainsKey(temp))
  40. {
  41. double value = letters[temp];
  42. value++;
  43. letters.Remove(temp);
  44. letters.Add(temp, value);
  45. count++;
  46. }
  47. else
  48. {
  49. letters.Add(temp, 1);
  50. count++;
  51. }
  52. }
  53. }
  54. List<char> changed = new List<char>();
  55. foreach (KeyValuePair<char, double> pair in letters)
  56. {
  57. if (changed.Contains(pair.Key)==false)
  58. {
  59. double prob = pair.Value / count;
  60. char letter = pair.Key;
  61. letters.Remove(letter);
  62. letters.Add(letter, prob);
  63. changed.Add(letter);
  64. }
  65. }
  66.  
  67. List<Pair> pairs = new List<Pair>();
  68. foreach (KeyValuePair<char, double> pair in letters)
  69. {
  70. pairs.Add(new Pair(pair.Key, pair.Value, "0"));
  71. }
  72.  
  73.  
  74. //foreach (var pair in letters.OrderBy(pair => pair.Value))
  75. //{
  76. // Console.WriteLine("{0} - {1}", pair.Key, pair.Value);
  77. //}
  78. //Создать лист структур и переопределить компоратор для нее
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement