Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.97 KB | None | 0 0
  1. program LabWork31;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. type
  9.    TIntArr = array of Integer;
  10.  
  11. function UserChooce(): Boolean;
  12. var
  13.    IsCorrectChoice: Boolean;
  14.    Choice: Char;
  15. begin
  16.    IsCorrectChoice := False;
  17.    repeat
  18.       Write('Y/N: ');
  19.       Readln(Choice);
  20.       Choice := UpCase(Choice);
  21.       case Choice of
  22.          'Y':
  23.             begin
  24.                IsCorrectChoice := True;
  25.                UserChooce := True;
  26.             end;
  27.          'N':
  28.             begin
  29.                IsCorrectChoice := True;
  30.                UserChooce := False;
  31.             end
  32.       else
  33.          Writeln('You made an incorrect choice, please, try again clearly following the instructions.');
  34.       end;
  35.    until IsCorrectChoice;
  36. end;
  37.  
  38.  
  39. function CorrectFormatOfTheFile(FileName: String): String;
  40. const
  41.    FormatLen = 4;
  42. begin
  43.    if AnsiCompareStr(copy(FileName, length(FileName) - FormatLen + 1 , FormatLen), '.txt') <> 0 then
  44.       FileName := FileName + '.txt';
  45.    CorrectFormatOfTheFile := FileName;
  46. end;
  47.  
  48. function InputFileName(): String;
  49. var
  50.    FileName: String;
  51.    Input: TextFile;
  52.    IsCorrectName: Boolean;
  53. begin
  54.    IsCorrectName := False;
  55.    repeat
  56.       Writeln('Please, enter name of the file or way to the file.');
  57.       Writeln('Example: F:\Programming\Lab\3_1\Delphi\Input.txt');
  58.       Readln(FileName);
  59.       FileName := CorrectFormatOfTheFile(FileName);
  60.       if FileExists(FileName) then
  61.       begin
  62.          AssignFile(Input, FileName);
  63.          Reset(Input);
  64.          if seekEOF(Input) then
  65.             Writeln('Your file is empty, please, try again')
  66.          else
  67.             IsCorrectName := True;
  68.          Close(Input);
  69.       end
  70.       else
  71.          Writeln('File with the same name does not exists, please, try again');
  72.    until IsCorrectName;
  73.    InputFileName := FileName;
  74. end;
  75.  
  76.  
  77.  
  78. function FindTheSizeOfTheArray(FileName: String): Integer;
  79. var
  80.    Input: TextFile;
  81.    SomeNumber, Size: Integer;
  82. begin
  83.    AssignFile(Input, FileName);
  84.    Reset(Input);
  85.    Size := 1;
  86.    try
  87.       while not seekEOF(Input) do
  88.       begin
  89.          Read(Input, SomeNumber);
  90.          Inc(Size);
  91.       end;
  92.    except
  93.       Writeln('Your have have no ingegers elements');
  94.       Size := -1;
  95.    end;
  96.    Close(Input);
  97.    FindTheSizeOfTheArray := Size;
  98. end;
  99.  
  100.  
  101. function EnterArrFromFile(): TIntArr;
  102. var
  103.    FileName: String;
  104.    Input: TextFile;
  105.    UserArr: TIntArr;
  106.    Index, SizeOfTheArr, HighIndex: Integer;
  107. begin
  108.    repeat
  109.       FileName := InputFileName();
  110.       SizeOfTheArr := FindTheSizeOfTheArray(FileName);
  111.    until (SizeOfTheArr <> -1);
  112.    SetLength(UserArr,  SizeOfTheArr);
  113.    HighIndex := High(UserArr);
  114.    AssignFile(Input, FileName);
  115.    Reset(Input);
  116.    Write('Your array: ');
  117.    for Index := 0 to HighIndex do
  118.    begin
  119.       Read(Input, UserArr[Index]);
  120.       Write(UserArr[Index], ' ');
  121.    end;
  122.    Writeln;
  123.    EnterArrFromFile := UserArr;
  124. end;
  125.  
  126.  
  127.  
  128. function EnterArrFromConsole(): TIntArr;
  129. const
  130.    HighInteger = 2147483647;
  131.    LowInteger = -2147483647;
  132. var
  133.    Index, SizeOfTheArr, HighIndex: Integer;
  134.    UserArr: TIntArr;
  135.    IsCorrectNumber: Boolean;
  136. begin
  137.    IsCorrectNumber := False;
  138.    repeat
  139.       Writeln('Please, enter size of your array 0 < YourSize < ', HighInteger);
  140.       try
  141.          Readln(SizeOfTheArr);
  142.          if SizeOfTheArr > 0 then
  143.             IsCorrectNumber := True
  144.          else
  145.             Writeln('Error! Size must be greater than zero, please, try again.');
  146.       except
  147.          Writeln('You entered an incorrect value, please, try again');
  148.       end;
  149.    until IsCorrectNumber;
  150.    SetLength(UserArr, SizeOfTheArr);
  151.    Writeln('Please, enter numbers of your array ', LowInteger,' < YourNumber < ', HighInteger);
  152.    HighIndex := High(UserArr);
  153.    for Index := 0 to HighIndex do
  154.       repeat
  155.          try
  156.             Readln(UserArr[Index]);
  157.             IsCorrectNumber := True;
  158.          except
  159.             Writeln('You entered an incorrect value, please, try again');
  160.             IsCorrectNumber := False;
  161.          end;
  162.       until IsCorrectNumber;
  163.    EnterArrFromConsole := UserArr;
  164. end;
  165.  
  166.  
  167.  
  168. //Èùåò èíäåêñ ïîäïîñëåäîâàòåëüíîñòè + 1
  169. function FindFinalSequenceIndex(const UserArray: TIntArr): Integer;
  170. var
  171.    HighIndex, Index, i: Integer;
  172.    IsEndOfTheSequence: Boolean;
  173. begin
  174.    Index := 0;
  175.    HighIndex := High(UserArray);
  176.    IsEndOfTheSequence := False;
  177.    while (Index < HighIndex) and (not IsEndOfTheSequence) do
  178.       if UserArray[Index] <= UserArray[Index + 1] then
  179.          Inc(Index)
  180.       else
  181.          IsEndOfTheSequence := True;
  182.    for i := 0 to Index do
  183.       Write(UserArray[i], ' ');
  184.    Write(' |  ');
  185.    if Index = HighIndex then
  186.       FindFinalSequenceIndex := -1
  187.    else
  188.       FindFinalSequenceIndex := Index + 1;
  189. end;
  190.  
  191. //Íåïîñðåäñòâåííî ñîðòèðîâêà ñëèÿíèåì
  192. function MergeSort(UserArray: TIntArr): TIntArr;
  193. var
  194.    LeftIndex, RightIndex, SortArrayIndex, IndexOfTheSequence: Integer;
  195.    UserArrayLen, LeftPartLen, RightPartLen, i, Iteration: Integer;
  196.    LeftPart, RightPart: TIntArr;
  197. begin
  198.    UserArrayLen := Length(UserArray);
  199.    if UserArrayLen > 1 then
  200.    begin
  201.       IndexOfTheSequence := FindFinalSequenceIndex(UserArray);
  202.       if IndexOfTheSequence <> -1 then
  203.       begin
  204.          LeftPart := copy(UserArray, 0, IndexOfTheSequence);
  205.          RightPart := MergeSort(copy(UserArray, IndexOfTheSequence, UserArrayLen));
  206.          LeftPartLen := Length(LeftPart);
  207.          RightPartLen := Length(RightPart);
  208.          Iteration := LeftPartLen - 1;
  209.          Writeln;
  210.          Write('Left part: ');
  211.          for i := 0 to Iteration do
  212.             Write(LeftPart[i], ' ');
  213.          Writeln;
  214.          Iteration := RightPartLen - 1;
  215.          Write('Right part: ');
  216.          for i := 0 to Iteration do
  217.             Write(RightPart[i], ' ');
  218.          Writeln;
  219.          LeftIndex := 0;
  220.          RightIndex := 0;
  221.          SortArrayIndex := 0;
  222.          SetLength(UserArray, LeftPartLen + RightPartLen);
  223.  
  224.          while (LeftIndex < LeftPartLen) and (RightIndex < RightPartLen) do
  225.          begin
  226.             if LeftPart[LeftIndex] <= RightPart[RightIndex] then
  227.             begin
  228.                UserArray[SortArrayIndex] := LeftPart[LeftIndex];
  229.                Inc(LeftIndex);
  230.             end
  231.             else
  232.             begin
  233.                UserArray[SortArrayIndex] := RightPart[RightIndex];
  234.                Inc(RightIndex);
  235.             end;
  236.             Inc(SortArrayIndex);
  237.          end;
  238.  
  239.          while LeftIndex < LeftPartLen do
  240.          begin
  241.             UserArray[SortArrayIndex] := LeftPart[LeftIndex];
  242.             Inc(LeftIndex);
  243.             Inc(SortArrayIndex);
  244.          end;
  245.  
  246.          while RightIndex < RightPartLen do
  247.          begin
  248.             UserArray[SortArrayIndex] := RightPart[RightIndex];
  249.             Inc(RightIndex);
  250.             Inc(SortArrayIndex);
  251.          end;
  252.          Write('After merge: ');
  253.          Iteration := UserArrayLen - 1;
  254.          for i := 0 to Iteration do
  255.             Write(UserArray[i], ' ');
  256.          Writeln;
  257.          Writeln('----------');
  258.       end;
  259.    end
  260.    else
  261.       Write(UserArray[0]);
  262.    MergeSort := UserArray;
  263. end;
  264.  
  265.  
  266. procedure Output(const UserArr: TIntArr);
  267. var
  268.    Output: TextFile;
  269.    FileName: String;
  270.    Index, HighIndex: Integer;
  271. begin
  272.    Writeln;
  273.    Writeln('Sort array: ');
  274.    HighIndex := High(UserArr);
  275.    for Index := 0 to HighIndex do
  276.       Write(UserArr[Index], ' ');
  277.    Writeln;
  278.    Writeln('Do you want to enter data to file?');
  279.    if UserChooce() then
  280.    begin
  281.       Writeln('Please, enter name of the file.');
  282.       Writeln('Example: Text.txt');
  283.       Readln(FileName);
  284.       FileName := CorrectFormatOfTheFile(FileName);
  285.       AssignFile(Output, FileName);
  286.       try
  287.          if FileExists(FileName) then
  288.          begin
  289.             Write('A file with this name already exists, ');
  290.             Writeln('do you want to append information(otherwise rewrite)?');
  291.             if UserChooce() then
  292.                Append(Output)
  293.             else
  294.                Rewrite(Output);
  295.             end
  296.             else
  297.                Rewrite(Output);
  298.       except
  299.          Writeln('Error writing to file.');
  300.       end;
  301.       Writeln(Output, 'Sort array: ');
  302.       for Index := 0 to HighIndex do
  303.          Write(Output, UserArr[Index], ' ');
  304.       Writeln(Output);
  305.       CloseFile(Output);
  306.       Writeln('Write was successfully.');
  307.    end;
  308. end;
  309.  
  310.  
  311. procedure Main();
  312. var
  313.    UserArr: TIntArr;
  314. begin
  315.    Writeln('Hello, this program sort your array, via merge sort');
  316.    repeat
  317.       Writeln('Do you want to enter data through a file? (otherwise via console).');
  318.       if UserChooce then
  319.          UserArr := EnterArrFromFile()
  320.       else
  321.          UserArr := EnterArrFromConsole();
  322.       Writeln('Our subsequence');
  323.       Write('| ');
  324.       Output(MergeSort(UserArr));
  325.       Writeln('Do you want enter another array?');
  326.    until not UserChooce();
  327.    Writeln('Press enter to exit...');
  328.    Readln;
  329. end;
  330.  
  331.  
  332. begin
  333.    Main;
  334. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement