MaksNew

Untitled

Nov 24th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 12.29 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.  
  12.     ListD = record
  13.         Name: AnsiString;
  14.         Score: Integer;
  15.     end;
  16.  
  17.     AoL = Array of ListD;
  18.  
  19. Var
  20.     Path: String;
  21.     Size: Integer;
  22.     List: AoL;
  23.  
  24. function GetInputType(): InputType;
  25. var
  26.     Ret: InputType;
  27.     Input: String;
  28.     IsCorrect: Boolean;
  29. begin
  30.     IsCorrect := False;
  31.     repeat
  32.         Writeln('Выберите способ ввода предложения файл/консоль (ф/к)');
  33.         Readln(Input);
  34.         if (Input = 'ф') or (Input = 'Ф') or (Input = 'Файл') or (Input = 'файл')
  35.         then
  36.         begin
  37.             Ret := InputType.FromFile;
  38.             IsCorrect := True;
  39.         end
  40.         else if (Input = 'к') or (Input = 'К') or (Input = 'Консоль') or
  41.           (Input = 'консоль') then
  42.         begin
  43.             Ret := InputType.FromConsole;
  44.             IsCorrect := True;
  45.         end;
  46.     until IsCorrect;
  47.     Result := 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) 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(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
  153.               (List[I].Name <> List[0].Name)) then
  154.             begin
  155.                 Writeln(List[I].Name, ' ', List[I].Score);
  156.                 Ind := I;
  157.             End;
  158.         End;
  159.         BufOne := Ind;
  160.         Writeln(List[Ind + 1].Name, ' ', List[Ind + 1].Score);
  161.         For I := Ind + 1 to Size - 1 do
  162.         begin
  163.             If ((List[I].Score = List[Ind + 1].Score) and
  164.               (List[I].Name <> List[Ind + 1].Name)) then
  165.             Begin
  166.                 Writeln(List[I].Name, ' ', List[I].Score);
  167.                 Ind := I;
  168.             End;
  169.         End;
  170.         BufTwo := Ind;
  171.         If BufOne <> BufTwo then
  172.         Begin
  173.             Writeln(List[Ind + 1].Name, ' ', List[Ind + 1].Score);
  174.             For I := Ind + 1 to Size - 1 do
  175.             Begin
  176.                 If ((List[I].Score = List[Ind + 1].Score) and
  177.                   (List[I].Name <> List[Ind + 1].Name)) then
  178.                     Writeln(List[I].Name, ' ', List[I].Score)
  179.             End;
  180.         End
  181.         Else
  182.         Begin
  183.             Writeln(List[Ind + 2].Name, ' ', List[Ind + 2].Score);
  184.             For I := Ind + 2 to Size - 1 do
  185.             Begin
  186.                 If ((List[I].Score = List[Ind + 2].Score) and
  187.                   (List[I].Name <> List[Ind + 2].Name)) then
  188.                 Begin
  189.                     Writeln(List[I].Name, ' ', List[I].Score)
  190.                 End;
  191.             End
  192.         End;
  193.     End;
  194.  
  195.     Function IsFileCorrect(Path: String; Size: Integer): Boolean;
  196.     Var
  197.         ListFile: TextFile;
  198.         I, J, Num: Integer;
  199.         IsCorrect: Boolean;
  200.         Buf, NumS: AnsiString;
  201.         Letters: Set of AnsiChar;
  202.     Begin
  203.         AssignFile(ListFile, Path);
  204.         Reset(ListFile);
  205.         IsCorrect := True;
  206.         I := 1;
  207.         Letters := ['А' .. 'Я', 'а' .. 'я'];
  208.         While not(SeekEof(ListFile)) and IsCorrect do
  209.         Begin
  210.             If I mod 2 = 1 then
  211.             Begin
  212.                 While not(SeekEoln(ListFile)) and IsCorrect do
  213.                 Begin
  214.                     Read(ListFile, NumS);
  215.                     For J := 1 to Length(NumS) do
  216.                     Begin
  217.                         If NOT(NumS[J] in Letters) then
  218.                         Begin
  219.                             IsCorrect := False;
  220.                         End;
  221.                     End;
  222.                 End;
  223.             End
  224.             Else
  225.             Begin
  226.                 While not(SeekEoln(ListFile)) and IsCorrect do
  227.                 Begin
  228.                     Try
  229.                         Read(ListFile, Num);
  230.                     Except
  231.                         IsCorrect := False;
  232.                     End;
  233.                 End;
  234.                 Readln(ListFile);
  235.             End;
  236.             Inc(I);
  237.         End;
  238.         If I <> Size * 2 + 1 then
  239.         Begin
  240.             IsCorrect := False;
  241.         End;
  242.         CloseFile(ListFile);
  243.         IsFileCorrect := IsCorrect;
  244.     End;
  245.  
  246.     Function GetFilePath(): String;
  247.     Var
  248.         Path: String;
  249.         IsCorrect: Boolean;
  250.     Begin
  251.         Repeat
  252.             Writeln('Введите абсолютный путь к файлу ');
  253.             Readln(Path);
  254.             IsCorrect := False;
  255.             If FileExists(Path) then
  256.             Begin
  257.                 If IsFileCorrect(Path, Size) then
  258.                     IsCorrect := True
  259.                 Else
  260.                     Writeln('Данные в файле некорректны');
  261.             End
  262.             Else
  263.                 Writeln('Файл не найден');
  264.  
  265.         Until IsCorrect;
  266.         GetFilePath := Path;
  267.     End;
  268.  
  269.     Function GetListFromFile(List: AoL; Size: Integer): AoL;
  270.     Var
  271.         ListFile: TextFile;
  272.         I: Integer;
  273.     Begin
  274.         Path := GetFilePath();
  275.         AssignFile(ListFile, Path);
  276.         Reset(ListFile);
  277.         SetLength(List, Size);
  278.         For I := 0 to Size - 1 do
  279.         Begin
  280.             Readln(ListFile, List[I].Name);
  281.             Readln(ListFile, List[I].Score)
  282.         End;
  283.         CloseFile(ListFile);
  284.         GetListFromFile := List;
  285.     End;
  286.  
  287.     function GetList(): AoL;
  288.     var
  289.         SelectedInputType: InputType;
  290.     begin
  291.         SelectedInputType := GetInputType();
  292.         if (SelectedInputType = InputType.FromConsole) then
  293.         begin
  294.             List := GetListFromConsole(List, Size)
  295.         end
  296.         else if (SelectedInputType = InputType.FromFile) then
  297.         begin
  298.             List := GetListFromFile(List, Size)
  299.         end;
  300.         GetList := List;
  301.     end;
  302.  
  303.     Function Output(): String;
  304.     Var
  305.         Patho: String;
  306.         IsCorrect: Boolean;
  307.     Begin
  308.         IsCorrect := False;
  309.         Repeat
  310.             Writeln('Введите директорию, в которую хотите сохранить результат');
  311.             Readln(Patho);
  312.             If DirectoryExists(Patho) then
  313.                 IsCorrect := True
  314.             Else
  315.                 Writeln('Такой директории не существует. Попробуйте ещё раз');
  316.         Until IsCorrect;
  317.         Output := Patho;
  318.     End;
  319.  
  320.     Procedure SaveListToFile(List: AoL);
  321.     Var
  322.         Ind, I, J, BufOne, BufTwo: Integer;
  323.         OutputFile: TextFile;
  324.         Directory: String;
  325.     Begin
  326.         Directory := Output();
  327.         AssignFile(OutputFile, Directory + OutputFileName);
  328.         Rewrite(OutputFile);
  329.         Ind := 0;
  330.         Writeln(OutputFile, List[0].Name, ' ', List[0].Score);
  331.         For I := 0 to Size - 1 do
  332.         Begin
  333.             if ((List[I].Score = List[0].Score) and
  334.               (List[I].Name <> List[0].Name)) then
  335.             begin
  336.                 Writeln(OutputFile, List[I].Name, ' ', List[I].Score);
  337.                 Ind := I;
  338.             End;
  339.         End;
  340.         BufOne := Ind;
  341.         Writeln(OutputFile, List[Ind + 1].Name, ' ', List[Ind + 1].Score);
  342.         For I := Ind + 1 to Size - 1 do
  343.         begin
  344.             If ((List[I].Score = List[Ind + 1].Score) and
  345.               (List[I].Name <> List[Ind + 1].Name)) then
  346.             Begin
  347.                 Writeln(OutputFile, List[I].Name, ' ', List[I].Score);
  348.                 Ind := I;
  349.             End;
  350.         End;
  351.         BufTwo := Ind;
  352.         If BufOne <> BufTwo then
  353.         Begin
  354.             Writeln(OutputFile, List[Ind + 1].Name, ' ', List[Ind + 1].Score);
  355.             for I := Ind + 1 to Size - 1 do
  356.                 If ((List[I].Score = List[Ind + 1].Score) and
  357.                   (List[I].Name <> List[Ind + 1].Name)) then
  358.                     Writeln(OutputFile, List[I].Name, ' ', List[I].Score)
  359.         End
  360.         Else
  361.         Begin
  362.             Writeln(OutputFile, List[Ind + 2].Name, ' ', List[Ind + 2].Score);
  363.             For I := Ind + 2 to Size - 1 do
  364.             Begin
  365.                 If ((List[I].Score = List[Ind + 2].Score) and
  366.                   (List[I].Name <> List[Ind + 2].Name)) then
  367.                 Begin
  368.                     Writeln(OutputFile, List[I].Name, ' ', List[I].Score)
  369.                 End;
  370.             End
  371.         End;
  372.         Writeln('Результат сохранён по указанному пути');
  373.         CloseFile(OutputFile);
  374.         Write;
  375.     End;
  376.  
  377.     Procedure PrintListBeforeSorting(List: AoL; Size: Integer);
  378.     Var
  379.         I: Integer;
  380.     Begin
  381.         Writeln;
  382.         Writeln('Список игроков до сортировки: ');
  383.         for I := 0 to Size - 1 do
  384.         Begin
  385.             Writeln(List[I].Name, ' ', List[I].Score)
  386.         End;
  387.         Writeln;
  388.     End;
  389.  
  390.     Procedure PrintListAfterSorting(List: AoL; Size: Integer);
  391.     Var
  392.         I: Integer;
  393.     Begin
  394.         Writeln('Список игроков после сортировки: ');
  395.         for I := 0 to Size - 1 do
  396.         Begin
  397.             Writeln(List[I].Name, ' ', List[I].Score)
  398.         End;
  399.         Writeln;
  400.     End;
  401.  
  402. Begin
  403.     Size := GetSize(Size);
  404.         List := GetList();
  405.         PrintListBeforeSorting(List, Size);
  406.         Sorting(List);
  407.         PrintListAfterSorting(List, Size);
  408.         Top3Output(List, Size);
  409.         SaveListToFile(List);
  410.         Readln;
  411.  
  412. End.
Advertisement
Add Comment
Please, Sign In to add comment