using System;
using System.Collections.Generic;
using System.IO;
public class PoemSorter
{
public static void Notepad(string filePath)
{
string[] poem = {
"Уронили мишку на пол",
"Оторвали мишке лапу",
"Все равно его не брошу",
"Потому что он хороший"
};
File.WriteAllLines(filePath, poem);
}
public static void Main(string[] args)
{
string filePath = "poem.txt";
string sortedFilePath = "sorted_poem.txt";
Notepad(filePath);
string[] lines = File.ReadAllLines(filePath);
List<string> allWords = new List<string>();
foreach (string line in lines)
{
string[] words = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < words.Length; i++)
{
allWords.Add(words[i]);
}
}
for (int i = 0; i < allWords.Count - 1; i++)
{
for (int j = 0; j < allWords.Count - i - 1; j++)
{
if (string.Compare(allWords[j], allWords[j + 1]) > 0)
{
string temp = allWords[j];
allWords[j] = allWords[j + 1];
allWords[j + 1] = temp;
}
}
}
List<string> sortedLines = new List<string>();
int wordIndex = 0;
for (int i = 0; i < lines.Length; i++)
{
string[] originalWords = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int wordsInLine = originalWords.Length;
List<string> lineWords = new List<string>();
for (int j = 0; j < wordsInLine; j++)
{
lineWords.Add(allWords[wordIndex]);
wordIndex++;
}
sortedLines.Add(string.Join(" ", lineWords));
}
File.WriteAllLines(sortedFilePath, sortedLines);
Console.WriteLine("Отсортированный текст:");
foreach (string line in sortedLines)
{
Console.WriteLine(line);
}
}
}