MaksNew

Untitled

Jan 28th, 2021
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.23 KB | None | 0 0
  1. Program b3_3;
  2.  
  3. Uses
  4. System.SysUtils, Classes;
  5.  
  6. Const
  7. OutputFileName = '\output.txt';
  8.  
  9. Type
  10. InputType = (FromConsole, FromFile);
  11. ListD = Record
  12. Name:AnsiString;
  13. Score:Integer;
  14. End;
  15. AoL = Array of ListD;
  16.  
  17. Var
  18. Path: String;
  19. Size: Integer;
  20. List: AoL;
  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. (ф/к)');
  32. Readln(Input);
  33. If (Input = 'ф') or (Input = 'Ф') or (Input = 'Файл')
  34. or (Input = 'файл')
  35. then
  36. Begin
  37. Ret := InputType.FromFile;
  38. IsCorrect := True;
  39. End
  40. Else if (Input = 'к') or (Input = 'К') or (Input =
  41. 'Консоль') or (Input = 'консоль') then
  42. Begin
  43. Ret := InputType.FromConsole;
  44. IsCorrect := True;
  45. End;
  46. Until IsCorrect;
  47. GetInputType := Ret;
  48. End;
  49.  
  50. Function GetSize(Size: Integer): Integer;
  51. Var
  52. IsCorrect: Boolean;
  53. Begin
  54. Writeln('Введите количество участников: ');
  55. Repeat
  56. IsCorrect := True;
  57. Try
  58. Readln(Size);
  59. Except
  60. Writeln('Введите целое число!');
  61. IsCorrect := False;
  62. End;
  63. If Size < 3 then
  64. Begin
  65. Writeln('Минимальное количество участников: 3');
  66. IsCorrect := False
  67. End;
  68. Until (IsCorrect);
  69. GetSize := Size;
  70. End;
  71.  
  72. Function GetListFromConsole(List: AoL; Size: Integer): AoL;
  73. Var
  74. I, J, Ind, K, B, bif: Integer;
  75. IsCorrect: Boolean;
  76. Buf: AnsiString;
  77. bfs: String;
  78. Letters: Set of AnsiChar;
  79. Begin
  80. SetLength(List, Size);
  81. Letters := ['А'..'Я' , 'а'..'я'];
  82. For I := 0 to Size-1 do
  83. Begin
  84. Repeat
  85. IsCorrect := True;
  86. Writeln('Введите фамилию участника');
  87. Readln(List[I].Name);
  88. List[I].Name := Trim(List[I].Name);
  89. Buf := List[I].Name;
  90. For J := 1 to Length(Buf) do
  91. Begin
  92. If (NOT(Buf[J] in Letters)) then
  93. Begin
  94. IsCorrect := False;
  95. End;
  96. End;
  97. If IsCorrect = false then
  98. Begin
  99. Writeln('Фамилия должна состоять из русских букв')
  100. End;
  101. Until (IsCorrect);
  102.  
  103. Repeat
  104. Writeln('Введите счёт игрока');
  105. IsCorrect := True;
  106. Try
  107. Readln(List[I].Score);
  108. Except
  109. Writeln('Введите целое число!');
  110. IsCorrect := False;
  111. End;
  112. If List[I].Score < 0 then
  113. Begin
  114. Writeln('Введите неотрицательное число!');
  115. IsCorrect := False
  116. End;
  117. Until (IsCorrect)
  118. End;
  119. GetListFromConsole := List;
  120. End;
  121.  
  122. Procedure Sorting (var List: AoL);
  123. Var
  124. J, I, BufI :Integer;
  125. BufS: String;
  126. Begin
  127. For I := 0 to Size-1 do
  128. Begin
  129. For J := I + 1 to Size do
  130. Begin
  131. If List[I].Score < List[J].Score then
  132. Begin
  133. BufI := List[J].Score;
  134. List[J].Score := List[I].Score;
  135. List[I].Score := BufI;
  136. BufS := List[I].Name;
  137. List[I].Name := List[J].Name;
  138. List[J].Name := BufS;
  139. End;
  140. End;
  141. End;
  142. End;
  143.  
  144. Procedure Top3Output (List: AoL; Size: Integer);
  145. Var
  146. Ind, I, J, BufOne, BufTwo: Integer;
  147. Begin
  148. Ind := 0;
  149. Writeln('Участники, показавшие 3 лучших результата: ');
  150. Writeln(List[0].Name, ' ', List[0].Score);
  151. For I := 0 to Size-1 do
  152. Begin
  153. If ((List[I].Score = List[0].Score) and (List[I].Name
  154. <> List[0].Name)) then
  155. begin
  156. Writeln(List[I].Name, ' ', List[I].Score);
  157. Ind := I;
  158. End;
  159. End;
  160. BufOne := Ind;
  161. Writeln(List[Ind+1].Name, ' ', List[Ind+1].Score);
  162. For I := Ind+1 to Size-1 do
  163. Begin
  164. If ((List[I].Score = List[Ind+1].Score) and
  165. (List[I].Name <> List[Ind+1].Name)) then
  166. Begin
  167. Writeln(List[I].Name, ' ', List[I].Score);
  168. Ind := I;
  169. End;
  170. End;
  171. BufTwo := Ind;
  172. If BufOne <> BufTwo then
  173. Begin
  174. Writeln(List[Ind+1].Name, ' ', List[Ind+1].Score);
  175. For I := Ind+1 to Size-1 do
  176. Begin
  177. If ((List[I].Score = List[Ind+1].Score) and
  178. (List[I].Name <> List[Ind+1].Name)) then
  179. Writeln(List[I].Name, ' ', List[I].Score)
  180. End;
  181. End
  182. Else
  183. Begin
  184. Writeln(List[Ind+2].Name, ' ', List[Ind+2].Score);
  185. For I := Ind+2 to Size-1 do
  186. Begin
  187. If ((List[I].Score = List[Ind+2].Score) and
  188. (List[I].Name <> List[Ind+2].Name)) then
  189. Begin
  190. Writeln(List[I].Name, ' ', List[I].Score)
  191. End;
  192. End
  193. End;
  194. End;
  195.  
  196. Function IsFileCorrect(Path: String; Size:Integer): Boolean;
  197. Var
  198. ListFile: TextFile;
  199. I, J, Num: Integer;
  200. IsCorrect: Boolean;
  201. Buf, NumS: AnsiString;
  202. Letters: Set of Ansichar;
  203. Begin
  204. AssignFile(ListFile, Path);
  205. Reset(ListFile);
  206. IsCorrect := True;
  207. I := 1;
  208. Letters := ['А'..'Я' , 'а'..'я'];
  209. While not(SeekEof(ListFile)) and IsCorrect do
  210. Begin
  211. If I mod 2 = 1 then
  212. Begin
  213. While not(SeekEoln(ListFile)) and IsCorrect do
  214. Begin
  215. Read(ListFile, NumS);
  216. For J := 1 to Length(NumS) do
  217. Begin
  218. If NOT(NumS[J] in Letters) then
  219. Begin
  220. IsCorrect := False;
  221. End;
  222. End;
  223. End;
  224. End
  225. Else
  226. Begin
  227. While not(SeekEoln(ListFile)) and IsCorrect do
  228. Begin
  229. Try
  230. Read(ListFile, Num);
  231. Except
  232. IsCorrect := False;
  233. End;
  234. End;
  235. Readln(ListFile);
  236. End;
  237. Inc(I);
  238. End;
  239. If I <> Size*2+1 then
  240. Begin
  241. IsCorrect := False;
  242. End;
  243. CloseFile(ListFile);
  244. IsFileCorrect := IsCorrect;
  245. End;
  246.  
  247. Function GetFilePath(): String;
  248. Var
  249. Path: String;
  250. IsCorrect: Boolean;
  251. Begin
  252. Repeat
  253. Writeln('Введите абсолютный путь к файлу ');
  254. Readln(Path);
  255. IsCorrect := False;
  256. If FileExists(Path) then
  257. Begin
  258. If IsFileCorrect(Path, Size) then
  259. IsCorrect := True
  260. Else
  261. Writeln('Данные в файле некорректны');
  262. End
  263. Else
  264. Writeln('Файл не найден');
  265.  
  266. Until IsCorrect;
  267. GetFilePath := Path;
  268. End;
  269.  
  270. Function GetListFromFile(List:AoL; Size: Integer): AoL;
  271. Var
  272. ListFile: TextFile;
  273. I: Integer;
  274. Begin
  275. Path := GetFilePath();
  276. AssignFile(ListFile, Path);
  277. Reset(ListFile);
  278. SetLength(List, Size);
  279. For I := 0 to Size-1 do
  280. Begin
  281. Readln(ListFile, List[I].Name);
  282. Readln(ListFile, List[I].Score)
  283. End;
  284. CloseFile(ListFile);
  285. GetListFromFile := List;
  286. End;
  287.  
  288. Function GetList():AoL;
  289. Var
  290. SelectedInputType: InputType;
  291. Begin
  292. SelectedInputType := GetInputType();
  293. If (SelectedInputType = InputType.FromConsole) then
  294. Begin
  295. List := GetListFromConsole(List, Size)
  296. End
  297. Else if (SelectedInputType = InputType.FromFile) then
  298. Begin
  299. List := GetListFromFile(List, Size)
  300. End;
  301. GetList := List;
  302. End;
  303.  
  304. Function Output(): String;
  305. Var
  306. Patho: String;
  307. IsCorrect: Boolean;
  308. Begin
  309. IsCorrect := False;
  310. Repeat
  311. Writeln('Введите директорию, в которую хотите сохранить
  312. результат');
  313. Readln(Patho);
  314. If DirectoryExists(Patho) then
  315. IsCorrect := True
  316. Else
  317. Writeln('Такой директории не существует. Попробуйте
  318. ещё раз');
  319. Until IsCorrect;
  320. Output := Patho;
  321. End;
  322.  
  323. Procedure SaveListToFile(List: AoL);
  324. Var
  325. Ind, I, J, BufOne, BufTwo: Integer;
  326. OutputFile: TextFile;
  327. Directory: String;
  328. Begin
  329. Directory := Output();
  330. AssignFile(OutputFile, Directory + OutputFileName);
  331. Rewrite(OutputFile);
  332. Ind := 0;
  333. Writeln(OutputFile, List[0].Name, ' ', List[0].Score);
  334. For I := 0 to Size-1 do
  335. Begin
  336. If ((List[I].Score = List[0].Score) and (List[I].Name
  337. <> List[0].Name)) then
  338. Begin
  339. Writeln(OutputFile, List[I].Name, ' ',
  340. List[I].Score);
  341. Ind := I;
  342. End;
  343. End;
  344. BufOne := Ind;
  345. Writeln(OutputFile, List[Ind+1].Name, ' ',
  346. List[Ind+1].Score);
  347. For I := Ind+1 to Size-1 do
  348. Begin
  349. If ((List[I].Score = List[Ind+1].Score) and
  350. (List[I].Name <> List[Ind+1].Name)) then
  351. Begin
  352. Writeln(OutputFile, List[I].Name, ' ', List[I].Score);
  353. Ind := I;
  354. End;
  355. End;
  356. BufTwo := Ind;
  357. If BufOne <> BufTwo then
  358. Begin
  359. Writeln(OutputFile, List[Ind+1].Name, ' ',
  360. List[Ind+1].Score);
  361. For I := Ind+1 to Size-1 do
  362. If ((List[I].Score = List[Ind+1].Score) and
  363. (List[I].Name <> List[Ind+1].Name)) then
  364. Writeln(OutputFile, List[I].Name, ' ',
  365. List[I].Score)
  366. End
  367. Else
  368. Begin
  369. Writeln(OutputFile, List[Ind+2].Name, ' ',
  370. List[Ind+2].Score);
  371. For I := Ind+2 to Size-1 do
  372. Begin
  373. If ((List[I].Score = List[Ind+2].Score) and
  374. (List[I].Name <> List[Ind+2].Name)) then
  375. Begin
  376. Writeln(OutputFile, List[I].Name, ' ',
  377. List[I].Score)
  378. End;
  379. End
  380. End;
  381. Writeln('Результат сохранён по указанному пути');
  382. CloseFile(OutputFile);
  383. Write;
  384. End;
  385.  
  386. Procedure PrintListBeforeSorting(List: AoL; Size: Integer);
  387. Var
  388. I: Integer;
  389. Begin
  390. Writeln;
  391. Writeln('Список игроков до сортировки: ');
  392. For I := 0 to Size-1 do
  393. Begin
  394. Writeln(List[I].Name, ' ', List[I].Score)
  395. End;
  396. Writeln;
  397. End;
  398.  
  399. Procedure PrintListAfterSorting(List: AoL; Size: Integer);
  400. Var
  401. I: Integer;
  402. Begin
  403. Writeln('Список игроков после сортировки: ');
  404. For I := 0 to Size-1 do
  405. Begin
  406. Writeln(List[I].Name, ' ', List[I].Score)
  407. End;
  408. Writeln;
  409. End;
  410.  
  411. Begin
  412. Size := GetSize(Size);
  413. List := GetList();
  414. PrintListBeforeSorting(List, Size);
  415. Sorting(List);
  416. PrintListAfterSorting(List, Size);
  417. Top3Output(List, Size);
  418. SaveListToFile(List);
  419. Readln;
  420.  
  421. End.
  422.  
Advertisement
Add Comment
Please, Sign In to add comment