Алгоритм Хаффмана.Индекс вышел за пределы массива - C#
Формулировка задачи:
Добрый вечер!
Возникла проблем,писал алгоритм Хаффмана по методичкам и при работе выдаёт исключение...
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Threading.Tasks;
- namespace Huffman
- {
- class hafman
- {
- string ReverseString(string s)
- {
- char[] arr = s.ToCharArray();
- Array.Reverse(arr);
- return new string(arr);
- }
- void Swap<T>(ref T x, ref T y)
- {
- T p = x;
- x = y;
- y = p;
- }
- void Sort<T>(ref double[] x, ref T[] y)
- {
- for (int i = 0; i < x.Length; ++i)
- for (int j = 0; j < x.Length - 1; ++j)
- if (x[j] < x[j + 1])
- {
- Swap(ref x[j], ref x[j + 1]);
- Swap(ref y[j], ref y[j + 1]);
- }
- }
- public hafman()
- {
- StreamWriter s = new StreamWriter(@"c:\1.txt");
- Random rand = new Random();
- Console.WriteLine("длина алфавита");
- int len = Convert.ToInt32(Console.ReadLine());
- int[] a, b;
- double[] c, d;
- string[] h, e1;
- int j, u, i;
- a = new int[len];
- d = new double[len];
- c = new double[len];
- h = new string[len];
- e1 = new string[len];
- for (i = 0; i < h.Length; ++i)
- h[i] = string.Empty;
- Console.WriteLine("количество символов в алфавите");
- len = Convert.ToInt32(Console.ReadLine());
- b = new int[len];
- for (i = 0; i < a.Length; ++i)
- {
- a[i] = i + 1;
- e1[i] = (i + 1).ToString();
- }
- Console.WriteLine("вероятности");
- var aa = Console.ReadLine().Split(new char[] { }).Select(x => Convert.ToDouble(x)).ToArray();
- d[0] = aa[0];
- for (i = 1; i < a.Length; ++i)
- d[i] = d[i - 1] + aa[i];-----Здесь ругается на исключение Индекс вышел за пределы массива
- for (i = 0; i < b.Length; ++i)
- {
- u = 0;
- j = rand.Next(0, Convert.ToInt32(d[d.Length - 1]));
- while (j > d[u]) { ++u; };
- b[i] = a[u];
- }
- s.WriteLine("входное сообщение");
- string hh = string.Empty;
- for (i = 0; i < b.Length; ++i)
- {
- hh += b[i] + " ";
- }
- s.WriteLine(hh);
- s.WriteLine("");
- s.WriteLine("Вероятности появления символа :");
- Array.Reverse(aa);
- for (i = 0; i < a.Length; ++i)
- {
- s.WriteLine((i + 1) + ": " + aa[i]);
- }
- Sort(ref c, ref e1);
- Array.Reverse(c);
- for (j = c.Length - 1; j > 0; --j)
- {
- c[j - 1] += c[j];
- foreach (var x in e1[j])
- h[(x - '0') - 1] += 0;
- foreach (var x in e1[j - 1])
- h[(x - '0') - 1] += 1;
- e1[j - 1] += e1[j];
- Sort(ref c, ref e1);
- Array.Reverse(c);
- }
- s.WriteLine(" ");
- s.WriteLine("Результат :");
- for (i = 0; i < a.Length; ++i)
- {
- h[i] = ReverseString(h[i]);
- s.WriteLine(" " + (i + 1) + " = " + h[i]);
- }
- for (i = 0; i < d.Length; ++i)
- d[i] = b.Count(x => x == i + 1) * h[i].Length;
- int[] c2 = new int[Convert.ToInt32(d.Sum())];
- for (j = 0, i = 0; j < b.Length; ++j)
- foreach (var x in h[b[j] - 1])
- c2[i++] = x - '0';
- s.WriteLine("");
- s.WriteLine("Закодированное сообщение");
- s.WriteLine(String.Join(" ", new List<int>(c2).ConvertAll(x => x.ToString()).ToArray()));
- b = new int[205];
- int i1, u2 = -1;
- string hh1 = string.Empty; ;
- string r = string.Empty;
- string k = String.Join("", new List<int>(c2).ConvertAll(x => x.ToString()).ToArray());
- for (j = 0; j < k.Length; ++j)
- for (r += k[j], i = h.Min(x => x.Length); i < h.Max(x => x.Length); ++i)
- if (r.Length < i)
- break;
- else
- for (i1 = 0; i1 < h.Length; ++i1)
- if (r == h[i1])
- {
- b[++u2] = ++i1;
- r = string.Empty;
- break;
- }
- for (j = 0; j < b.Length; ++j) if (b[j] != 0) hh1 += b[j] + " "; else break;
- s.WriteLine("декодированное сообщение:");
- s.WriteLine(hh1);
- s.Close();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- hafman h = new hafman();
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Алгоритм Хаффмана.Индекс вышел за пределы массива»
textual
Листинг программы
- for (j = c.Length - 1; j > 0; --j)
- {
- c[j - 1] += c[j];
- foreach (var x in e1[j])
- h[(x - '0') - 1] += 0;----Индекс за пределы массива.
- foreach (var x in e1[j - 1])
- h[(x - '0') - 1] += 1;
- e1[j - 1] += e1[j];
- Sort(ref c, ref e1);
- Array.Reverse(c);
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д