Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Sort(Const InputArray: TArr; Path: String): TArr;
- Var
- I, J, K, Key, TempValue, Size: Integer;
- OutputFile: TextFile;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- AssignFile(OutputFile, Path);
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Write('Ошбика! Отказано в доступе! Измените параметры файла или укажите ссылку на новый.', #10#13);
- Path := InputPathToFile();
- End;
- Until IsCorrect;
- Writeln(#10#13, 'Сортировка...');
- For I := 1 To High(InputArray) Do
- Begin
- Key := InputArray[I];
- J := I;
- Write(OutputFile, #10#13);
- While (J > 0) And (InputArray[J - 1] > Key) Do
- Begin
- TempValue := InputArray[J];
- InputArray[J] := InputArray[J - 1];
- InputArray[J - 1] := TempValue;
- For K := Low(InputArray) To High(InputArray) Do
- Write(OutputFile, InputArray[K], ' ');
- Writeln(OutputFile);
- Dec(J);
- End;
- InputArray[J] := Key;
- End;
- Writeln(#10#13, 'Массив отсортирован.');
- CloseFile(OutputFile);
- Sort := InputArray;
- End;
- Procedure OutputOfSortedArrInConsole(Const SortedArr: TArr);
- Var
- I: Integer;
- Begin
- Writeln(#10#13, 'Отсортированный массив: ');
- For I := 0 To High(SortedArr) Do
- Write(SortedArr[I], ' ');
- End;
- Procedure OutputOfArrayInFile(Const SortedArr: TArr; Path: String);
- Var
- I: Integer;
- OutputFile: TextFile;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- AssignFile(OutputFile, Path);
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Write('Ошбика! Отказано в доступе! Измените параметры файла или укажите ссылку на новый.', #10#13);
- Path := InputPathToFile();
- End;
- Until IsCorrect;
- Write(OutputFile, 'Отсортированный массив: ');
- For I := Low(SortedArr) To High(SortedArr) Do
- Begin
- Write(OutputFile, SortedArr[I], ' ');
- End;
- CloseFile(OutputFile);
- Writeln(#10#13, 'Данные успешно записаны в файл!');
- End;
- Procedure Main();
- Var
- ChoiceForInput, ChoiceForOutput: Byte;
- Size: Integer;
- InputArr, SortedArr: TArr;
- InputFile, OutputFile, NullPath: String;
- Begin
- NullPath := '';
- OutputOfTaskInfo();
- Write('Вы хотите создать массив вручную(1) или считать с файла(2) ? ');
- ChoiceForInput := GetVerificationOfChoice();
- If ChoiceForInput = 1 Then
- Begin
- Size := ReadSizeFromConsole();
- InputArr := InputArrayInConsole(Size);
- OutputOfArrayInConsole(InputArr);
- End;
- If ChoiceForInput = 2 Then
- Begin
- InputFile := InputPathToFile();
- InputArr := ReadArrayFromFile(InputFile);
- End;
- Write(#10#13, 'Вы хотите получить массив и действия в консоли(1) или в файле(2) ? ');
- ChoiceForOutput := GetVerificationOfChoice();
- If ChoiceForOutput = 1 Then
- Begin
- SortedArr := Sort(InputArr, NullPath);
- OutputOfSortedArrInConsole(SortedArr);
- End;
- If ChoiceForOutput = 2 Then
- Begin
- OutputFile := InputPathToFile();
- SortedArr := Sort(InputArr, OutputFile);
- OutputOfArrayInFile(SortedArr, OutputFile);
- End;
- End;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement