Не могу доделать код Шеннона-Фано - C#
Формулировка задачи:
Суть метода описана здесь: http://informkod.narod.ru/4_4item.htm
Я ввожу символы с клавиатуры. Имеются два массива, в одном хранятся эти символы, в другом частота с которой они встречаются. Я в порядке убывания отсортировала их, теперь надо делить на группы с примерно равной суммой элементов, не могу понять как это реализовать.
Мой код:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication12 { class Program { static void Main(string[] args) { string text = null; text = (Console.ReadLine()).ToString(); string AlreadyDone = ""; string[,] Result = new string[text.Length, 2]; float[] kol = new float[text.Length]; float[] ver = new float[text.Length];//вероятность int i = 0, l = 0; foreach (char ch in text) { i = 0; foreach (char c in text) { if (ch == c) i++; } if (AlreadyDone.Contains(ch) != true) { // Console.WriteLine(ch + " повторяется " + i.ToString() + " раз "); Result[l, 0] = ch.ToString();//символ // Result[l, 1] = i.ToString(); //кол-во повторов kol[l] = i; AlreadyDone += ch; l++; } } float dlina = text.Length; for (int s = 0; s < AlreadyDone.Length; s++) { ver[s] = kol[s] / text.Length; Console.WriteLine("{0} повторяется {1} раз", Result[s, 0], kol[s]); } // Console.WriteLine("Кол-во букв " + text.Length); float sum = ver.Sum(); /* Array.Sort(ver); Array.Reverse(ver);*/ for (i = 0; i < Result.Length; i++) { for (int j = 0; j < ver.Length - i - 1; j++) { if (ver[j] < ver[j + 1]) { string temp2; float temp1 = 0; temp1 = ver[j]; temp2 = Result[j, 0]; ver[j] = ver[j + 1]; Result[j, 0] = Result[j + 1, 0]; ver[j + 1] = temp1; Result[j + 1, 0] = temp2; } } } float [] gr1 = new float[20]; for (int s = 0; s < AlreadyDone.Length; s++) { /* while(gr1.Sum()<(sum/2)) { if (ver[s] >= (sum - ver[s])) { gr1[s] = ver[s]; } else { gr1[s] = ver[s]; gr1[s + 1] = ver[s + 1]; } }*/ Console.WriteLine(Result[s, 0] + ver[s]); // Console.WriteLine(gr1[s]); } Console.WriteLine(sum); Console.ReadKey(); } } }
Решение задачи: «Не могу доделать код Шеннона-Фано»
textual
Листинг программы
using System; using System.Collections.Generic; using System.Linq; using System.Text; class A { double[] P1 = { 0.166, 0.5, 0.166, 0.166}; char[] Alpha = { 'М', 'О', 'Л', 'К' }; string[] Res=new string[33]; double schet1=0; double schet2=0; public void Sort() { for (int i = 0; i < P1.Length; i++) { for (int j = 0; j < P1.Length - i - 1; j++) { if (P1[j] < P1[j + 1]) { char temp2; double temp1 = 0; temp1 = P1[j]; temp2 = Alpha[j]; P1[j] = P1[j + 1]; Alpha[j] = Alpha[j + 1]; P1[j + 1] = temp1; Alpha[j + 1] = temp2; } } } } int m; public int Delenie_Posledovatelnosty(int L, int R) { schet1= 0; for(int i=L;i<=R-1;i++) { schet1=schet1+P1[i]; } schet2=P1[R]; m= R; while (schet1>=schet2 ) { m=m-1; schet1=schet1 - P1[m]; schet2=schet2+ P1[m]; } return m; } int g = 0; public void Fano(int L, int R) { int n; if (L < R) { n = Delenie_Posledovatelnosty(L, R); Console.WriteLine(n); for (int i = L; i <= R; i++) { if (i <= n) { Res[i] += Convert.ToByte(0); } else { Res[i] += Convert.ToByte(1); } } Fano1(L, n); Fano(n + 1, R); } } public void Fano1(int L, int R) { int n; if (L < R) { n = Delenie_Posledovatelnosty(L, R); Console.WriteLine(n); for (int i = L; i <= R; i++) { if (i <= n) { Res[i] += Convert.ToByte(0); } else { Res[i] += Convert.ToByte(1); } } Fano(L, n); Fano1(n + 1, R); } } public static void Main() { A ob=new A(); ob.Sort(); ob.Fano(0,32); for (int i = 0; i < 33; i++) { Console.WriteLine(ob.Alpha[i] + " " + ob.Res[i]); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д