MaksNew

Untitled

Nov 14th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 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: AnsiString;
  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. List[I] := Trim(List[I]);
  82. Name := List[I];
  83. for J := 1 to Length(Name) do
  84. begin
  85. repeat
  86. IsCorrect := True;
  87. if (NOT(Name[J] in ['0'..'9', 'А'..'Я' , 'а'..'я', ' '])) then
  88. begin
  89. Writeln('Ошибка ввода');
  90. IsCorrect := False;
  91. end;
  92. until (IsCorrect);
  93. end;
  94.  
  95. //for J := 1 to Pos(' ', List[I]) do
  96.  
  97.  
  98. end;
  99.  
  100.  
  101. for I := 0 to Size-1 do
  102. begin
  103. Writeln(List[I]);
  104. end;
  105.  
  106.  
  107. end;
  108.  
  109. Function GetFilePath(): String;
  110. Var
  111. Path: String;
  112. IsCorrect: Boolean;
  113. Begin
  114. Repeat
  115. Writeln('Введите абсолютный путь к файлу ');
  116. Readln(Path);
  117. IsCorrect := False;
  118. If FileExists(Path) then
  119. Begin
  120. Writeln('Файл успешно открыт');
  121. IsCorrect := true
  122. End
  123. Else
  124. Writeln('Файл не найден');
  125. Until IsCorrect;
  126. Result := Path;
  127. End;
  128.  
  129. Function GetSentenceFromFile(Sentence:String): String;
  130. Var
  131. SenFile: TextFile;
  132. Begin
  133. Path := GetFilePath();
  134. AssignFile(SenFile, Path);
  135. Reset(SenFile);
  136. Read(SenFile, Sentence);
  137. CloseFile(SenFile);
  138. Result := Sentence;
  139. End;
  140.  
  141. Procedure PrintSentenceBeforeEditing(Sentence: String);
  142. Begin
  143. Writeln;
  144. Writeln('Исходное предложение: ');
  145. Write(Sentence);
  146. Writeln;
  147. End;
  148.  
  149. function GetSentence():Aos;
  150. var
  151. SelectedInputType: InputType;
  152. begin
  153. SelectedInputType := GetInputType();
  154. if (SelectedInputType = InputType.FromConsole) then
  155. begin
  156. List := GetSentenceFromConsole(List, Size)
  157. end
  158. else if (SelectedInputType = InputType.FromFile) then
  159. begin
  160. Sentence := GetSentenceFromFile(Sentence);
  161. end;
  162. PrintSentenceBeforeEditing(Sentence);
  163. Result := List;
  164. end;
  165.  
  166. Function Output(): String;
  167. Var
  168. Patho: String;
  169. IsCorrect: Boolean;
  170. Begin
  171. IsCorrect := False;
  172. Repeat
  173. Writeln('Введите директорию, в которую хотите сохранить результат');
  174. Readln(Patho);
  175. If DirectoryExists(Patho) then
  176. IsCorrect := True
  177. Else
  178. Writeln('Такой директории не существует. Попробуйте ещё раз');
  179. Until IsCorrect;
  180. Result := Patho;
  181. End;
  182.  
  183. Procedure SaveSentenceToFile(Sentence: String);
  184. Var
  185. I, J: Integer;
  186. OutputFile: TextFile;
  187. Directory: String;
  188. Begin
  189. Directory := Output();
  190. AssignFile(OutputFile, Directory + '\output.txt');
  191. Rewrite(OutputFile);
  192. Write(OutputFile,Sentence);
  193. Writeln(OutputFile);
  194. Writeln('Результат сохранён по указанному пути');
  195. CloseFile(OutputFile);
  196. Write;
  197. End;
  198.  
  199. Procedure PrintSentenceAfterEditing(var Sentence: String);
  200. Begin
  201. Writeln;
  202. if Length(Sentence) < 1 then
  203. Sentence := 'Ни один символ не прошёл проверку! От вашей строки ничего не осталось!'
  204. else
  205. Writeln('Исправленное предложение:');
  206. Writeln(Sentence);
  207. Writeln;
  208. End;
  209.  
  210. Function Edit(Sentence: String): String;
  211. Var
  212. I:Integer;
  213. Begin
  214. I := 1;
  215. while (I <= Length(Sentence)) do
  216. begin
  217. if (NOT(Sentence[I] in ['0'..'9', 'a'..'z' , 'A'..'Z', ' '])) then
  218. begin
  219. Delete(Sentence, I, 1);
  220. Dec(I);
  221. end;
  222. Inc(I)
  223. end;
  224. Result := Sentence;
  225. End;
  226.  
  227. Begin
  228. Size := IntSize(Size);
  229. List := GetSentence();
  230. Sentence := Edit(Sentence);
  231. PrintSentenceAfterEditing(Sentence);
  232. SaveSentenceToFile(Sentence);
  233. Readln;
  234.  
  235. End.
Advertisement
Add Comment
Please, Sign In to add comment