Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program L3D;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- T1DArray = array of Integer;
- Option = (UserFile, Console);
- procedure Print1DArray(Array1D: T1DArray; L, R: Integer);
- var
- i: Integer;
- begin
- Write(Output, '[ ');
- for i := L to R do
- Write(Output, Array1D[i], ' ');
- Writeln(Output, ']');
- end;
- procedure Merge(InArray: T1DArray; const L, M, R: Integer);
- var
- i, j, k: Integer;
- Temp: T1DArray;
- begin
- SetLength(Temp, R - L + 1);
- i := L;
- j := M + 1;
- k := 0;
- while (i <= M) and (j <= R) do
- begin
- if (InArray[i] < InArray[j]) then
- begin
- Temp[k] := InArray[i];
- Inc(k);
- Inc(i);
- end
- else
- begin
- Temp[k] := InArray[j];
- Inc(k);
- Inc(j);
- end;
- end;
- while (j <= R) do
- begin
- Temp[k] := InArray[j];
- Inc(k);
- Inc(j);
- end;
- while (i <= M) do
- begin
- Temp[k] := InArray[i];
- Inc(k);
- Inc(i);
- end;
- k := 0;
- while (k < (R - L + 1)) do
- begin
- InArray[L + k] := Temp[k];
- Inc(k);
- end;
- end;
- procedure NaturalMergeSortWithDemo(A: T1DArray; L, R: Integer;
- LastView: String);
- var
- M: Integer;
- NewView: String;
- begin
- if L <= R then
- begin
- NewView := LastView + '[ ';
- M := L;
- repeat
- NewView := NewView + IntToStr(A[M]) + ' ';
- Inc(M);
- until (M > R) or (A[M] <= A[M - 1]);
- NewView := NewView + ']';
- Dec(M);
- Write(Output, NewView);
- Print1DArray(A, M + 1, R);
- NaturalMergeSortWithDemo(A, M + 1, R, NewView);
- Merge(A, L, M, R);
- Write(Output, LastView);
- Print1DArray(A, L, R);
- end;
- end;
- function ConvertNumOption(NumOption: Char): Option;
- begin
- case NumOption of
- '1':
- ConvertNumOption := UserFile;
- '2':
- ConvertNumOption := Console;
- end;
- end;
- procedure CloseFileAfterWrite;
- begin
- CloseFile(Output);
- AssignFile(Output, '');
- Rewrite(Output);
- Writeln('Done.');
- end;
- procedure OpenFileToWrite;
- var
- Option: Char;
- StopReadFileName, StopReadOption: Boolean;
- FileName: String;
- begin
- StopReadFileName := True;
- repeat
- Writeln('Enter file name: ');
- Readln(FileName);
- try
- if FileExists(FileName) then
- begin
- Writeln('Do you want to clear file? [Y/N]: ');
- AssignFile(Output, FileName);
- StopReadOption := False;
- repeat
- Readln(Option);
- if (UpperCase(Option) = 'Y') then
- begin
- StopReadOption := True;
- Rewrite(Output);
- end
- else if UpperCase(Option) = 'N' then
- begin
- StopReadOption := True;
- Append(Output);
- end;
- until StopReadOption;
- end
- else
- begin
- AssignFile(Output, FileName);
- Rewrite(Output);
- end;
- except
- StopReadFileName := False;
- AssignFile(Output, '');
- Rewrite(Output);
- Writeln('Can not open the file...');
- end;
- until StopReadFileName;
- end;
- procedure WriteResultOfMergeSort(Matrix1D: T1DArray);
- var
- NumOption: Char;
- StopReadOption: Boolean;
- WriteTo: Option;
- begin
- Writeln('[1] Write data to file');
- Writeln('[2] Write data to console');
- StopReadOption := False;
- repeat
- Readln(NumOption);
- WriteTo := ConvertNumOption(NumOption);
- case WriteTo of
- UserFile:
- begin
- StopReadOption := True;
- OpenFileToWrite;
- Print1DArray(Matrix1D, 0, Length(Matrix1D) - 1);
- NaturalMergeSortWithDemo(Matrix1D, 0, Length(Matrix1D) - 1, '');
- CloseFileAfterWrite;
- end;
- Console:
- begin
- StopReadOption := True;
- Print1DArray(Matrix1D, 0, Length(Matrix1D) - 1);
- NaturalMergeSortWithDemo(Matrix1D, 0, Length(Matrix1D) - 1, '');
- end;
- end;
- until StopReadOption;
- end;
- function GetPositiveInt(var N: Integer): Boolean;
- begin
- GetPositiveInt := True;
- try
- Readln(Input, N);
- except
- Writeln('N is not a number');
- GetPositiveInt := False;
- end;
- if Result and (N <= 0) then
- begin
- Writeln('N must be positive...');
- GetPositiveInt := False;
- end;
- end;
- function ReadMatrix1D(var Matrix1D: T1DArray; SourceType: Option): Boolean;
- var
- StopReadN, IsNumber, ExitFunction: Boolean;
- i, N: Integer;
- begin
- ExitFunction := False;
- StopReadN := False;
- repeat
- Writeln(Output, 'Enter matrix dimension: ');
- if not GetPositiveInt(N) then
- begin
- if SourceType = UserFile then
- ExitFunction := True;
- end
- else
- StopReadN := True;
- until StopReadN or ExitFunction;
- if not ExitFunction then
- begin
- Writeln(Output, 'Done.');
- SetLength(Matrix1D, N);
- Writeln('Enter matrices: ');
- for i := 0 to N - 1 do
- if not ExitFunction then
- repeat
- IsNumber := True;
- try
- Read(Input, Matrix1D[i]);
- except
- IsNumber := False;
- if SourceType = UserFile then
- ExitFunction := True;
- Writeln('Not a number...');
- end;
- until IsNumber or ExitFunction;
- if not ExitFunction then
- Writeln(Output, 'Done.')
- else
- Writeln(Output, 'File contains wrong data...');
- end;
- ReadMatrix1D := not ExitFunction;
- end;
- procedure CloseFileAfterRead;
- begin
- AssignFile(Input, '');
- Reset(Input);
- end;
- procedure OpenFileToRead;
- var
- StopReadFileName: Boolean;
- FileName: String;
- begin
- StopReadFileName := False;
- repeat
- Writeln('Enter file name: ');
- Readln(FileName);
- if FileExists(FileName) then
- begin
- StopReadFileName := True;
- AssignFile(Input, FileName);
- try
- Reset(Input);
- except
- begin
- StopReadFileName := False;
- Writeln('Can not open the file...');
- CloseFileAfterRead;
- end;
- end;
- end
- else
- Writeln('No such file...');
- until StopReadFileName;
- end;
- function ReadUserData(var Matrix3D: T1DArray): Boolean;
- var
- ReadInputThen, StopRead: Boolean;
- NumOption: Char;
- ReadFrom: Option;
- begin
- ReadInputThen := True;
- Writeln('[1] Read data from file');
- Writeln('[2] Read data from console');
- Writeln('[~] Close');
- Readln(NumOption);
- ReadFrom := ConvertNumOption(NumOption);
- case ReadFrom of
- UserFile:
- repeat
- OpenFileToRead;
- StopRead := ReadMatrix1D(Matrix3D, UserFile);
- CloseFileAfterRead;
- until StopRead;
- Console :
- ReadMatrix1D(Matrix3D, Console);
- else
- ReadInputThen := False;
- end;
- ReadUserData := ReadInputThen;
- end;
- procedure Main;
- var
- Array1D: T1DArray;
- begin
- while ReadUserData(Array1D) do
- begin
- WriteResultOfMergeSort(Array1D);
- end;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement