Строка: Составить частотный список символов текста. - C#
Формулировка задачи:
Составить частотный список символов текста.
Решение задачи: «Строка: Составить частотный список символов текста.»
textual
Листинг программы
public struct FrequencyRecord
{
private char _char;
private int _freq;
private float _freqPercent;
public char Char { get { return _char; } }
public int Frequency { get { return _freq; } }
public float FrequencyPercentage { get { return _freqPercent; } }
public static FrequencyRecord[] GetFrequencyDictionary(string text)
{
var result = new List<FrequencyRecord>();
foreach (var c in text)
{
var index = result.FindIndex(r => r.Char == c);
if (index == -1)
{
var record = new FrequencyRecord();
record._char = c;
record._freq = 1;
record._freqPercent = 1.0f / ((float)text.Length / 100);
result.Add(record);
}
else
{
var record = new FrequencyRecord();
record._char = c;
record._freq = result[index]._freq + 1;
record._freqPercent = result[index]._freq / ((float)text.Length / 100);
result[index] = record;
}
}
return result.ToArray();
}
}