Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.37 KB | None | 0 0
  1. program L3D;
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses
  5.    SysUtils;
  6.  
  7. type
  8.    T1DArray = array of Integer;
  9.    Option = (UserFile, Console);
  10.  
  11. procedure Print1DArray(Array1D: T1DArray; L, R: Integer);
  12. var
  13.    i: Integer;
  14. begin
  15.    Write(Output, '[ ');
  16.    for i := L to R do
  17.       Write(Output, Array1D[i], ' ');
  18.    Writeln(Output, ']');
  19. end;
  20.  
  21. procedure Merge(InArray: T1DArray; const L, M, R: Integer);
  22. var
  23.    i, j, k: Integer;
  24.    Temp: T1DArray;
  25. begin
  26.    SetLength(Temp, R - L + 1);
  27.    i := L;
  28.    j := M + 1;
  29.    k := 0;
  30.    while (i <= M) and (j <= R) do
  31.    begin
  32.       if (InArray[i] < InArray[j]) then
  33.       begin
  34.          Temp[k] := InArray[i];
  35.          Inc(k);
  36.          Inc(i);
  37.       end
  38.       else
  39.       begin
  40.          Temp[k] := InArray[j];
  41.          Inc(k);
  42.          Inc(j);
  43.       end;
  44.    end;
  45.  
  46.    while (j <= R) do
  47.    begin
  48.       Temp[k] := InArray[j];
  49.       Inc(k);
  50.       Inc(j);
  51.    end;
  52.  
  53.    while (i <= M) do
  54.    begin
  55.       Temp[k] := InArray[i];
  56.       Inc(k);
  57.       Inc(i);
  58.    end;
  59.  
  60.    k := 0;
  61.    while (k < (R - L + 1)) do
  62.    begin
  63.       InArray[L + k] := Temp[k];
  64.       Inc(k);
  65.    end;
  66. end;
  67.  
  68. procedure NaturalMergeSortWithDemo(A: T1DArray; L, R: Integer;
  69.    LastView: String);
  70. var
  71.    M: Integer;
  72.    NewView: String;
  73. begin
  74.    if L <= R then
  75.    begin
  76.       NewView := LastView + '[ ';
  77.       M := L;
  78.       repeat
  79.          NewView := NewView + IntToStr(A[M]) + ' ';
  80.          Inc(M);
  81.       until (M > R) or (A[M] <= A[M - 1]);
  82.       NewView := NewView + ']';
  83.       Dec(M);
  84.       Write(Output, NewView);
  85.       Print1DArray(A, M + 1, R);
  86.       NaturalMergeSortWithDemo(A, M + 1, R, NewView);
  87.       Merge(A, L, M, R);
  88.       Write(Output, LastView);
  89.       Print1DArray(A, L, R);
  90.    end;
  91. end;
  92.  
  93. function ConvertNumOption(NumOption: Char): Option;
  94. begin
  95.    case NumOption of
  96.       '1':
  97.          ConvertNumOption := UserFile;
  98.       '2':
  99.          ConvertNumOption := Console;
  100.    end;
  101. end;
  102.  
  103. procedure CloseFileAfterWrite;
  104. begin
  105.    CloseFile(Output);
  106.    AssignFile(Output, '');
  107.    Rewrite(Output);
  108.    Writeln('Done.');
  109. end;
  110.  
  111. procedure OpenFileToWrite;
  112. var
  113.    Option: Char;
  114.    StopReadFileName, StopReadOption: Boolean;
  115.    FileName: String;
  116. begin
  117.    StopReadFileName := True;
  118.    repeat
  119.       Writeln('Enter file name: ');
  120.       Readln(FileName);
  121.       try
  122.          if FileExists(FileName) then
  123.          begin
  124.             Writeln('Do you want to clear file? [Y/N]: ');
  125.             AssignFile(Output, FileName);
  126.             StopReadOption := False;
  127.             repeat
  128.                Readln(Option);
  129.                if (UpperCase(Option) = 'Y') then
  130.                begin
  131.                   StopReadOption := True;
  132.                   Rewrite(Output);
  133.                end
  134.                else if UpperCase(Option) = 'N' then
  135.                begin
  136.                   StopReadOption := True;
  137.                   Append(Output);
  138.                end;
  139.             until StopReadOption;
  140.          end
  141.          else
  142.          begin
  143.             AssignFile(Output, FileName);
  144.             Rewrite(Output);
  145.          end;
  146.       except
  147.          StopReadFileName := False;
  148.          AssignFile(Output, '');
  149.          Rewrite(Output);
  150.          Writeln('Can not open the file...');
  151.       end;
  152.    until StopReadFileName;
  153. end;
  154.  
  155. procedure WriteResultOfMergeSort(Matrix1D: T1DArray);
  156. var
  157.    NumOption: Char;
  158.    StopReadOption: Boolean;
  159.    WriteTo: Option;
  160. begin
  161.    Writeln('[1] Write data to file');
  162.    Writeln('[2] Write data to console');
  163.    StopReadOption := False;
  164.    repeat
  165.       Readln(NumOption);
  166.       WriteTo := ConvertNumOption(NumOption);
  167.       case WriteTo of
  168.          UserFile:
  169.             begin
  170.                StopReadOption := True;
  171.                OpenFileToWrite;
  172.                Print1DArray(Matrix1D, 0, Length(Matrix1D) - 1);
  173.                NaturalMergeSortWithDemo(Matrix1D, 0, Length(Matrix1D) - 1, '');
  174.                CloseFileAfterWrite;
  175.             end;
  176.          Console:
  177.             begin
  178.                StopReadOption := True;
  179.                Print1DArray(Matrix1D, 0, Length(Matrix1D) - 1);
  180.                NaturalMergeSortWithDemo(Matrix1D, 0, Length(Matrix1D) - 1, '');
  181.             end;
  182.       end;
  183.    until StopReadOption;
  184. end;
  185.  
  186. function GetPositiveInt(var N: Integer): Boolean;
  187. begin
  188.    GetPositiveInt := True;
  189.    try
  190.       Readln(Input, N);
  191.    except
  192.       Writeln('N is not a number');
  193.       GetPositiveInt := False;
  194.    end;
  195.    if Result and (N <= 0) then
  196.    begin
  197.       Writeln('N must be positive...');
  198.       GetPositiveInt := False;
  199.    end;
  200. end;
  201.  
  202. function ReadMatrix1D(var Matrix1D: T1DArray; SourceType: Option): Boolean;
  203. var
  204.    StopReadN, IsNumber, ExitFunction: Boolean;
  205.    i, N: Integer;
  206. begin
  207.    ExitFunction := False;
  208.    StopReadN := False;
  209.    repeat
  210.       Writeln(Output, 'Enter matrix dimension: ');
  211.       if not GetPositiveInt(N) then
  212.       begin
  213.          if SourceType = UserFile then
  214.             ExitFunction := True;
  215.       end
  216.       else
  217.          StopReadN := True;
  218.    until StopReadN or ExitFunction;
  219.    if not ExitFunction then
  220.    begin
  221.       Writeln(Output, 'Done.');
  222.       SetLength(Matrix1D, N);
  223.       Writeln('Enter matrices: ');
  224.       for i := 0 to N - 1 do
  225.          if not ExitFunction then
  226.             repeat
  227.                IsNumber := True;
  228.                try
  229.                   Read(Input, Matrix1D[i]);
  230.                except
  231.                   IsNumber := False;
  232.                   if SourceType = UserFile then
  233.                      ExitFunction := True;
  234.                   Writeln('Not a number...');
  235.                end;
  236.             until IsNumber or ExitFunction;
  237.             if not ExitFunction then
  238.                Writeln(Output, 'Done.')
  239.             else
  240.                Writeln(Output, 'File contains wrong data...');
  241.    end;
  242.    ReadMatrix1D := not ExitFunction;
  243. end;
  244.  
  245. procedure CloseFileAfterRead;
  246. begin
  247.    AssignFile(Input, '');
  248.    Reset(Input);
  249. end;
  250.  
  251. procedure OpenFileToRead;
  252. var
  253.    StopReadFileName: Boolean;
  254.    FileName: String;
  255. begin
  256.    StopReadFileName := False;
  257.    repeat
  258.       Writeln('Enter file name: ');
  259.       Readln(FileName);
  260.       if FileExists(FileName) then
  261.       begin
  262.          StopReadFileName := True;
  263.          AssignFile(Input, FileName);
  264.          try
  265.             Reset(Input);
  266.          except
  267.  
  268.             begin
  269.                StopReadFileName := False;
  270.                Writeln('Can not open the file...');
  271.                CloseFileAfterRead;
  272.             end;
  273.          end;
  274.       end
  275.       else
  276.          Writeln('No such file...');
  277.    until StopReadFileName;
  278. end;
  279.  
  280. function ReadUserData(var Matrix3D: T1DArray): Boolean;
  281. var
  282.    ReadInputThen, StopRead: Boolean;
  283.    NumOption: Char;
  284.    ReadFrom: Option;
  285. begin
  286.    ReadInputThen := True;
  287.    Writeln('[1] Read data from file');
  288.    Writeln('[2] Read data from console');
  289.    Writeln('[~] Close');
  290.    Readln(NumOption);
  291.    ReadFrom := ConvertNumOption(NumOption);
  292.    case ReadFrom of
  293.       UserFile:
  294.          repeat
  295.             OpenFileToRead;
  296.             StopRead := ReadMatrix1D(Matrix3D, UserFile);
  297.             CloseFileAfterRead;
  298.          until StopRead;
  299.       Console :
  300.          ReadMatrix1D(Matrix3D, Console);
  301.    else
  302.       ReadInputThen := False;
  303.    end;
  304.    ReadUserData := ReadInputThen;
  305. end;
  306.  
  307. procedure Main;
  308.  
  309. var
  310.    Array1D: T1DArray;
  311.  
  312. begin
  313.    while ReadUserData(Array1D) do
  314.    begin
  315.       WriteResultOfMergeSort(Array1D);
  316.    end;
  317. end;
  318.  
  319. begin
  320.    Main;
  321.  
  322. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement