fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. public class PoemSorter
  6. {
  7. public static void Notepad(string filePath)
  8. {
  9. string[] poem = {
  10. "Уронили мишку на пол",
  11. "Оторвали мишке лапу",
  12. "Все равно его не брошу",
  13. "Потому что он хороший"
  14. };
  15.  
  16. File.WriteAllLines(filePath, poem);
  17. }
  18.  
  19. public static void Main(string[] args)
  20. {
  21. string filePath = "poem.txt";
  22. string sortedFilePath = "sorted_poem.txt";
  23.  
  24. Notepad(filePath);
  25.  
  26. string[] lines = File.ReadAllLines(filePath);
  27.  
  28. List<string> allWords = new List<string>();
  29. foreach (string line in lines)
  30. {
  31. string[] words = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  32. for (int i = 0; i < words.Length; i++)
  33. {
  34. allWords.Add(words[i]);
  35. }
  36. }
  37.  
  38. for (int i = 0; i < allWords.Count - 1; i++)
  39. {
  40. for (int j = 0; j < allWords.Count - i - 1; j++)
  41. {
  42. if (string.Compare(allWords[j], allWords[j + 1]) > 0)
  43. {
  44. string temp = allWords[j];
  45. allWords[j] = allWords[j + 1];
  46. allWords[j + 1] = temp;
  47. }
  48. }
  49. }
  50.  
  51. List<string> sortedLines = new List<string>();
  52. int wordIndex = 0;
  53. for (int i = 0; i < lines.Length; i++)
  54. {
  55. string[] originalWords = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  56. int wordsInLine = originalWords.Length;
  57. List<string> lineWords = new List<string>();
  58.  
  59. for (int j = 0; j < wordsInLine; j++)
  60. {
  61. lineWords.Add(allWords[wordIndex]);
  62. wordIndex++;
  63. }
  64.  
  65. sortedLines.Add(string.Join(" ", lineWords));
  66. }
  67.  
  68. File.WriteAllLines(sortedFilePath, sortedLines);
  69.  
  70. Console.WriteLine("Отсортированный текст:");
  71. foreach (string line in sortedLines)
  72. {
  73. Console.WriteLine(line);
  74. }
  75. }
  76. }
  77.  
  78.  
Success #stdin #stdout 0.06s 30516KB
stdin
уронили мишку на пол
оторвали мишке лапу
все равно его не брошу
потому что он хороший
stdout
Отсортированный текст:
брошу Все его лапу
мишке мишку на
не он Оторвали пол Потому
равно Уронили хороший что