Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Laba_3_3;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- TList = array of Integer;
- TInputMode = (WithFile, Console);
- resourcestring
- InstructionMessage
- = 'This program sorts your sequence using Insertion Sort';
- WayOfInputMessage = 'Where do you want to input data from? [F]ile or [C]onsole';
- IncorrectInputFilePathMessage
- = 'Incorrect input file path, check if file exists and try again';
- IncorrectInputModeMessage = 'Incorrect way of input, choose [F]ile or [C]onsole';
- OutputFileExistsErrorMessage
- = 'Incorrect output file, such file already exists, try again';
- ShouldCreateMessage = 'Do you want to save output data into file? [Y]es or [N]o';
- ShouldCreateErrorMessage = 'Incorrect answer, choose [Y]es or [N]o';
- EmptySequenceMessage = 'Your sequence doesn''t contain any of assumed symbols';
- OutputMessage = 'Composed set based on your sequence';
- SuccessfullySavedMessage = 'Output data was successfully saved into ';
- function GetInputFilePath: string;
- var
- Path: string;
- begin
- Writeln('Enter input file path');
- Readln(Path);
- while not FileExists(Path) do
- begin
- Writeln(IncorrectInputFilePathMessage);
- Readln(Path);
- end;
- GetInputFilePath := Path;
- end;
- function ShouldCreateOutputFile: Boolean;
- var
- Answer: string;
- begin
- Writeln(ShouldCreateMessage);
- Readln(Answer);
- Answer := LowerCase(Answer);
- while (Answer <> 'y') and (Answer <> 'n') do
- begin
- Writeln(ShouldCreateErrorMessage);
- Readln(Answer);
- end;
- ShouldCreateOutputFile := Answer = 'y';
- end;
- function GetOutputFilePath: string;
- var
- Path: string;
- begin
- Writeln('Enter output file path');
- Readln(Path);
- while FileExists(Path) do
- begin
- Writeln(OutputFileExistsErrorMessage);
- Readln(Path);
- end;
- GetOutputFilePath := Path;
- end;
- function ChooseInputMode: TInputMode;
- var
- Mode: string;
- begin
- Writeln(WayOfInputMessage);
- Readln(Mode);
- Mode := LowerCase(Mode);
- while (Mode <> 'c') and (Mode <> 'f') do
- begin
- Writeln(IncorrectInputModeMessage);
- Readln(Mode);
- end;
- if Mode = 'f' then
- ChooseInputMode := WithFile
- else
- ChooseInputMode := Console;
- end;
- function ReadFile(var InputFile: TextFile; const Len: Integer): TList;
- var
- Sequence: TList;
- i: Integer;
- begin
- SetLength(Sequence, Len);
- Reset(InputFile);
- i := 0;
- while not EoF(InputFile) do
- begin
- Read(InputFile, Sequence[i]);
- Inc(i);
- end;
- CloseFile(InputFile);
- ReadFile := Sequence;
- end;
- function CheckData(var InputFile: TextFile; var Len: Integer): Boolean;
- var
- AssumedLength, Item: Integer;
- begin
- AssumedLength := 0;
- try
- while not EoF(InputFile) do
- begin
- Read(InputFile, Item);
- Inc(AssumedLength);
- end;
- CheckData := True;
- Len := AssumedLength;
- except
- Writeln('Data error');
- Len := - 1;
- CheckData := False;
- end;
- end;
- function ReadConsole: TList;
- var
- N, i: Integer;
- IsCorrect: Boolean;
- List: TList;
- begin
- IsCorrect := False;
- Writeln('Enter amount of elements of your sequence');
- repeat
- try
- Readln(N);
- IsCorrect := True;
- except
- Writeln('Try again');
- end;
- until IsCorrect;
- SetLength(List, N);
- Writeln('Enter elems');
- for i := 0 to N - 1 do begin
- IsCorrect := False;
- repeat
- try
- Readln(List[i]);
- IsCorrect := True;
- except
- Writeln('Try again man');
- end;
- until IsCorrect;
- end;
- ReadConsole := List;
- end;
- procedure WriteList(var OutputFile: TextFile; List: TList);
- var
- i, LastIndex: Integer;
- begin
- LastIndex := High(List);
- for i := 0 to LastIndex do
- Write(OutputFile, List[i], ' ');
- Writeln(OutputFile, '');
- end;
- procedure SortList(var OutputFile: TextFile; List: TList);
- var
- i, j, LastIndex, Current: Integer;
- begin
- LastIndex := High(List) + 1;
- Writeln(OutputFile, 'Sorted seqeunce');
- for i := 1 to LastIndex do
- begin
- WriteList(OutputFile, List);
- Current := List[i];
- j := i - 1;
- while (List[j] > Current) and (j >= 0) do
- begin
- List[j+1] := List[j];
- Dec(j);
- end;
- List[j+1] := Current;
- end;
- end;
- procedure Main;
- var
- Len: Integer;
- List, ListToFile: TList;
- InputFile, OutputFile: TextFile;
- begin
- Writeln(InstructionMessage);
- Len := 0;
- case ChooseInputMode() of
- WithFile:
- begin
- Assign(InputFile, {'input.txt'}GetInputFilePath());
- Reset(InputFile);
- if CheckData(InputFile, Len) then
- List := ReadFile(InputFile, Len);
- end;
- Console:
- List := ReadConsole();
- end;
- if Len <> -1 then
- begin
- ListToFile := Copy(List);
- SortList(Output, List);
- if ShouldCreateOutputFile() then
- begin
- AssignFile(OutputFile, GetOutputFilePath());
- Rewrite(OutputFile);
- SortList(OutputFile, ListToFile);
- CloseFile(OutputFile);
- end;
- end;
- Writeln('Fin!');
- Readln;
- end;
- begin
- Main();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement