MaksNew

Untitled

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