MaksNew

Untitled

Nov 14th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. Program b3_3;
  2.  
  3. Uses
  4. System.SysUtils, Classes;
  5.  
  6. type
  7. InputType = (FromConsole, FromFile);
  8. Aos = Array of string;
  9.  
  10.  
  11. Var
  12. Path: String;
  13. Sentence: String;
  14. Size: Integer;
  15. List: Aos;
  16.  
  17.  
  18. function GetInputType(): InputType;
  19. var
  20. Ret: InputType;
  21. Input: String;
  22. IsCorrect: Boolean;
  23. begin
  24. IsCorrect := False;
  25. repeat
  26. Writeln('Выберите способ ввода предложения файл/консоль (ф/к)');
  27. Readln(Input);
  28. if (Input = 'ф') or (Input = 'Ф') or (Input = 'Файл')
  29. or (Input = 'файл')
  30. then
  31. begin
  32. Ret := InputType.FromFile;
  33. IsCorrect := True;
  34. end
  35. else if (Input = 'к') or (Input = 'К') or (Input =
  36. 'Консоль') or (Input = 'консоль') then
  37. begin
  38. Ret := InputType.FromConsole;
  39. IsCorrect := True;
  40. end;
  41. until IsCorrect;
  42. Result := Ret;
  43. end;
  44.  
  45. function IntSize(Size: Integer): Integer;
  46. var
  47. IsCorrect: Boolean;
  48. Begin
  49. Writeln('Введите количество участников: ');
  50. repeat
  51. IsCorrect := True;
  52. try
  53. Readln(Size);
  54. except
  55. Writeln('Введите целое число!');
  56. IsCorrect := False;
  57. end;
  58. if Size <= 0 then
  59. begin
  60. Writeln('Введите положительное число!');
  61. IsCorrect := False
  62. end;
  63. until (IsCorrect);
  64. IntSize := Size;
  65. End;
  66.  
  67. function GetSentenceFromConsole(List: Aos; Size: Integer): Aos;
  68.  
  69. var
  70. I, J: Integer;
  71. Name: String;
  72. IsCorrect: Boolean;
  73.  
  74. begin
  75. SetLength(List, Size);
  76.  
  77. for I := 0 to Size-1 do
  78. begin
  79. Writeln('Введите фамилию участника и его счёт через пробел');
  80. Readln(List[I]);
  81. Name := List[I];
  82. for J := 1 to Length(Name) do
  83. repeat
  84. IsCorrect := True;
  85. Readln(Name[J]);
  86. if (NOT(Name[J] in ['0'..'9', 'А'..'Я' , 'а'..'я', ' '])) then
  87. begin
  88. Writeln('Ошибка ввода');
  89. IsCorrect := False;
  90. end;
  91. until (IsCorrect);
  92. end;
  93.  
  94.  
  95. for I := 0 to Size-1 do
  96. begin
  97. Writeln(List[I]);
  98. end;
  99.  
  100. end;
  101.  
  102. Function GetFilePath(): String;
  103. Var
  104. Path: String;
  105. IsCorrect: Boolean;
  106. Begin
  107. Repeat
  108. Writeln('Введите абсолютный путь к файлу ');
  109. Readln(Path);
  110. IsCorrect := False;
  111. If FileExists(Path) then
  112. Begin
  113. Writeln('Файл успешно открыт');
  114. IsCorrect := true
  115. End
  116. Else
  117. Writeln('Файл не найден');
  118. Until IsCorrect;
  119. Result := Path;
  120. End;
  121.  
  122. Function GetSentenceFromFile(Sentence:String): String;
  123. Var
  124. SenFile: TextFile;
  125. Begin
  126. Path := GetFilePath();
  127. AssignFile(SenFile, Path);
  128. Reset(SenFile);
  129. Read(SenFile, Sentence);
  130. CloseFile(SenFile);
  131. Result := Sentence;
  132. End;
  133.  
  134. Procedure PrintSentenceBeforeEditing(Sentence: String);
  135. Begin
  136. Writeln;
  137. Writeln('Исходное предложение: ');
  138. Write(Sentence);
  139. Writeln;
  140. End;
  141.  
  142. function GetSentence():Aos;
  143. var
  144. SelectedInputType: InputType;
  145. begin
  146. SelectedInputType := GetInputType();
  147. if (SelectedInputType = InputType.FromConsole) then
  148. begin
  149. List := GetSentenceFromConsole(List, Size)
  150. end
  151. else if (SelectedInputType = InputType.FromFile) then
  152. begin
  153. Sentence := GetSentenceFromFile(Sentence);
  154. end;
  155. PrintSentenceBeforeEditing(Sentence);
  156. Result := List;
  157. end;
  158.  
  159. Function Output(): String;
  160. Var
  161. Patho: String;
  162. IsCorrect: Boolean;
  163. Begin
  164. IsCorrect := False;
  165. Repeat
  166. Writeln('Введите директорию, в которую хотите сохранить результат');
  167. Readln(Patho);
  168. If DirectoryExists(Patho) then
  169. IsCorrect := True
  170. Else
  171. Writeln('Такой директории не существует. Попробуйте ещё раз');
  172. Until IsCorrect;
  173. Result := Patho;
  174. End;
  175.  
  176. Procedure SaveSentenceToFile(Sentence: String);
  177. Var
  178. I, J: Integer;
  179. OutputFile: TextFile;
  180. Directory: String;
  181. Begin
  182. Directory := Output();
  183. AssignFile(OutputFile, Directory + '\output.txt');
  184. Rewrite(OutputFile);
  185. Write(OutputFile,Sentence);
  186. Writeln(OutputFile);
  187. Writeln('Результат сохранён по указанному пути');
  188. CloseFile(OutputFile);
  189. Write;
  190. End;
  191.  
  192. Procedure PrintSentenceAfterEditing(var Sentence: String);
  193. Begin
  194. Writeln;
  195. if Length(Sentence) < 1 then
  196. Sentence := 'Ни один символ не прошёл проверку! От вашей строки ничего не осталось!'
  197. else
  198. Writeln('Исправленное предложение:');
  199. Writeln(Sentence);
  200. Writeln;
  201. End;
  202.  
  203. Function Edit(Sentence: String): String;
  204. Var
  205. I:Integer;
  206. Begin
  207. I := 1;
  208. while (I <= Length(Sentence)) do
  209. begin
  210. if (NOT(Sentence[I] in ['0'..'9', 'a'..'z' , 'A'..'Z', ' '])) then
  211. begin
  212. Delete(Sentence, I, 1);
  213. Dec(I);
  214. end;
  215. Inc(I)
  216. end;
  217. Result := Sentence;
  218. End;
  219.  
  220. Begin
  221. Size := IntSize(Size);
  222. List := GetSentence();
  223. Sentence := Edit(Sentence);
  224. PrintSentenceAfterEditing(Sentence);
  225. SaveSentenceToFile(Sentence);
  226. Readln;
  227.  
  228. End.
Advertisement
Add Comment
Please, Sign In to add comment