Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Laba3;
- Uses
- System.SysUtils;
- Type
- TArray = Array of Integer;
- Function InputData() : Integer;
- Var
- IsCorrect : Boolean;
- N : Integer;
- Begin
- N := 0;
- Repeat
- IsCorrect := True;
- Try
- Readln(N)
- Except
- Writeln('Please, enter a integer number:');
- IsCorrect := False;
- End;
- Until(IsCorrect);
- InputData := N;
- End;
- Function Choose() : Boolean;
- Var
- InputNumber : Integer;
- IsCorrect : Boolean;
- Const
- MIN_NUM = 0;
- MAX_NUM = 1;
- Begin
- InputNumber := 0;
- Repeat
- IsCorrect := True;
- InputNumber := InputData();
- If ((InputNumber < MIN_NUM) or (InputNumber > MAX_NUM)) then
- Begin
- Writeln('You are out of input range!');
- IsCorrect := False;
- End;
- Until IsCorrect;
- If (InputNumber = 1) then
- Choose := True
- Else
- Choose := False;
- End;
- Function InputArraySize () : Integer;
- Var
- Num : Integer;
- IsCorrect : Boolean;
- Const
- MIN_SIZE = 2;
- Begin
- Repeat
- IsCorrect := True;
- Num := InputData();
- If (Num < MIN_SIZE) Then
- Begin
- Writeln('Please, enter a number > 2:');
- IsCorrect := False
- End;
- Until IsCorrect;
- InputArraySize := Num;
- End;
- Function InputArray (N : Integer) : TArray;
- Var
- I: Integer;
- Arr : TArray;
- Begin
- SetLength (Arr, N);
- For I := 0 to High(Arr) do
- Arr[I] := InputData();
- InputArray := Arr;
- End;
- Function SortArrayBySelectionSort (Arr : TArray): TArray;
- Var
- I, J, Temp, Min : Integer;
- Begin
- Writeln('Sort stages:');
- For I := 0 To High(Arr) - 1 do
- Begin
- For J := I + 1 To High(Arr) do
- If (Arr[J] < Arr[I]) then
- Begin
- Min := Arr[J];
- Arr[J] := Arr[I];
- Arr[I] := Min;
- End;
- Write('Stage ', I + 1, ': ');
- For Temp := 0 To High(Arr) Do
- Write(Arr[Temp], ' ');
- Writeln;
- End;
- SortArrayBySelectionSort := Arr;
- End;
- Procedure OutputArray (Arr : TArray);
- Var
- I : Integer;
- Begin
- For I := 0 to High(Arr) do
- Write(Arr[I], ' ');
- Writeln;
- End;
- Function InputFilePath() : String;
- Var
- IsCorrect : Boolean;
- Path : String;
- Begin
- Writeln('Input path to file: ');
- Repeat
- IsCorrect := True;
- Readln(Path);
- If(Not FileExists(Path)) Then
- Begin
- IsCorrect := False;
- Writeln('Wrong way to file. Input correct path.');
- End
- Else If (ExtractFileExt(Path) <> '.txt') Then
- Begin
- Writeln('Must have .txt');
- IsCorrect := False;
- End;
- Until IsCorrect;
- InputFilePath := Path;
- End;
- Function InputSizeOfArrayFromFile(Path : String) : Integer;
- Var
- Num : Integer;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Const
- MIN = 2;
- Begin
- Num := 0;
- AssignFile(InputFile, Path);
- Reset(InputFile);
- IsCorrect := True;
- Try
- Readln(InputFile, Num);
- Except
- IsCorrect := False;
- Writeln('Mistake of reading size of array from file.');
- End;
- If (IsCorrect And (Num < MIN)) Then
- Writeln('Mistake of array elements.');
- CloseFile(InputFile);
- InputSizeOfArrayFromFile := Num;
- End;
- Function InputArrayFile(Path : String; Num : Integer) : TArray;
- Var
- I: Integer;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Arr : TArray;
- Begin
- SetLength(Arr, Num);
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Readln(InputFile);
- Repeat
- IsCorrect := True;
- For I := 0 To High(Arr) Do
- Begin
- Try
- Read(InputFile, Arr[I]);
- Except
- IsCorrect := False;
- Writeln('Mistake of reading from file.')
- End;
- End;
- Until IsCorrect;
- Close(InputFile);
- InputArrayFile := Arr;
- End;
- Procedure FileOutput(Path : String; Arr : TArray);
- Var
- IsCorrect : Boolean;
- OutputFile : TextFile;
- I: Integer;
- Begin
- AssignFile(OutputFile, Path);
- Repeat
- IsCorrect := True;
- Try
- Rewrite(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Mistake with writing in file. Input another path.');
- Path := InputFilePath();
- End;
- Until IsCorrect;
- Writeln(OutputFile, 'Sorted array:');
- For I := 0 To High(Arr) Do
- Write(OutputFile, Arr[I], ' ');
- CloseFile(OutputFile);
- Write('Success!');
- End;
- Function Input () : TArray;
- Var
- Arr : TArray;
- Num : Integer;
- Path : String;
- Chose : Boolean;
- Begin
- Writeln('Selection sort. Demonstration.');
- Writeln('Enter type of input: ');
- Writeln('1 is console input, 0 is file input.');
- Chose := Choose();
- If Chose then
- Begin
- Writeln('Input size of array:');
- Num := InputArraySize();
- Writeln('Input array elements:');
- Arr := InputArray(Num);
- Writeln ('Unsorted array:');
- OutputArray(Arr);
- End
- Else
- Begin
- Path := InputFilePath();
- Num := InputSizeOfArrayFromFile(Path);
- Arr := InputArrayFile(Path, Num);
- End;
- Input := Arr;
- End;
- Procedure Output (Arr : TArray);
- Var
- Path : String;
- Chose : Boolean;
- Begin
- Writeln('Enter type of output:');
- Writeln('1 is console output, 0 is file output.');
- Chose := Choose();
- If Chose then
- Begin
- Writeln ('Sorted array:');
- OutputArray(Arr);
- End
- Else
- Begin
- Path := InputFilePath();
- FileOutput(Path, Arr);
- End;
- Readln;
- End;
- Var
- Arr : TArray;
- Begin
- Arr := Input();
- Arr := SortArrayBySelectionSort(Arr);
- Output (Arr);
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement