Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program laba3_3;
- uses
- System.SysUtils;
- type
- DynamicArray = Array of Double;
- function InputLengthFromConsole(): Integer; Forward;
- function InputLengthFromFile(Path: String): Integer; Forward;
- function InputPath(): String; Forward;
- procedure InputArrayFromConsole(var Arr: Array of Double); Forward;
- procedure InputArrayFromFile(var Arr: Array of Double; Path: String); Forward;
- procedure ViewIterations(var Arr: Array of Double; I: Integer; Current: Double); Forward;
- function ChooseSource(): Integer;
- var
- Choice: Integer;
- IsCorrect: Boolean;
- begin
- Writeln('Choose where to enter data. Enter 1 or 2:');
- Writeln('1.File');
- Writeln('2.Console');
- repeat
- IsCorrect := True;
- try
- Readln(Choice);
- except
- Writeln('Enter an integer!');
- IsCorrect := False;
- end;
- if (IsCorrect) and (Choice <> 1) and (Choice <> 2) then
- begin
- Writeln('Enter 1 or 2!');
- IsCorrect := False;
- end;
- until IsCorrect;
- ChooseSource := Choice;
- end;
- function InputData(Source: Integer): DynamicArray;
- var
- Length: Integer;
- PathInput: String;
- Arr: DynamicArray;
- begin
- Case Source of
- 1:
- begin
- Writeln('Enter the absolute link to the input file');
- PathInput := InputPath();
- Length := InputLengthFromFile(PathInput);
- SetLength(Arr, Length);
- InputArrayFromFile(Arr, PathInput);
- end;
- 2:
- begin
- Length := InputLengthFromConsole();
- SetLength(Arr, Length);
- InputArrayFromConsole(Arr);
- end;
- end;
- InputData := Arr;
- end;
- function InputPath(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- Readln(Path);
- if not FileExists(Path) then
- begin
- IsCorrect := False;
- Writeln('File not found! Enter the absolute link to the file');
- end;
- until IsCorrect;
- InputPath := Path;
- end;
- function InputLengthFromFile(Path: String): Integer;
- var
- Length: Integer;
- Flag: Boolean;
- InputFile: TextFile;
- begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Flag := True;
- Writeln('The length of the array is read from file...');
- try
- Readln(InputFile, Length);
- except
- Writeln('The file has incorrect length! Enter the length from console');
- Flag := False;
- Length := InputLengthFromConsole();
- end;
- if (Flag) and ((Length < 1) or (Length > 20)) then
- begin
- Length := InputLengthFromConsole();
- end;
- CloseFile(InputFile);
- InputLengthFromFile := Length;
- end;
- procedure InputArrayFromFile(var Arr: Array of Double; Path: String);
- var
- I: Integer;
- IsCorrect: Boolean;
- InputFile: TextFile;
- begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Readln(InputFile);
- Writeln('The array is read from file...');
- try
- for I := 0 to High(Arr) do
- begin
- Read(InputFile, Arr[I]);
- end;
- except
- Writeln('The file has incorrect data! Enter the array from console');
- InputArrayFromConsole(Arr);
- end;
- CloseFile(InputFile);
- end;
- function InputLengthFromConsole(): Integer;
- var
- Length: Integer;
- IsCorrect: Boolean;
- begin
- Writeln('Enter the length of the array (from 1 to 20)');
- repeat
- IsCorrect := True;
- try
- Readln(Length);
- except
- Writeln('Enter an integer!');
- IsCorrect := False;
- end;
- if (IsCorrect) and ((Length < 1) or (Length > 20)) then
- begin
- Writeln('Incorrect input! Enter the length of the range from 1 to 20');
- IsCorrect := False;
- end;
- until IsCorrect;
- InputLengthFromConsole := Length;
- end;
- procedure InputArrayFromConsole(var Arr: Array of Double);
- var
- IsCorrect: Boolean;
- I: Integer;
- begin
- for I := 0 to High(Arr) do
- begin
- repeat
- IsCorrect := True;
- Writeln('Enter the array element at number ', i + 1);
- try
- Readln(Arr[I]);
- except
- Writeln('Incorrect input! Enter the number');
- IsCorrect := False;
- end;
- until IsCorrect;
- end;
- end;
- procedure PrintArray(Arr: Array of Double);
- var
- I: Integer;
- begin
- for I := 0 to High(Arr) do
- Write(FormatFloat('0.####',Arr[I]) , '; ');
- Writeln;
- Writeln;
- end;
- procedure DoInsertionSort(var Arr: Array of Double);
- var
- I, J: Integer;
- Current: Double;
- begin
- for I := 0 to High(Arr) do
- begin
- Current := Arr[I];
- J := I;
- while (J > 0) and (Current < Arr[J - 1]) do
- begin
- Arr[J] := Arr[J - 1];
- Dec(J);
- end;
- Arr[J] := Current;
- ViewIterations(Arr, I, Current);
- end;
- end;
- procedure ViewIterations(var Arr: Array of Double; I: Integer; Current: Double);
- begin
- Writeln('Iteration ', I + 1,':');
- Writeln('Inserted element = ', FormatFloat('0.####',Current));
- Writeln('Array:');
- PrintArray(Arr);
- end;
- procedure OutputToFile(var Arr: Array of Double; Path: String);
- var
- OutputFile: TextFile;
- I: Integer;
- begin
- AssignFile(OutputFile, Path);
- Reset(OutputFile);
- Rewrite(OutputFile);
- for I := 0 to High(Arr) do
- begin
- Write(OutputFile, FormatFloat('0.####',Arr[I]), '; ');
- end;
- CloseFile(OutputFile);
- Writeln('The data is successfully recorded in the file');
- end;
- procedure OutputData(Source: Integer; Arr: Array of Double);
- var
- PathOutput: String;
- begin
- if Source = 1 then
- begin
- Writeln;
- Writeln('Enter the absolute link to the output file');
- PathOutput := InputPath();
- OutputToFile(Arr, PathOutput);
- end;
- end;
- procedure Main();
- var
- Length, Source: Integer;
- Arr: DynamicArray;
- begin
- Writeln('The program sorts the array using the simple insertion method.');
- Source := ChooseSource();
- Arr := InputData(Source);
- Writeln('Source array:');
- PrintArray(Arr);
- DoInsertionSort(Arr);
- Writeln('Final array:');
- PrintArray(Arr);
- OutputData(Source, Arr);
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement