Advertisement
fatalryuu

Untitled

Oct 19th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.71 KB | None | 0 0
  1. Function Sort(Const InputArray: TArr; Path: String): TArr;
  2. Var
  3.     I, J, K, Key, TempValue, Size: Integer;
  4.     OutputFile: TextFile;
  5.     IsCorrect: Boolean;
  6. Begin
  7.    
  8.     Repeat
  9.         IsCorrect := True;
  10.         AssignFile(OutputFile, Path);
  11.         Try
  12.             Rewrite(OutputFile);
  13.         Except
  14.             IsCorrect := False;
  15.             Write('Ошбика! Отказано в доступе! Измените параметры файла или укажите ссылку на новый.', #10#13);
  16.             Path := InputPathToFile();
  17.         End;
  18.     Until IsCorrect;
  19.        
  20.     Writeln(#10#13, 'Сортировка...');
  21.  
  22.     For I := 1 To High(InputArray) Do
  23.     Begin
  24.         Key := InputArray[I];
  25.         J := I;
  26.         Write(OutputFile, #10#13);
  27.         While (J > 0) And (InputArray[J - 1] > Key) Do
  28.         Begin
  29.             TempValue :=  InputArray[J];
  30.             InputArray[J] := InputArray[J - 1];
  31.             InputArray[J - 1] := TempValue;
  32.  
  33.             For K := Low(InputArray) To High(InputArray) Do
  34.                 Write(OutputFile, InputArray[K], ' ');
  35.  
  36.             Writeln(OutputFile);
  37.             Dec(J);
  38.         End;
  39.         InputArray[J] := Key;
  40.     End;
  41.  
  42.     Writeln(#10#13, 'Массив отсортирован.');
  43.     CloseFile(OutputFile);
  44.     Sort := InputArray;
  45. End;
  46.  
  47. Procedure OutputOfSortedArrInConsole(Const SortedArr: TArr);
  48. Var
  49.     I: Integer;
  50. Begin
  51.     Writeln(#10#13, 'Отсортированный массив: ');
  52.     For I := 0 To High(SortedArr) Do
  53.         Write(SortedArr[I], ' ');
  54. End;
  55.  
  56. Procedure OutputOfArrayInFile(Const SortedArr: TArr; Path: String);
  57. Var
  58.     I: Integer;
  59.     OutputFile: TextFile;
  60.     IsCorrect: Boolean;
  61. Begin
  62.  
  63.     Repeat
  64.         IsCorrect := True;
  65.         AssignFile(OutputFile, Path);
  66.         Try
  67.             Rewrite(OutputFile);
  68.         Except
  69.             IsCorrect := False;
  70.             Write('Ошбика! Отказано в доступе! Измените параметры файла или укажите ссылку на новый.', #10#13);
  71.             Path := InputPathToFile();
  72.         End;
  73.     Until IsCorrect;
  74.    
  75.     Write(OutputFile, 'Отсортированный массив: ');
  76.    
  77.     For I := Low(SortedArr) To High(SortedArr) Do
  78.     Begin
  79.       Write(OutputFile, SortedArr[I], ' ');
  80.     End;
  81.  
  82.     CloseFile(OutputFile);
  83.     Writeln(#10#13, 'Данные успешно записаны в файл!');
  84. End;
  85.  
  86. Procedure Main();
  87. Var
  88.     ChoiceForInput, ChoiceForOutput: Byte;
  89.     Size: Integer;
  90.     InputArr, SortedArr: TArr;
  91.     InputFile, OutputFile, NullPath: String;
  92. Begin
  93.     NullPath := '';
  94.     OutputOfTaskInfo();
  95.  
  96.     Write('Вы хотите создать массив вручную(1) или считать с файла(2) ? ');
  97.     ChoiceForInput := GetVerificationOfChoice();
  98.  
  99.     If ChoiceForInput = 1 Then
  100.     Begin
  101.         Size := ReadSizeFromConsole();
  102.         InputArr := InputArrayInConsole(Size);
  103.         OutputOfArrayInConsole(InputArr);
  104.     End;
  105.  
  106.     If ChoiceForInput = 2 Then
  107.     Begin
  108.         InputFile := InputPathToFile();
  109.         InputArr := ReadArrayFromFile(InputFile);
  110.     End;
  111.  
  112.     Write(#10#13, 'Вы хотите получить массив и действия в консоли(1) или в файле(2) ? ');
  113.     ChoiceForOutput := GetVerificationOfChoice();
  114.    
  115.     If ChoiceForOutput = 1 Then
  116.     Begin
  117.         SortedArr := Sort(InputArr, NullPath);
  118.         OutputOfSortedArrInConsole(SortedArr);
  119.     End;
  120.  
  121.     If ChoiceForOutput = 2 Then
  122.     Begin
  123.         OutputFile := InputPathToFile();
  124.         SortedArr := Sort(InputArr, OutputFile);
  125.         OutputOfArrayInFile(SortedArr, OutputFile);
  126.     End;
  127.  
  128. End;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement