MaksNew

Untitled

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