Advertisement
StreetKatya

Fano-Shenon

May 22nd, 2023
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Фано_Шеннон
  9. {
  10.     internal class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             double[] prob;
  15.             char[] chars;
  16.             string stroka = "";
  17.             FanoShennon fs;
  18.             Dictionary<char, string> dict = new Dictionary<char, string>();
  19.             Dictionary<string, char> reversedict = new Dictionary<string, char>();
  20.             using(var sr = new StreamReader("input.txt"))
  21.             {
  22.                 string[] str = sr.ReadToEnd().Split('\n');
  23.                 int n = str.Length;
  24.                 prob = new double[str.Length-1]; chars = new char[str.Length-1];
  25.                 for (int i = 0; i < str.Length-1; i++)
  26.                 {
  27.                     string[] vs = str[i].Split(new char[] {'\r', ' '});
  28.                     prob[i] = double.Parse(vs[0]);
  29.                     chars[i] = char.Parse(vs[1]);
  30.                 }
  31.                 stroka = str[str.Length-1];
  32.                 Console.WriteLine(stroka);
  33.                 fs = new FanoShennon(prob, chars);
  34.                 fs.Sort();
  35.                 fs.Fano(0,n-2);
  36.                 for (int i = 0; i < n-1; i++)
  37.                 {
  38.                     dict.Add(fs.let[i], fs.Res[i]);
  39.                     reversedict.Add(fs.Res[i], fs.let[i]);
  40.                     Console.WriteLine(fs.let[i] + " " + fs.Res[i]);
  41.                 }
  42.  
  43.             }
  44.             string codeStr = "";
  45.             for (int i = 0; i < stroka.Length; i++)
  46.             {
  47.                 codeStr += dict[stroka[i]];
  48.             }
  49.             Console.WriteLine(codeStr);
  50.             double step = Math.Round((double)stroka.Length * 8 / codeStr.Length,4);
  51.             Console.WriteLine($"Степень сжатия = {step}");
  52.  
  53.             string tempstr = "";
  54.             string result = "";
  55.             for (int i = 0; i < codeStr.Length; i++)
  56.             {
  57.                 tempstr += codeStr[i];
  58.                 if(reversedict.ContainsKey(tempstr))
  59.                 {
  60.                     result += reversedict[tempstr];
  61.                     tempstr = "";
  62.                 }
  63.             }
  64.             Console.WriteLine(result);
  65.             Console.ReadKey();
  66.         }
  67.     }
  68.     class FanoShennon
  69.     {
  70.  
  71.         public FanoShennon(double[] p, char[] letters)
  72.         {
  73.             prob = p;
  74.             let = letters;
  75.             Res = new string[prob.Length];
  76.         }
  77.         double[] prob;
  78.         internal char[] let;
  79.         internal string[] Res;
  80.  
  81.         double schet1 = 0;
  82.         double schet2 = 0;
  83.  
  84.         public void Sort()
  85.         {
  86.             for (int i = 0; i < prob.Length; i++)
  87.             {
  88.                 for (int j = 0; j < prob.Length - i - 1; j++)
  89.                 {
  90.                     if (prob[j] < prob[j + 1])
  91.                     {
  92.                         char temp2;
  93.                         double temp1 = 0;
  94.  
  95.                         temp1 = prob[j];
  96.                         temp2 = let[j];
  97.                         prob[j] = prob[j + 1];
  98.                         let[j] = let[j + 1];
  99.                         prob[j + 1] = temp1;
  100.                         let[j + 1] = temp2;
  101.  
  102.                     }
  103.                 }
  104.             }
  105.  
  106.         }
  107.         int m;
  108.  
  109.         public int Delenie_Posledovatelnosty(int L, int R)
  110.         {
  111.  
  112.             schet1 = 0;
  113.             for (int i = L; i <= R - 1; i++)
  114.             {
  115.                 schet1 = schet1 + prob[i];
  116.             }
  117.  
  118.             schet2 = prob[R];
  119.             m = R;
  120.             while (schet1 >= schet2)
  121.             {
  122.                 m = m - 1;
  123.                 schet1 = schet1 - prob[m];
  124.                 schet2 = schet2 + prob[m];
  125.             }
  126.             return m;
  127.  
  128.         }
  129.  
  130.         public void Fano(int L, int R)
  131.         {
  132.             int n;
  133.  
  134.             if (L < R)
  135.             {
  136.  
  137.                 n = Delenie_Posledovatelnosty(L, R);
  138.                 //Console.WriteLine(n);
  139.  
  140.                 for (int i = L; i <= R; i++)
  141.                 {
  142.                     if (i <= n) Res[i] += Convert.ToByte(0);
  143.                     else Res[i] += Convert.ToByte(1);
  144.                 }
  145.  
  146.                 Fano(L, n);
  147.                 Fano(n + 1, R);
  148.  
  149.             }
  150.         }
  151.  
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement