using System;
using System.Collections.Generic;
public class gistogramms
{
public static void main(string[] args)
{
Console.WriteLine("Введите текст:");
string inputText = Console.ReadLine();
if (string.IsNullOrEmpty(inputText))
{
Console.WriteLine("Вы не ввели текст.");
return;
}
// Подсчет количества каждого символа
Dictionary<char, int> charCounts = new Dictionary<char, int>();
for (int i = 0; i < inputText.Length; i++)
{
char currentChar = inputText[i];
if (charCounts.ContainsKey(currentChar))
{
charCounts[currentChar]++;
}
else
{
charCounts.Add(currentChar, 1);
}
}
int dictSize = charCounts.Count;
char[] sortedKeys = new char[dictSize];
int keyIndex = 0;
System.Collections.IDictionaryEnumerator enumerator = charCounts.GetEnumerator();
while (enumerator.MoveNext())
{
sortedKeys[keyIndex++] = (char)enumerator.Key;
}
// Сортируем
Array.Sort(sortedKeys);
// Определяем максимальную ширину для чисел (для выравнивания)
int maxCount = 0;
for (int i = 0; i < sortedKeys.Length; i++)
{
int currentCount = charCounts[sortedKeys[i]];
if (currentCount > maxCount)
{
maxCount = currentCount;
}
}
// Получаем длину максимального числа для выравнивания
int maxNumWidth = maxCount.ToString().Length;
Console.WriteLine("\nГистограмма символов:");
// Выводим гистограмму, используя отсортированные ключи
for (int i = 0; i < sortedKeys.Length; i++)
{
char character = sortedKeys[i];
int count = charCounts[character];
// Формируем строку для гистограммы
string histogramBar = new string('*', count);
// Выводим символ, его количество и гистограмму
Console.WriteLine($"'{character}' : {count.ToString().PadLeft(maxNumWidth)} | {histogramBar}");
}
}
}