Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Lab1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Coding();
  16.             Console.ReadLine();
  17.         }
  18.  
  19.         private static void Coding()
  20.         {
  21.             var setProbability = new Dictionary<string, double>
  22.             {
  23.                 { "p(a1)", 0.213 },
  24.                 { "p(a2)", 0.196 },
  25.                 { "p(a3)", 0.147 },
  26.                 { "p(a4)", 0.142 },
  27.                 { "p(a5)", 0.095 },
  28.                 { "p(a6)", 0.065 },
  29.                 { "p(a7)", 0.031 },
  30.                 { "p(a8)", 0.111 }
  31.             };
  32.  
  33.             for (var i = 0; i < setProbability.Count; i++)
  34.                 Console.WriteLine($"p(a{i + 1}) = {setProbability[$"p(a{i + 1})"]}");
  35.  
  36.  
  37.  
  38.             var initialSet = setProbability;
  39.  
  40.  
  41.             setProbability = Sort(setProbability);
  42.             var codeShennonPhano = new Dictionary<string, string>();
  43.             ShannoPhano(setProbability, codeShennonPhano);
  44.  
  45.  
  46.             var L1 = 0.0;
  47.             var Kcc1 = 0.0;
  48.             var Koe1 = 0.0;
  49.             var H1 = 0.0;
  50.             var Hmax1 = 0.0;
  51.             Console.WriteLine();
  52.             Console.WriteLine("Коды элементов по методу Шеннона-Фано: ");
  53.             foreach (var codeShPh in codeShennonPhano)
  54.             {
  55.                 Console.WriteLine(codeShPh.Key + " = " + codeShPh.Value);
  56.                 L1 += codeShPh.Value.Length * initialSet[codeShPh.Key];
  57.                 H1 += -1 * (initialSet[codeShPh.Key] * Math.Log(initialSet[codeShPh.Key], 2));
  58.             }
  59.  
  60.             Hmax1 = Math.Log(8, 2);
  61.             Kcc1 = Hmax1 / L1;
  62.             Koe1 = H1 / L1;
  63.             Console.WriteLine($"lср = {L1}");
  64.             Console.WriteLine($"H = {H1}");
  65.             Console.WriteLine($"Hmax = {Hmax1}");
  66.             Console.WriteLine($"Kcc = {Kcc1}");
  67.             Console.WriteLine($"Kоэ = {Koe1}");
  68.  
  69.  
  70.             var code = new Dictionary<string, Point>();
  71.             foreach (var point in setProbability)
  72.                 code[point.Key] = new Point();
  73.  
  74.             Haffman(setProbability, code);
  75.  
  76.             var L2 = 0.0;
  77.             var H2 = 0.0;
  78.             var Hmax2 = 0.0;
  79.             var Kcc2 = 0.0;
  80.             var Koe2 = 0.0;
  81.             var answer = string.Empty;
  82.             Console.WriteLine();
  83.             Console.WriteLine("Коды элементов по методу Хаффмана: ");
  84.             foreach (var codeHaggman in initialSet)
  85.             {
  86.                 answer = string.Empty;
  87.                 Console.Write(codeHaggman.Key + " = ");
  88.                 PrintHaffman(code[codeHaggman.Key], ref answer);
  89.                 L2 += answer.Length * initialSet[codeHaggman.Key];
  90.                 H2 += -1 * (initialSet[codeHaggman.Key] * Math.Log(initialSet[codeHaggman.Key], 2));
  91.                 Console.WriteLine();
  92.             }
  93.  
  94.             Hmax2 = Math.Log(8, 2);
  95.             Kcc2 = Hmax2 / L2;
  96.             Koe2 = H1 / L2;
  97.             Console.WriteLine($"lср = {L2}");
  98.             Console.WriteLine($"H = {H2}");
  99.             Console.WriteLine($"Hmax = {Hmax2}");
  100.             Console.WriteLine($"Kcc = {Kcc2}");
  101.             Console.WriteLine($"Kоэ = {Koe2}");
  102.             Console.ReadKey();
  103.         }
  104.  
  105.  
  106.         private static void PrintHaffman(Point point, ref string answer, bool print = true)
  107.         {
  108.             if (point.parent != null)
  109.                 PrintHaffman(point.parent, ref answer);
  110.  
  111.             answer += point.value;
  112.             if (print)
  113.                 Console.Write(point.value);
  114.         }
  115.  
  116.         private static void Haffman(Dictionary<string, double> set, Dictionary<string, Point> code)
  117.         {
  118.             set = Sort(set, false);
  119.             var count = 0;
  120.             var dir = new Dictionary<string, double>();
  121.             var sum = 0.0;
  122.             var keyBuf = string.Empty;
  123.  
  124.             foreach (var point in set)
  125.             {
  126.                 count++;
  127.                 if (count < 3)
  128.                 {
  129.                     sum += point.Value;
  130.                     if (count == 1)
  131.                     {
  132.                         code[point.Key].value += "0";
  133.                         keyBuf += point.Key;
  134.                     }
  135.  
  136.                     else
  137.                     {
  138.                         code[point.Key].value += "1";
  139.                         code[keyBuf].parent = new Point();
  140.                         code[point.Key].parent = code[keyBuf].parent;
  141.                         code[keyBuf + point.Key] = code[keyBuf].parent;
  142.                         dir[keyBuf + point.Key] = sum;
  143.                     }
  144.                 }
  145.  
  146.                 else
  147.                 {
  148.                     dir[point.Key] = point.Value;
  149.                 }
  150.             }
  151.  
  152.             if (set.Count != 2)
  153.                 Haffman(dir, code);
  154.         }
  155.  
  156.         private static Dictionary<string, double> Sort(Dictionary<string, double> setProbability, bool reverse = true)
  157.         {
  158.             var sortSetProbability = setProbability.OrderBy(value => value.Value).ToDictionary(
  159.                         row => row.Key,
  160.                         row => row.Value);
  161.  
  162.             if (reverse)
  163.                 return sortSetProbability.Reverse().ToDictionary(
  164.                  row => row.Key,
  165.                  row => row.Value);
  166.             else
  167.                 return sortSetProbability;
  168.         }
  169.  
  170.         private static void ShannoPhano(Dictionary<string, double> set, Dictionary<string, string> code)
  171.         {
  172.             var SumProbability = set.Sum(x => x.Value);
  173.             var group1 = new Dictionary<string, double>();
  174.             var fullnessGroup1 = 0.0;
  175.             var group2 = new Dictionary<string, double>();
  176.  
  177.             foreach (var point in set)
  178.             {
  179.                 var fullness = (fullnessGroup1 + point.Value) / SumProbability;
  180.  
  181.                 if (Math.Abs(fullnessGroup1 / SumProbability - 0.5) > Math.Abs(fullness - 0.5))
  182.                 {
  183.                     group1.Add(point.Key, point.Value);
  184.                     fullnessGroup1 += point.Value;
  185.                     if (code.ContainsKey(point.Key))
  186.                         code[point.Key] += "0";
  187.  
  188.                     else code[point.Key] = "0";
  189.                 }
  190.                 else
  191.                 {
  192.                     group2.Add(point.Key, point.Value);
  193.                     fullnessGroup1 += 1;
  194.                     if (code.ContainsKey(point.Key))
  195.                         code[point.Key] += "1";
  196.  
  197.                     else code[point.Key] = "1";
  198.                 }
  199.             }
  200.             if (group1.Count > 1)
  201.                 ShannoPhano(group1, code);
  202.  
  203.             if (group2.Count > 1)
  204.                 ShannoPhano(group2, code);
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement