MaksNew

Untitled

Nov 22nd, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.84 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.         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 GetSize(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 < 3 then
  63.         Begin
  64.         Writeln('Минимальное количество участников: 3');
  65.         IsCorrect := False
  66.         End;
  67.     Until (IsCorrect);
  68.     GetSize := Size;
  69. End;
  70.  
  71. Function GetListFromConsole(List: AoL; Size: Integer): AoL;
  72. Var
  73.     I, J, Ind, K, B, bif: Integer;
  74.     IsCorrect: Boolean;
  75.     Buf: AnsiString;
  76.     bfs: String;
  77.     Letters: Set of AnsiChar;
  78. Begin
  79. SetLength(List, Size);
  80. Letters := ['А'..'Я' , 'а'..'я'];
  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 Letters)) 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.     GetListFromConsole := List;
  119. End;
  120.  
  121. Procedure Sorting (var List: AoL);
  122. Var
  123.     J, I, BufI :Integer;
  124.     BufS: String;
  125. Begin
  126. For I := 0 to Size-1 do
  127. Begin
  128. For J := I + 1 to Size do
  129. Begin
  130.     If List[I].Score < List[J].Score then
  131.     Begin
  132.         BufI := List[J].Score;
  133.         List[J].Score := List[I].Score;
  134.         List[I].Score := BufI;
  135.         BufS := List[I].Name;
  136.         List[I].Name := List[J].Name;
  137.         List[J].Name := BufS;
  138.     End;
  139. End;
  140. End;
  141. End;
  142.  
  143. Procedure Top3Output (var List: AoL; Size: Integer);
  144. Var
  145.     Ind, I, J, BufOne, BufTwo: Integer;
  146. Begin
  147. Ind := 0;
  148. Writeln('Участники, показавшие 3 лучших результата: ');
  149. Writeln(List[0].Name, ' ', List[0].Score);
  150.     For I := 0 to Size-1 do
  151.     Begin
  152.         if ((List[I].Score = List[0].Score) and (List[I].Name <> List[0].Name)) then
  153.         begin
  154.             Writeln(List[I].Name, ' ', List[I].Score);
  155.             Ind := I;
  156.         End;
  157.     End;
  158.     BufOne := Ind;
  159.     Writeln(List[Ind+1].Name, ' ', List[Ind+1].Score);
  160.     For I := Ind+1 to Size-1 do
  161.     begin
  162.         If ((List[I].Score = List[Ind+1].Score) and (List[I].Name <> List[Ind+1].Name)) then
  163.         Begin
  164.             Writeln(List[I].Name, ' ', List[I].Score);
  165.             Ind := I;
  166.         End;
  167.     End;
  168.     BufTwo := Ind;
  169.     If BufOne <> BufTwo then
  170.     Begin
  171.         Writeln(List[Ind+1].Name, ' ', List[Ind+1].Score);
  172.         For I := Ind+1 to Size-1 do
  173.         Begin
  174.         If ((List[I].Score = List[Ind+1].Score) and (List[I].Name <> List[Ind+1].Name)) then
  175.             Writeln(List[I].Name, ' ', List[I].Score)
  176.         End;
  177.     End
  178.     Else
  179.     Begin
  180.         Writeln(List[Ind+2].Name, ' ', List[Ind+2].Score);
  181.         For I := Ind+2 to Size-1 do
  182.         Begin
  183.             If ((List[I].Score = List[Ind+2].Score) and (List[I].Name <> List[Ind+2].Name)) then
  184.             Begin
  185.                 Writeln(List[I].Name, ' ', List[I].Score)
  186.             End;
  187.         End
  188.     End;
  189. End;
  190.  
  191. Function IsFileCorrect(Path: String; Size:Integer): Boolean;
  192. Var
  193.     ListFile: TextFile;
  194.     I, J, Num: Integer;
  195.     IsCorrect: Boolean;
  196.     Buf, NumS: AnsiString;
  197.     Letters: Set of Ansichar;
  198. Begin
  199.     AssignFile(ListFile, Path);
  200.     Reset(ListFile);
  201.     IsCorrect := True;
  202.     I := 1;
  203.     Letters := ['А'..'Я' , 'а'..'я'];
  204.   While not(SeekEof(ListFile)) and IsCorrect do
  205.   Begin
  206.         If I mod 2 = 1 then
  207.         Begin
  208.             While not(SeekEoln(ListFile)) and IsCorrect do
  209.             Begin
  210.                 Read(ListFile, NumS);
  211.                 For J := 1 to Length(NumS) do
  212.                 Begin
  213.                     If NOT(NumS[J] in Letters) then
  214.                     Begin
  215.                     IsCorrect := False;
  216.                     End;
  217.                 End;
  218.             End;
  219.         End
  220.         Else
  221.         Begin
  222.             While not(SeekEoln(ListFile)) and IsCorrect do
  223.             Begin
  224.                 Try
  225.                     Read(ListFile, Num);
  226.                 Except
  227.                     IsCorrect := False;
  228.                 End;
  229.             End;
  230.             Readln(ListFile);
  231.         End;
  232.     Inc(I);
  233.   End;
  234.   If I <> Size*2+1 then
  235.   Begin
  236.   IsCorrect := False;
  237.   End;
  238.   CloseFile(ListFile);
  239.   IsFileCorrect := IsCorrect;
  240. End;
  241.  
  242. Function GetFilePath(): String;
  243. Var
  244.     Path: String;
  245.     IsCorrect: Boolean;
  246. Begin
  247.     Repeat
  248.         Writeln('Введите абсолютный путь к файлу ');
  249.         Readln(Path);
  250.         IsCorrect := False;
  251.         If FileExists(Path) then
  252.         Begin
  253.             If IsFileCorrect(Path, Size) then
  254.                 IsCorrect := True
  255.             Else
  256.                 Writeln('Данные в файле некорректны');
  257.         End
  258.         Else
  259.             Writeln('Файл не найден');
  260.  
  261.     Until IsCorrect;
  262.     GetFilePath := Path;
  263. End;
  264.  
  265. Function GetListFromFile(List:AoL; Size: Integer): AoL;
  266. Var
  267.     ListFile: TextFile;
  268.     I: Integer;
  269. Begin
  270.     Path := GetFilePath();
  271.     AssignFile(ListFile, Path);
  272.     Reset(ListFile);
  273.     SetLength(List, Size);
  274.     For I := 0 to Size-1 do
  275.     Begin
  276.         Readln(ListFile, List[I].Name);
  277.         Readln(ListFile, List[I].Score)
  278.     End;
  279.     CloseFile(ListFile);
  280.     GetListFromFile := List;
  281. End;
  282.  
  283. function GetList():AoL;
  284. var
  285.     SelectedInputType: InputType;
  286. begin
  287.     SelectedInputType := GetInputType();
  288.     if (SelectedInputType = InputType.FromConsole) then
  289.     begin
  290.         List := GetListFromConsole(List, Size)
  291.     end
  292.     else if (SelectedInputType = InputType.FromFile) then
  293.     begin
  294.         List := GetListFromFile(List, Size)
  295.     end;
  296.     GetList := List;
  297. end;
  298.  
  299. Function Output(): String;
  300. Var
  301.     Patho: String;
  302.     IsCorrect: Boolean;
  303. Begin
  304.     IsCorrect := False;
  305.     Repeat
  306.         Writeln('Введите директорию, в которую хотите сохранить результат');
  307.         Readln(Patho);
  308.         If DirectoryExists(Patho) then
  309.             IsCorrect := True
  310.         Else
  311.             Writeln('Такой директории не существует. Попробуйте ещё раз');
  312.     Until IsCorrect;
  313.     Output := Patho;
  314. End;
  315.  
  316. Procedure SaveListToFile(List: AoL);
  317. Var
  318.     Ind, I, J, BufOne, BufTwo: Integer;
  319.     OutputFile: TextFile;
  320.     Directory: String;
  321. Begin
  322.     Directory := Output();
  323.     AssignFile(OutputFile, Directory + OutputFileName);
  324.     Rewrite(OutputFile);
  325.     Ind := 0;
  326.     Writeln(OutputFile, List[0].Name, ' ', List[0].Score);
  327.     For I := 0 to Size-1 do
  328.     Begin
  329.         if ((List[I].Score = List[0].Score) and (List[I].Name <> List[0].Name)) then
  330.         begin
  331.             Writeln(OutputFile, List[I].Name, ' ', List[I].Score);
  332.             Ind := I;
  333.         End;
  334.     End;
  335.     BufOne := Ind;
  336.     Writeln(OutputFile, List[Ind+1].Name, ' ', List[Ind+1].Score);
  337.     For I := Ind+1 to Size-1 do
  338.     begin
  339.         If ((List[I].Score = List[Ind+1].Score) and (List[I].Name <> List[Ind+1].Name)) then
  340.         Begin
  341.             Writeln(OutputFile, List[I].Name, ' ', List[I].Score);
  342.             Ind := I;
  343.         End;
  344.     End;
  345.     BufTwo := Ind;
  346.     If BufOne <> BufTwo then
  347.     Begin
  348.         Writeln(OutputFile, List[Ind+1].Name, ' ', List[Ind+1].Score);
  349.         for I := Ind+1 to Size-1 do
  350.         If ((List[I].Score = List[Ind+1].Score) and (List[I].Name <> List[Ind+1].Name)) then
  351.             Writeln(OutputFile, List[I].Name, ' ', List[I].Score)
  352.     End
  353.     Else
  354.     Begin
  355.         Writeln(OutputFile, List[Ind+2].Name, ' ', List[Ind+2].Score);
  356.         For I := Ind+2 to Size-1 do
  357.         Begin
  358.             If ((List[I].Score = List[Ind+2].Score) and (List[I].Name <> List[Ind+2].Name)) then
  359.             Begin
  360.                 Writeln(OutputFile, List[I].Name, ' ', List[I].Score)
  361.             End;
  362.         End
  363.     End;
  364.     Writeln('Результат сохранён по указанному пути');
  365.     CloseFile(OutputFile);
  366.     Write;
  367. End;
  368.  
  369. Procedure PrintListBeforeSorting(List: AoL; Size: Integer);
  370. Var
  371.     I: Integer;
  372. Begin
  373.     Writeln;
  374.     Writeln('Список игроков до сортировки: ');
  375.     for I := 0 to Size-1 do
  376.     Begin
  377.         Writeln(List[I].Name, ' ', List[I].Score)
  378.     End;
  379.     Writeln;
  380. End;
  381.  
  382. Procedure PrintListAfterSorting(List: AoL; Size: Integer);
  383. Var
  384.     I: Integer;
  385. Begin
  386.     Writeln('Список игроков после сортировки: ');
  387.     for I := 0 to Size-1 do
  388.     Begin
  389.         Writeln(List[I].Name, ' ', List[I].Score)
  390.     End;
  391.     Writeln;
  392. End;
  393.  
  394. Begin
  395.     Size := GetSize(Size);
  396.     List := GetList();
  397.     PrintListBeforeSorting(List, Size);
  398.     Sorting(List);
  399.     PrintListAfterSorting(List, Size);
  400.     Top3Output(List, Size);
  401.     SaveListToFile(List);
  402.     Readln;
  403.  
  404. End.
Advertisement
Add Comment
Please, Sign In to add comment