MaksNew

Untitled

Nov 18th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.01 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.     AoL = Array of ListD;
  13.  
  14.  
  15. Var
  16.     Path: String;
  17.     Size: Integer;
  18.     List: AoL;
  19.  
  20.  
  21. function GetInputType(): InputType;
  22. var
  23.     Ret: InputType;
  24.     Input: String;
  25.     IsCorrect: Boolean;
  26. begin
  27.     IsCorrect := False;
  28.     repeat
  29.         Writeln('Выберите способ ввода предложения файл/консоль (ф/к)');
  30.         Readln(Input);
  31.         if (Input = 'ф') or (Input = 'Ф') or (Input = 'Файл')
  32.         or (Input = 'файл')
  33.         then
  34.         begin
  35.             Ret := InputType.FromFile;
  36.             IsCorrect := True;
  37.         end
  38.         else if (Input = 'к') or (Input = 'К') or (Input =
  39.             'Консоль') or (Input = 'консоль') then
  40.         begin
  41.             Ret := InputType.FromConsole;
  42.             IsCorrect := True;
  43.         end;
  44.     until IsCorrect;
  45.     Result := Ret;
  46. end;
  47.  
  48. function IntSize(Size: Integer): Integer;
  49. var
  50. IsCorrect: Boolean;
  51. Begin
  52.     Writeln('Введите количество участников: ');
  53.     repeat
  54.     IsCorrect := True;
  55.         try
  56.             Readln(Size);
  57.         except
  58.             Writeln('Введите целое число!');
  59.             IsCorrect := False;
  60.         end;
  61.         if Size <= 0 then
  62.         begin
  63.         Writeln('Введите положительное число!');
  64.         IsCorrect := False
  65.         end;
  66.     until (IsCorrect);
  67.     IntSize := Size;
  68. End;
  69.  
  70. Function GetListFromConsole(List: AoL; Size: Integer): AoL;
  71. var
  72.     I, J, Ind, K, B, bif: Integer;
  73.     IsCorrect: Boolean;
  74.     Buf: AnsiString;
  75.     bfs: String;
  76. begin
  77. SetLength(List, Size);
  78.     for I := 0 to Size-1 do
  79.     begin
  80.         repeat
  81.         IsCorrect := True;
  82.             Writeln('Введите фамилию участника');
  83.             Readln(List[I].Name);
  84.             List[I].Name := Trim(List[I].Name);
  85.             Buf := List[I].Name;
  86.             for J := 1 to Length(Buf) do
  87.             begin
  88.                 if (NOT(Buf[J] in ['А'..'Я' , 'а'..'я'])) then
  89.                 begin
  90.                 IsCorrect := False;
  91.                 end;
  92.             end;
  93.             if IsCorrect = false then
  94.             begin
  95.             Writeln('Фамилия должна состоять из русских букв')
  96.             end;
  97.         until (IsCorrect);
  98.  
  99.         repeat
  100.             Writeln('Введите счёт игрока');
  101.             IsCorrect := True;
  102.             try
  103.                 Readln(List[I].Score);
  104.             except
  105.                 Writeln('Введите целое число!');
  106.                 IsCorrect := False;
  107.             end;
  108.                 if List[I].Score < 0 then
  109.                 begin
  110.                     Writeln('Введите неотрицательное число!');
  111.                     IsCorrect := False
  112.                 end;
  113.         until (IsCorrect)
  114.     end;
  115.     GetListFromConsole := List;
  116. End;
  117.  
  118. Procedure Sorting (var List: AoL);
  119. Var
  120.     J, I, BufI :Integer;
  121.     BufS: String;
  122. Begin
  123. for J := I + 1 to Size do
  124. Begin
  125.     If List[I].Score < List[J].Score then
  126.     Begin
  127.         BufI := List[J].Score;
  128.         List[J].Score := List[I].Score;
  129.         List[I].Score := BufI;
  130.         BufS := List[I].Name;
  131.         List[I].Name := List[J].Name;
  132.         List[J].Name := BufS;
  133.     End;
  134. End;
  135. End;
  136.  
  137. Procedure Top3Output (var List: AoL; Size: Integer);
  138. Var
  139.     Ind, I, J, BufOne, BufTwo: Integer;
  140.     HelpArray: AoL;
  141. Begin
  142. //SetLength(HelpArray,
  143. Ind := 0;
  144. Writeln(List[0].Name, ' ', List[0].Score);
  145.     For I := 0 to Size-1 do
  146.     Begin
  147.         if ((List[I].Score = List[0].Score) and (List[I].Name <> List[0].Name)) then
  148.         begin
  149.             Writeln(List[I].Name, ' ', List[I].Score);
  150.             Ind := I;
  151.         End;
  152.     End;
  153.     BufOne := Ind;
  154.     Writeln(List[Ind+1].Name, ' ', List[Ind+1].Score);
  155.     For I := Ind+1 to Size-1 do
  156.     begin
  157.         If ((List[I].Score = List[Ind+1].Score) and (List[I].Name <> List[Ind+1].Name)) then
  158.         Begin
  159.             Writeln(List[I].Name, ' ', List[I].Score);
  160.             Ind := I;
  161.         End;
  162.     End;
  163.     BufTwo := Ind;
  164.     If BufOne <> BufTwo then
  165.     Begin
  166.         Writeln(List[Ind+1].Name, ' ', List[Ind+1].Score);
  167.         for I := Ind+1 to Size-1 do
  168.         If ((List[I].Score = List[Ind+1].Score) and (List[I].Name <> List[Ind+1].Name)) then
  169.             Writeln(List[I].Name, ' ', List[I].Score)
  170.     End
  171.     Else
  172.     Begin
  173.         Writeln(List[Ind+2].Name, ' ', List[Ind+2].Score);
  174.         For I := Ind+2 to Size-1 do
  175.         Begin
  176.             If ((List[I].Score = List[Ind+2].Score) and (List[I].Name <> List[Ind+2].Name)) then
  177.             Begin
  178.                 Writeln(List[I].Name, ' ', List[I].Score)
  179.             End;
  180.         End
  181.     End;
  182. End;
  183.  
  184. Function IsFileCorrect(Path: String; Size:Integer): Boolean;
  185. Var
  186.     ListFile: TextFile;
  187.     I, J, Num: Integer;
  188.     IsCorrect: Boolean;
  189.     Buf, NumS: AnsiString;
  190. Begin
  191.     AssignFile(ListFile, Path);
  192.     Reset(ListFile);
  193.     IsCorrect := True;
  194.     I := 0;
  195.   while not(SeekEof(ListFile)) and IsCorrect do
  196.   begin
  197.         if I mod 2 = 0 then
  198.         begin
  199.             while not(SeekEoln(ListFile)) and IsCorrect do
  200.             begin
  201.                 try
  202.                     Read(ListFile, Num);
  203.                 except
  204.                     writeln('проблема в цирфе');
  205.                     IsCorrect := false;
  206.                 end;
  207.                 Writeln(Num);
  208.             end;
  209.             Readln(ListFile);
  210.         end
  211.         else
  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 ['А'..'Я' , 'а'..'я']) then
  219.                     begin
  220.                     IsCorrect := False;
  221.                     writeln('проблема в слове')
  222.                     end;
  223.                 end;
  224.                 Writeln(NumS);
  225.             end;
  226.             Readln(ListFile);
  227.         end;
  228.     Inc(I);
  229.     writeln('счётчик = ', I);
  230.   End;
  231.   if I <> Size*2 then
  232.   Begin
  233.   writeln('проблема в размере');
  234.   IsCorrect := False;
  235.   End;
  236.   CloseFile(ListFile);
  237.   IsFileCorrect := IsCorrect;
  238. End;
  239.  
  240. Function GetFilePath(): String;
  241. Var
  242.     Path: String;
  243.     IsCorrect: Boolean;
  244. Begin
  245.     Repeat
  246.         Writeln('Введите абсолютный путь к файлу ');
  247.         Readln(Path);
  248.         IsCorrect := False;
  249.         If FileExists(Path) then
  250.         Begin
  251.             If IsFileCorrect(Path, Size) then
  252.                 IsCorrect := true
  253.             Else
  254.                 Writeln('Данные в файле некорректны');
  255.         End
  256.         Else
  257.             Writeln('Файл не найден');
  258.  
  259.     Until IsCorrect;
  260.     GetFilePath := Path;
  261. End;
  262.  
  263. Function GetListFromFile(Size: Integer): AoL;
  264. Var
  265.     ListFile: TextFile;
  266.     I, Num: Integer;
  267.     NumS: AnsiString;
  268.     BufList: AoL;
  269.  
  270. Begin
  271.     Path := GetFilePath();
  272.     SetLength(BufList, Size);
  273.     AssignFile(ListFile, Path);
  274.     Reset(ListFile);
  275.     for I := 0 to High(BufList) do
  276.     Begin
  277.     If I mod 2 = 0 then
  278.         begin
  279.             Read(ListFile, Num);
  280.             Readln(ListFile)
  281.         end
  282.     Else
  283.         begin
  284.             Read(ListFile, NumS);
  285.             Readln(ListFile)
  286.         end;
  287.     End;
  288.     CloseFile(ListFile);
  289.     GetListFromFile := BufList;
  290.  
  291. End;
  292.  
  293. function GetList():AoL;
  294. var
  295.     SelectedInputType: InputType;
  296. begin
  297.     SelectedInputType := GetInputType();
  298.     if (SelectedInputType = InputType.FromConsole) then
  299.     begin
  300.         List := GetListFromConsole(List, Size)
  301.     end
  302.     else if (SelectedInputType = InputType.FromFile) then
  303.     begin
  304.         List := GetListFromFile(Size);
  305.     end;
  306.     GetList := List;
  307. end;
  308.  
  309. Function Output(): String;
  310. Var
  311.     Patho: String;
  312.     IsCorrect: Boolean;
  313. Begin
  314.     IsCorrect := False;
  315.     Repeat
  316.         Writeln('Введите директорию, в которую хотите сохранить результат');
  317.         Readln(Patho);
  318.         If DirectoryExists(Patho) then
  319.             IsCorrect := True
  320.         Else
  321.             Writeln('Такой директории не существует. Попробуйте ещё раз');
  322.     Until IsCorrect;
  323.     Output := Patho;
  324. End;
  325.  
  326. Procedure SaveListToFile(List: AoL);
  327. Var
  328.     I, J: Integer;
  329.     OutputFile: TextFile;
  330.     Directory: String;
  331. Begin
  332.     Directory := Output();
  333.     AssignFile(OutputFile, Directory + '\output.txt');
  334.     Rewrite(OutputFile);
  335.  
  336.  
  337.     Writeln('Результат сохранён по указанному пути');
  338.     CloseFile(OutputFile);
  339.     Write;
  340. End;
  341.  
  342. Begin
  343.     Size := IntSize(Size);
  344.     List := GetList();
  345.     Sorting(List);
  346.     Top3Output(List, Size);
  347.     SaveListToFile(List);
  348.     Readln;
  349.  
  350. End.
Advertisement
Add Comment
Please, Sign In to add comment