Advertisement
believe_me

Untitled

Nov 29th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 21.25 KB | None | 0 0
  1. program lab33;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.     System.SysUtils;
  8.  
  9. type
  10.     TArray = array of Integer;
  11.  
  12. Function CheckExtension(Path: String): Boolean; forward;
  13.  
  14. Function InputIntegerNumber(MinNumber, MaxNumber: Integer): Integer;
  15.  
  16. var
  17.     IsCorrect: Boolean;
  18.     Number: Integer;
  19.  
  20. begin
  21.     repeat
  22.         IsCorrect := true;
  23.         try
  24.             Readln(Number);
  25.         except
  26.             Writeln('You need to write an integer number, which is not less than ', MinNumber,
  27.                     ' and more than ', MaxNumber);
  28.             IsCorrect := false;
  29.         end;
  30.         if IsCorrect and ((Number < MinNumber) or (Number > MaxNumber)) then
  31.         begin
  32.             Writeln('You need to write a number, which is not less than  ', MinNumber,
  33.                     ' and more than ', MaxNumber);
  34.             IsCorrect := false;
  35.         end;
  36.     until IsCorrect;
  37.     InputIntegerNumber := Number;
  38. end;
  39.  
  40. Function ChooseWayOfInput(): Integer;
  41.  
  42. const
  43.     CONSOLE_WAY = 1;
  44.     FILE_WAY = 2;
  45.     RANDOM_GENERATION_INPUT = 3;
  46.  
  47. var
  48.     UserWay: Integer;
  49.  
  50. begin
  51.     Repeat
  52.         UserWay := InputIntegerNumber(1, 3);
  53.     Until (UserWay = CONSOLE_WAY) or (UserWay = FILE_WAY) or (UserWay = RANDOM_GENERATION_INPUT);
  54.     ChooseWayOfInput := UserWay;
  55. end;
  56.  
  57. Function ChooseWayOfOutput(): Integer;
  58.  
  59. const
  60.     CONSOLE_WAY = 1;
  61.     FILE_WAY = 2;
  62.  
  63. var
  64.     UserWay: Integer;
  65.  
  66. begin
  67.     Repeat
  68.         UserWay := InputIntegerNumber(1, 2);
  69.     Until (UserWay = CONSOLE_WAY) or (UserWay = FILE_WAY);
  70.     ChooseWayOfOutput := UserWay;
  71. end;
  72.  
  73. Function InputPathToFile(): String;
  74.  
  75. var
  76.     Path: String;
  77.     IsCorrect: Boolean;
  78.     NewFile: TextFile;
  79. begin
  80.     Writeln('Input path to file:');
  81.     repeat
  82.         Readln(Path);
  83.         IsCorrect := CheckExtension(Path);
  84.     until IsCorrect;
  85.     InputPathToFile := Path;
  86. end;
  87.  
  88. Function CheckPermissionForReading(Path: String): Boolean;
  89.  
  90. var
  91.     OutputFile: TextFile;
  92.     RightPermission: Boolean;
  93.  
  94. begin
  95.     Assign(OutputFile, Path);
  96.     RightPermission := true;
  97.     try
  98.         Reset(OutputFile);
  99.         Close(OutputFile);
  100.     except
  101.         Writeln('File is not available for reading.');
  102.         RightPermission := false;
  103.     end;
  104.     CheckPermissionForReading := RightPermission;
  105. end;
  106.  
  107. Function ReceiveNumberOfElementsFromConsole(): Integer;
  108.  
  109. var
  110.     NumberOfElements: Integer;
  111.     Text: String;
  112.  
  113. begin
  114.     Writeln('Input number of elements: ');
  115.     NumberOfElements := InputIntegerNumber(2, 200);
  116.     ReceiveNumberOfElementsFromConsole := NumberOfElements;
  117. end;
  118.  
  119. Function CheckFile(PathToFile: string): Boolean;
  120.  
  121. var
  122.     IsCorrect, Flag: Boolean;
  123.     EndNumber, i: Integer;
  124.     InputFile: TextFile;
  125.     Text: String;
  126.  
  127. begin
  128.     Assign(InputFile, PathToFile);
  129.     Reset(InputFile);
  130.     Read(InputFile, Text);
  131.     Flag:= true;
  132.     IsCorrect:= false;
  133.     EndNumber:= Length(Text) + 1;
  134.     i:= 1;
  135.     while (i < EndNumber) and Flag do
  136.     begin;
  137.         if (Text[i] <> ' ') and (Text[i] <> '-') then
  138.             try
  139.                 StrToInt(copy(Text, i, 1));
  140.             except
  141.                 Writeln('Received data includes non-integer chars.');
  142.                 Flag := false;
  143.             end;
  144.         Inc(i);
  145.     end;
  146.     Close(InputFile);
  147.     if flag and (Text = '') then
  148.     begin
  149.         Flag := false;
  150.         Writeln('File is empty');
  151.     end;
  152.     CheckFile := flag;
  153. end;
  154.  
  155. Function ReceiveNumberOfElementsFromFile(PathToFile: String): Integer;
  156.  
  157. var
  158.     InputFile: TextFile;
  159.     ELement, NumberOfElements: Integer;
  160.  
  161. begin;
  162.     NumberOfElements := 0;
  163.     Assign(InputFile, PathToFile);
  164.     Reset(InputFile);
  165.     while (not EOF(InputFile)) do
  166.     begin
  167.         Read(InputFile, Element);
  168.         Inc(NumberOfElements);
  169.     end;
  170.     Close(InputFile);
  171.     ReceiveNumberOfElementsFromFile := NumberOfElements;
  172. end;
  173.  
  174. Function ReceiveNumberOfElements(var UserWay: Integer; var PathToFile: string): Integer;
  175.  
  176. const
  177.     CONSOLE_WAY = 1;
  178.     FILE_WAY = 2;
  179.     RANDOM_GENERATION_WAY = 3;
  180.  
  181. var
  182.     NumberOfElements: Integer;
  183.     IsTextCorrect: Boolean;
  184.     IsCorrect: Boolean;
  185.  
  186. begin;
  187.     IsTextCorrect := true;
  188.     Writeln('Choose way of input:'#13#10'Type "1" if you want to receive sequence from console;'#13#10'Type "2" if you want to receieve sequence from file;'#13#10'Type "3" if you want to fill sequence with random numbers:');
  189.     UserWay := ChooseWayOfInput();
  190.     case UserWay of
  191.         CONSOLE_WAY:
  192.             begin
  193.                 NumberOfElements := ReceiveNumberOfElementsFromConsole();
  194.             end;
  195.         FILE_WAY:
  196.             begin
  197.                 repeat
  198.                     PathToFile := InputPathToFile();
  199.                 until CheckPermissionForReading(PathToFile) and CheckFile(PathToFile);
  200.                 NumberOfElements := ReceiveNumberOfElementsFromFile(PathToFile);
  201.             end;
  202.         RANDOM_GENERATION_WAY:
  203.             begin
  204.                 NumberOfElements := ReceiveNumberOfElementsFromConsole();
  205.             end;
  206.     end;
  207.     if not IsTextCorrect then
  208.         Writeln('There are no reqired elements.');
  209.     ReceiveNumberOfElements := NumberOfElements;
  210. end;
  211.  
  212. Procedure FillingSourceArrayFromConsole(var SourceArray: array of Integer);
  213.  
  214. var
  215.     i, EndNumber: Integer;
  216.  
  217. begin;
  218.     EndNumber:= length(SourceArray) - 1;
  219.     for i := 0 to EndNumber do
  220.     begin
  221.         Writeln('Input element: ');
  222.         SourceArray[i] := InputIntegerNumber(-50, 50);
  223.     end;
  224. end;
  225.  
  226. Procedure FillSourceArrayWithRandomNumbers(var SourceArray: array of Integer);
  227.  
  228. var
  229.     i, EndNumber: Integer;
  230.  
  231. begin
  232.     Randomize;
  233.     EndNumber := Length(SourceArray) - 1;
  234.     for i:= 0 to EndNumber do
  235.         SourceArray[i] := Random(101) - 50;
  236. end;
  237.  
  238. Procedure ReceieveSourceArrayFromFile(var SourceArray: array of Integer; PathToFile: string);
  239.  
  240. var
  241.     i, EndNumber: Integer;
  242.     InputFile: TextFile;
  243.  
  244. begin
  245.     Assign(InputFile, PathToFile);
  246.     Reset(InputFile);
  247.     i:= 0;
  248.     EndNumber := Length(SourceArray);
  249.     while (not EOF(InputFile)) and (i < EndNumber) do
  250.     begin
  251.         Read(InputFile, SourceArray[i]);
  252.         Inc(i);
  253.     end;
  254.     Close(InputFile);
  255. end;
  256.  
  257. Procedure ReceiveSourceArray(var SourceArray: array of Integer; NumberOfElements: Integer; UserWay: Integer; PathToFile: string);
  258.  
  259. const
  260.     CONSOLE_WAY = 1;
  261.     FILE_WAY = 2;
  262.     RANDOM_GENERATION_FAY = 3;
  263.  
  264. var
  265.     i, EndNumber: Integer;
  266.  
  267.  
  268. begin
  269.     case UserWay of
  270.         CONSOLE_WAY:
  271.             begin
  272.                 FillingSourceArrayFromConsole(SourceArray);
  273.             end;
  274.         FILE_WAY:
  275.             begin
  276.                 ReceieveSourceArrayFromFile(SourceArray, PathToFile);
  277.             end;
  278.         RANDOM_GENERATION_FAY:
  279.             begin
  280.                 FillSourceArrayWithRandomNumbers(SourceArray);
  281.             end;
  282.     end;
  283.     EndNumber:= Length(SourceArray) - 1;
  284.     Writeln('Source array:');
  285.     for i := 0 to EndNumber do
  286.         Write(SourceArray[i]:3,' ');
  287.     Writeln('');
  288. end;
  289.  
  290. Function CheckExtension(Path: String): Boolean;
  291.  
  292. var
  293.     RigthExtension: Boolean;
  294.  
  295. begin
  296.     if (ExtractFileExt(Path) = '.txt') then
  297.         RigthExtension := true
  298.     else
  299.     begin
  300.         Writeln('Wrong file extension.');
  301.         RigthExtension := false;
  302.     end;
  303.     CheckExtension := RigthExtension;
  304. end;
  305.  
  306. Function CheckPermissionForWriting(Path: String): Boolean;
  307.  
  308. var
  309.     OutputFile: TextFile;
  310.     RightPermission: Boolean;
  311.  
  312. begin
  313.     Assign(OutputFile, Path);
  314.     RightPermission := true;
  315.     try
  316.         Rewrite(OutputFile);
  317.         Close(OutputFile);
  318.     except
  319.         Writeln('File is not available for writing.');
  320.         RightPermission := false;
  321.     end;
  322.     CheckPermissionForWriting := RightPermission;
  323. end;
  324.  
  325. Procedure PrintResultToFile(Path: String; SortedArray: array of Integer);
  326.  
  327. var
  328.     OutputFile: TextFile;
  329.     i, EndNumber: Integer;
  330.  
  331. begin
  332.     Assign(OutputFile, Path);
  333.     Append(OutputFile);
  334.     Write(OutputFile, 'Sorted array: ');
  335.     EndNumber := Length(SortedArray) - 1;
  336.     for i := 0 to EndNumber do
  337.             Write(OutputFile, SortedArray[i]:3, ' ');
  338.     Close(OutputFile);
  339.     Writeln('Result is writen in file.');
  340. end;
  341.  
  342. Procedure PrintResultInConsole(SortedArray: array of Integer);
  343.  
  344. var
  345.     i, EndNumber: Integer;
  346.  
  347. begin;
  348.     Writeln('Sorted array: ');
  349.     EndNumber := Length(SortedArray) - 1;
  350.     for i := 0 to EndNumber do
  351.        Write(SortedArray[i]:3, ' ');
  352. end;
  353.  
  354. Procedure PrintResult(SortedArray: array of Integer; var UserWay: Integer; var PathToFile: string);
  355.  
  356. const
  357.     CONSOLE_WAY = 1;
  358.     FILE_WAY = 2;
  359.  
  360. begin;
  361.     case UserWay of
  362.         CONSOLE_WAY:
  363.             begin
  364.                 PrintResultInConsole(SortedArray);
  365.             end;
  366.         FILE_WAY:
  367.             begin
  368.                 PrintResultToFile(PathToFile, SortedArray);
  369.             end;
  370.     end;
  371.     Writeln('Program is completed.');
  372. end;
  373.  
  374. Procedure FillingArrayWithZeros(var SizesOfIntermediateArray: array of Integer);
  375.  
  376. var
  377.     Counter, i, NumberOfElements, EndNumber: Integer;
  378.  
  379. begin
  380.     NumberofElements:= length(SizesOfIntermediateArray);
  381.     Counter := NumberOfElements mod 2;
  382.     for i := 0 to Counter do
  383.         SizesOfIntermediateArray[i] := 0;
  384. end;
  385.  
  386. Function FindPositionToWriteSizesOfFirstSortingArray(var SizesOfFirstSortingArray: array of Integer; NumberOfElements: Integer): Integer;
  387.  
  388. var
  389.     i, Index: Integer;
  390.     IsCorrect: Boolean;
  391.  
  392. begin
  393.     i:= 0;
  394.     Index:= 0;
  395.     IsCorrect:= true;
  396.     while (i <  NumberOfElements) and IsCorrect do
  397.     begin
  398.         if (SizesOfFirstSortingArray[i] = 0) then
  399.         begin
  400.            IsCorrect:= false;
  401.            Index:= i;
  402.         end;
  403.         Inc(i);
  404.     end;
  405.     FindPositionToWriteSizesOfFirstSortingArray := Index;
  406. end;
  407.  
  408. Function FindPositionToWriteSizesOfSecondSortingArray(var SizesOfSecondSortingArray: array of Integer; NumberOfElements: Integer): Integer;
  409.  
  410. var
  411.     i, Index: Integer;
  412.     IsCorrect: Boolean;
  413.  
  414. begin
  415.     i:= 0;
  416.     Index:= 0;
  417.     IsCorrect:= true;
  418.     while (i <  NumberOfElements) and IsCorrect do
  419.     begin
  420.         if (SizesOfSecondSortingArray[i] = 0) then
  421.         begin
  422.            IsCorrect:= false;
  423.            Index:= i;
  424.         end;
  425.         Inc(i);
  426.     end;
  427.     FindPositionToWriteSizesOfSecondSortingArray := Index;
  428. end;
  429.  
  430. Function FindConditionOfSorted(var IntermediateArray: array of Integer) : Boolean;
  431.  
  432. var
  433.     Counter, i, EndNumber: Integer;
  434.     Flag: Boolean;
  435.  
  436. begin
  437.     Counter:= 0;
  438.     EndNumber:= Length(IntermediateArray) - 1;
  439.     for i := 1 to EndNumber do
  440.     begin;
  441.         if (IntermediateArray[i - 1] <= IntermediateArray[i]) then
  442.         begin
  443.             Inc(Counter);
  444.         end;
  445.     end;
  446.     if (Counter = EndNumber) then
  447.         Flag:= true
  448.     else
  449.         Flag:= false;
  450.     FindConditionOfSorted :=  Flag;
  451. end;
  452.  
  453. Procedure CreatingFirstF(var IntermediateArray, SourceArray: array of Integer);
  454.  
  455. var
  456.     EndNumber, i: Integer;
  457.  
  458. begin
  459.     EndNumber:= Length(SourceArray) - 1;
  460.     for i:= 0 to EndNumber do
  461.        IntermediateArray[i]:= SourceArray[i];
  462. end;
  463.  
  464. Procedure CreatingSortedArray(var SortedArray: array of Integer; var IntermediateArray: array of Integer);
  465.  
  466. var
  467.     EndNumber, i: Integer;
  468.  
  469. begin
  470.     EndNumber:= Length(IntermediateArray) - 1;
  471.     for i:= 0 to EndNumber do
  472.        SortedArray[i]:= IntermediateArray[i];
  473. end;
  474.  
  475. Function FindNumberOfPairs(var SizesOfFirstSortingArray: array of Integer; NumberOfElements: Integer): Integer;
  476.  
  477. var
  478.     Counter, i, NullPosition: Integer;
  479.  
  480. begin
  481.     i:= 0;
  482.     Counter:= NumberOfElements div 2 + 1;
  483.     NullPosition := Counter;
  484.     while i < Counter do
  485.     begin
  486.         if (SizesOfFirstSortingArray[i] = 0) then
  487.         begin
  488.             NullPosition := i;
  489.             i := Counter;
  490.         end;
  491.         Inc(i);
  492.     end;
  493.     FindNumberOfPairs := NullPosition;
  494. end;
  495.  
  496. Procedure FindSortedPart(var IntermediateArray: array of Integer; var Sort: array of Integer; NumberOfElements: Integer; var Index: Integer; var Length: Integer);
  497.  
  498. var
  499.     k, i, j: Integer;
  500.  
  501. begin
  502.     k := 0;
  503.     i := Index;
  504.     while (i < NumberOfElements) do
  505.      begin
  506.         if (i + 1 < NumberOfElements) then
  507.         begin
  508.             if (IntermediateArray[i] <= IntermediateArray[i + 1]) then
  509.                 Inc(k)
  510.             else
  511.                 i := NumberOfElements;
  512.         end;
  513.         Inc(i);
  514.      end;
  515.     if (k > 0) then
  516.     begin
  517.         j := 0;
  518.         for i := Index to Index + k do
  519.         begin
  520.             sort[j] := IntermediateArray[i];
  521.             Inc(j);
  522.         end;
  523.     end
  524.     else
  525.         sort[0] := IntermediateArray[Index];
  526.     Length := k + 1;
  527.     Index := Index + Length;
  528. end;
  529.  
  530. Procedure FillFirstSortingArray(var FirstSortingArray: array of Integer; var sort: array of Integer; var SizesOfFirstSortingArray: array of Integer; NumberOfElements: Integer; Length: Integer; var IndexOfIntermediateArray: Integer);
  531.  
  532. var
  533.     i, NumberOfPosition: Integer;
  534.  
  535. begin
  536.     for i := IndexOfIntermediateArray to IndexOfIntermediateArray + Length - 1 do
  537.         FirstSortingArray[i] := sort[i - IndexOfIntermediateArray];
  538.     NumberOfPosition := findPositionToWriteSizesOfFirstSortingArray(SizesOfFirstSortingArray, NumberOfElements);
  539.     SizesOfFirstSortingArray[NumberOfPosition] := Length;
  540.     IndexOfIntermediateArray := IndexOfIntermediateArray + Length;
  541. end;
  542.  
  543. Procedure FillSecondSortingArray(var SecondSortingArray: array of Integer; var sort: array of Integer; var SizesOfSecondSortingArray: array of Integer; NumberOfElements: Integer; Length: Integer; var IndexOfIntermediateArray: Integer);
  544.  
  545. var
  546.     i, NumberOfPosition: Integer;
  547.  
  548. begin
  549.  
  550.     for i := IndexOfIntermediateArray to IndexOfIntermediateArray + Length - 1 do
  551.         SecondSortingArray[i] := sort[i - IndexOfIntermediateArray];
  552.     NumberOfPosition := findPositionToWriteSizesOfSecondSortingArray(SizesOfSecondSortingArray, NumberOfElements);
  553.     SizesOfSecondSortingArray[NumberOfPosition] := Length;
  554.     IndexOfIntermediateArray := IndexOfIntermediateArray + Length;
  555. end;
  556.  
  557. Procedure FillingSortingArrays(var IntermediateArray: array of Integer; var FirstSortingArray: array of Integer; var SecondSortingArray: array of Integer; var SizesOfFirstSortingArray: array of Integer; var SizesOfFSecondSortingArray: array of Integer; NumberOfElements: Integer);
  558.  
  559. var
  560.     Index, IndexOfIntermediateArray, Length, Condition, i: Integer;
  561.     Sort: array of Integer;
  562.  
  563. begin
  564.     Index := 0;
  565.     IndexOfIntermediateArray := 0;
  566.     Length := 0;
  567.     Condition := 0;
  568.     i:= 0;
  569.     SetLength(Sort, NumberOfElements);
  570.     FillingArrayWithZeros(SizesOfFirstSortingArray);
  571.     FillingArrayWithZeros(SizesOfFSecondSortingArray);
  572.     while (i < NumberOfElements) do
  573.     begin
  574.         findSortedPart(IntermediateArray, sort, NumberOfElements, Index, Length);
  575.         if (Condition mod 2 = 0) then
  576.             FillFirstSortingArray(FirstSortingArray, sort, SizesOfFirstSortingArray, NumberOfElements, Length, IndexOfIntermediateArray)
  577.         else
  578.             FillSecondSortingArray(SecondSortingArray, sort, SizesOfFSecondSortingArray, NumberOfElements, Length, IndexOfIntermediateArray);
  579.         Inc(Condition);
  580.         i := i + Length;
  581.     end;
  582. end;
  583.  
  584. Procedure PrintArray(var SourceArray: array of Integer; UserWay: Integer; PathToFile: string);
  585.  
  586. const
  587.     CONSOLE_WAY = 1;
  588.     FILE_WAY = 2;
  589.  
  590. var
  591.     i, EndNumber: Integer;
  592.     FileOut: TextFile;
  593.  
  594. begin
  595.     EndNumber:= Length(SourceArray) - 1;
  596.     case UserWay of
  597.         CONSOLE_WAY:
  598.             begin
  599.                 for i := 0 to  EndNumber do
  600.                     Write(SourceArray[i]:3, ' ');
  601.                 Writeln('');
  602.             end;
  603.         FILE_WAY:
  604.             begin
  605.                 Assign(FileOut, PathToFile);
  606.                 Append(FileOut);
  607.                 for i := 0 to  EndNumber do
  608.                     Write(FileOut ,SourceArray[i]:3, ' ');
  609.                 Writeln(FileOut, '');
  610.                 Close(FileOut);
  611.             end;
  612.     end;
  613. end;
  614.  
  615. Procedure ReceiveNewSourceArray(var IntermediateArray: array of Integer; var FirstSortingArray: array of Integer; SecondSortingArray: array of Integer; SizesOfFirstSortingArray: array of Integer; SizesOfFSecondSortingArray: array of Integer; NumberOfElements: Integer; var UserWay: Integer; var PathToFile: string);
  616.  
  617. var
  618.     Counter, Index, i, j1, j2, z, EndNumber, SizeOfFirstSortingArray, SizeOfSecondSortingArray: Integer;
  619.  
  620. begin
  621.     fillingSortingArrays(IntermediateArray, FirstSortingArray, SecondSortingArray, SizesOfFirstSortingArray, SizesOfFSecondSortingArray, NumberOfElements);
  622.     Counter := FindNumberOfPairs(SizesOfFirstSortingArray, NumberOfElements);
  623.     Index := 0;
  624.     EndNumber := Counter - 1;
  625.     for i := 0 to EndNumber do
  626.     begin
  627.         SizeOfFirstSortingArray := SizesOfFirstSortingArray[i];
  628.         SizeOfSecondSortingArray := SizesOfFSecondSortingArray[i];
  629.         j1 := Index;
  630.         j2 := Index + SizeOfFirstSortingArray;
  631.         z := Index;
  632.         while (z < Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray) do
  633.         begin
  634.             if ((j1 = Index + SizeOfFirstSortingArray) and (j2 <> Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray)) then
  635.             begin
  636.                 while (j2 < SizeOfFirstSortingArray + SizeOfSecondSortingArray + Index) do
  637.                 begin
  638.                     IntermediateArray[z] := SecondSortingArray[j2];
  639.                     Inc(z);
  640.                     Inc(j2);
  641.                 end;
  642.             end;
  643.             if ((j2 = Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray) and (j1 <> Index + SizeOfFirstSortingArray)) then
  644.             begin
  645.                 while (j1 < SizeOfFirstSortingArray + Index) do
  646.                 begin
  647.                     IntermediateArray[z] := FirstSortingArray[j1];
  648.                     Inc(z);
  649.                     Inc(j1)
  650.                 end
  651.             end;
  652.             if ((FirstSortingArray[j1] < SecondSortingArray[j2]) and (j2 < NumberOfElements) and (j1 < NumberOfElements) and (z < Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray)) then
  653.             begin
  654.                 IntermediateArray[z] := FirstSortingArray[j1];
  655.                 Inc(z);
  656.                 Inc(j1);
  657.             end
  658.             else
  659.             begin
  660.                 if ((SecondSortingArray[j2] < FirstSortingArray[j1]) and (j2 < NumberOfElements) and (j1 < NumberOfElements) and (z < Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray)) then
  661.                 begin
  662.                     IntermediateArray[z] := SecondSortingArray[j2];
  663.                     Inc(z);
  664.                     Inc(j2)
  665.                 end
  666.                 else
  667.                     if ((SecondSortingArray[j2] = FirstSortingArray[j1]) and (j2 < NumberOfElements) and (j1 < NumberOfElements) and (z < Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray)) then
  668.                     begin
  669.                         IntermediateArray[z] := FirstSortingArray[j1];
  670.                         Inc(z);
  671.                         Inc(j1);
  672.                         IntermediateArray[z] := SecondSortingArray[j2];
  673.                         Inc(z);
  674.                         Inc(j2);
  675.                     end;
  676.             end;
  677.         end;
  678.         Index := Index + SizeOfFirstSortingArray + SizeOfSecondSortingArray;
  679.     end;
  680.     PrintArray(IntermediateArray, UserWay, PathToFile);
  681. end;
  682.  
  683. Procedure ReceieveWayOfOutput(var UserWay: Integer; var PathToFile: string);
  684.  
  685. const
  686.     CONSOLE_WAY = 1;
  687.     FILE_WAY = 2;
  688.  
  689. var
  690.     FileOut: TextFile;
  691.  
  692. begin
  693.     Writeln('Choose way of output:'#13#10'Type ''1'' if you want to print sorting and result in console.'#13#10'Type ''2'' if you want to print sorting and result in file.');
  694.     UserWay := ChooseWayOfOutput();
  695.     case UserWay of
  696.         CONSOLE_WAY:
  697.             begin
  698.                 Writeln('Sorting:');
  699.             end;
  700.         FILE_WAY:
  701.             begin
  702.                 repeat
  703.                     PathToFile := inputPathToFile();
  704.                 until (checkExtension(PathToFile) and checkPermissionForWriting(PathToFile));
  705.                 Assign(FileOut, PathToFile);
  706.                 Rewrite(FileOut);
  707.                 Writeln(FileOut, 'Sorting:');
  708.                 Close(FileOut);
  709.             end;
  710.     end;
  711. end;
  712.  
  713. Procedure Sorting(var SourceArray: array of Integer; var SortedArray: array of Integer; NumberOfElements: Integer; var UserWay: Integer; var PathToFile: string);
  714.  
  715. var
  716.    IntermediateArray, FirstSortingArray, SecondSortingArray, SizesOfFirstSortingArray, SizesOfFSecondSortingArray: array of Integer;
  717.    HalfNumberOfElements: Integer;
  718.  
  719. begin;
  720.     Setlength(IntermediateArray, NumberOfElements);
  721.     Setlength(FirstSortingArray, NumberOfElements);
  722.     Setlength(SecondSortingArray, NumberOfElements);
  723.     HalfNumberOfElements := NumberOfELements div 2 + 1;
  724.     Setlength(SizesOfFirstSortingArray, HalfNumberOfElements);
  725.     Setlength(SizesOfFSecondSortingArray, HalfNumberOfElements);
  726.     CreatingFirstF(IntermediateArray, SourceArray);
  727.     while (not findConditionOfSorted(IntermediateArray)) do
  728.     begin
  729.         ReceiveNewSourceArray(IntermediateArray, FirstSortingArray, SecondSortingArray, SizesOfFirstSortingArray, SizesOfFSecondSortingArray, NumberOfElements, UserWay, PathToFile);
  730.     end;
  731.     CreatingSortedArray(SortedArray, IntermediateArray);
  732. end;
  733.  
  734. Procedure main();
  735.  
  736. var
  737.     NumberOfElements, i, UserWay: Integer;
  738.     SourceArray, SortedArray: array of Integer;
  739.     PathToFile: string;
  740.  
  741. begin
  742.     Writeln('This program implements natural merge sorting.');
  743.     NumberOfElements := ReceiveNumberOfElements(UserWay, PathToFile);
  744.     Setlength(SourceArray, NumberOfElements);
  745.     ReceiveSourceArray(SourceArray, NumberOfElements, UserWay, PathToFile);
  746.     Setlength(SortedArray, NumberOfElements);
  747.     ReceieveWayOfOutput(UserWay, PathToFile);
  748.     Sorting(SourceArray, SortedArray, NumberOfElements, UserWay, PathToFile);
  749.     PrintResult(SortedArray, UserWay, PathToFile);
  750.     Readln;
  751. end;
  752.  
  753. begin
  754.     Main();
  755. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement