Загрузка данных
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Console.WriteLine("Введите текст для анализа:");
string input = Console.ReadLine() ?? "";
if (string.IsNullOrWhiteSpace(input))
{
Console.WriteLine("Вы не ввели текст.");
return;
}
// Подсчёт символов
var charCount = new Dictionary<char, int>();
foreach (char c in input)
{
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
}
// Берём топ-8 символов для читаемости диаграммы
var topChars = charCount.OrderByDescending(kvp => kvp.Value).Take(8).ToList();
int othersCount = charCount.Sum(kvp => kvp.Value) - topChars.Sum(kvp => kvp.Value);
var data = new List<(string Label, int Value)>();
foreach (var kvp in topChars)
data.Add(($"'{kvp.Key}'", kvp.Value));
if (othersCount > 0)
data.Add(("Другие", othersCount));
// Рисуем текстовую круговую диаграмму
Console.WriteLine("\n=== Круговая диаграмма символов ===\n");
DrawTextPieChart(data);
}
static void DrawTextPieChart(List<(string Label, int Value)> data)
{
int total = data.Sum(d => d.Value);
int width = 40; // ширина диаграммы
int height = 12; // высота диаграммы
char[,] canvas = new char[height, width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
canvas[i, j] = ' ';
double centerX = width / 2.0;
double centerY = height / 2.0;
double radius = Math.Min(centerX, centerY) - 1;
double currentAngle = -Math.PI / 2; // начинаем с верхней точки
foreach (var item in data)
{
double angle = 2 * Math.PI * item.Value / total;
double endAngle = currentAngle + angle;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
double dx = x - centerX;
double dy = y - centerY;
double dist = Math.Sqrt(dx * dx + dy * dy);
if (dist > radius - 1 && dist <= radius + 0.5)
{
double pointAngle = Math.Atan2(dy, dx);
if (pointAngle < 0) pointAngle += 2 * Math.PI;
bool inSector = (pointAngle >= currentAngle && pointAngle <= endAngle) ||
(currentAngle > endAngle && (pointAngle >= currentAngle || pointAngle <= endAngle));
if (inSector && canvas[y, x] == ' ')
canvas[y, x] = GetShadeChar(item.Label.GetHashCode());
}
}
}
currentAngle = endAngle;
}
// Отрисовка круга
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Console.Write(canvas[y, x]);
}
Console.WriteLine();
}
// Легенда
Console.WriteLine("\nЛегенда:");
int totalAll = data.Sum(d => d.Value);
foreach (var item in data)
{
double percent = 100.0 * item.Value / totalAll;
Console.WriteLine($"{GetShadeChar(item.Label.GetHashCode())} {item.Label}: {item.Value} ({percent:F1}%)");
}
}
static char GetShadeChar(int hash)
{
char[] shades = { '@', '#', '%', '*', '+', '=', '-', '~' };
return shades[Math.Abs(hash) % shades.Length];
}
}