Skip to content

Instantly share code, notes, and snippets.

@ImShizer
Created March 14, 2024 23:59
Show Gist options
  • Save ImShizer/6cb21ba06ee295766b0476ba8613ac34 to your computer and use it in GitHub Desktop.
Save ImShizer/6cb21ba06ee295766b0476ba8613ac34 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Threading.Tasks;
class Program
{
static void Main()
{
Console.WriteLine("Введіть текст:");
string inputText = Console.ReadLine();
bool analysisInProgress = false;
while (true)
{
Console.WriteLine("Виберіть дію:");
Console.WriteLine("1. Запустити аналіз тексту");
Console.WriteLine("2. Зупинити аналіз тексту");
Console.WriteLine("3. Повторити аналіз тексту");
Console.WriteLine("4. Зберегти звіт у файл");
Console.WriteLine("5. Вийти з програми");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
if (!analysisInProgress)
{
Task analysisTask = AnalyzeTextAsync(inputText);
analysisInProgress = true;
}
else
{
Console.WriteLine("Аналіз вже виконується.");
}
break;
case "2":
if (analysisInProgress)
{
analysisInProgress = false;
Console.WriteLine("Аналіз зупинено.");
}
else
{
Console.WriteLine("Аналіз не виконується.");
}
break;
case "3":
if (!analysisInProgress)
{
Console.WriteLine("Введіть текст:");
inputText = Console.ReadLine();
}
else
{
Console.WriteLine("Не можна редагувати текст, поки аналіз виконується.");
}
break;
case "4":
if (!analysisInProgress)
{
SaveReportToFile(inputText);
}
else
{
Console.WriteLine("Збереження звіту у файл неможливе, поки аналіз виконується.");
}
break;
case "5":
Environment.Exit(0);
break;
default:
Console.WriteLine("Невірний вибір.");
break;
}
}
}
static async Task AnalyzeTextAsync(string text)
{
await Task.Run(() =>
{
int sentenceCount = 0;
int characterCount = text.Length;
int wordCount = text.Split(new char[] { ' ', '.', ',', '!', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
int questionSentenceCount = 0;
int exclamationSentenceCount = 0;
foreach (char c in text)
{
if (c == '.' || c == '!' || c == '?')
sentenceCount++;
if (c == '?')
questionSentenceCount++;
if (c == '!')
exclamationSentenceCount++;
}
Console.WriteLine("Звіт аналізу тексту:");
Console.WriteLine($"Кількість речень: {sentenceCount}");
Console.WriteLine($"Кількість символів: {characterCount}");
Console.WriteLine($"Кількість слів: {wordCount}");
Console.WriteLine($"Кількість питальних речень: {questionSentenceCount}");
Console.WriteLine($"Кількість окличних речень: {exclamationSentenceCount}");
});
}
static void SaveReportToFile(string text)
{
string fileName = "report.txt";
using (StreamWriter writer = new StreamWriter(fileName))
{
int sentenceCount = 0;
int characterCount = text.Length;
int wordCount = text.Split(new char[] { ' ', '.', ',', '!', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
int questionSentenceCount = 0;
int exclamationSentenceCount = 0;
foreach (char c in text)
{
if (c == '.' || c == '!' || c == '?')
sentenceCount++;
if (c == '?')
questionSentenceCount++;
if (c == '!')
exclamationSentenceCount++;
}
writer.WriteLine("Звіт аналізу тексту:");
writer.WriteLine($"Кількість речень: {sentenceCount}");
writer.WriteLine($"Кількість символів: {characterCount}");
writer.WriteLine($"Кількість слів: {wordCount}");
writer.WriteLine($"Кількість питальних речень: {questionSentenceCount}");
writer.WriteLine($"Кількість окличних речень: {exclamationSentenceCount}");
}
Console.WriteLine($"Звіт був збережений у файл {fileName}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment