Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.42 KB | None | 0 0
  1. unit MUtils;
  2.  
  3. interface
  4.  
  5. uses VCL.GRIDS, System.SysUtils, List;
  6.  
  7. function MergeNonDescendingLists(ListA, ListB: TList): TList;
  8. procedure RandomizeSG(var E: TStringGrid);
  9. procedure ClearSG(var E: TStringGrid);
  10. function CheckIfNonDescendingAndCorrectSG(var E: TStringGrid): Boolean;
  11.  
  12. implementation
  13.  
  14. uses Winapi.Windows;
  15.  
  16. function MergeNonDescendingLists(ListA, ListB: TList): TList;
  17. var
  18.    Temp: TList;
  19. begin
  20.    Temp := TList.Create;
  21.    while (ListA.Next <> nil) or (ListB.Next <> nil) do
  22.    begin
  23.       if (ListA.Next <> nil) and (ListB.Next <> nil) then
  24.       begin
  25.          if ListA.Next.Val <= ListB.Next.Val then
  26.          begin
  27.             Temp.Add(ListA.Next.Val);
  28.             ListA := ListA.Next;
  29.          end
  30.          else
  31.          begin
  32.             if ListB.Next <> nil then
  33.             begin
  34.                Temp.Add(ListB.Next.Val);
  35.                ListB := ListB.Next;
  36.             end;
  37.          end;
  38.       end
  39.       else
  40.       begin
  41.          if ListA.Next <> nil then
  42.          begin
  43.             Temp.Add(ListA.Next.Val);
  44.             ListA := ListA.Next;
  45.          end
  46.          else
  47.          begin
  48.             Temp.Add(ListB.Next.Val);
  49.             ListB := ListB.Next;
  50.          end;
  51.       end;
  52.    end;
  53.    Result := Temp;
  54. end;
  55.  
  56. function CheckIfNonDescendingAndCorrectSG(var E: TStringGrid): Boolean;
  57. var
  58.    I: Integer;
  59. begin
  60.    Randomize;
  61.    with E do
  62.       for I := 0 to ColCount - 2 do
  63.          try
  64.             if StrToInt(Cells[I + 1, 0]) < StrToInt(Cells[I, 0]) then
  65.             begin
  66.                Result := False;
  67.                Exit;
  68.             end;
  69.          except
  70.             MessageBox(0, PChar('List shouldn''t to contain empty fields.'),
  71.               PChar('Warning!'), MB_ICONERROR);
  72.             Result := False;
  73.             Exit;
  74.          end;
  75.    Result := True;
  76. end;
  77.  
  78. procedure RandomizeSG(var E: TStringGrid);
  79. var
  80.    I, Buf: Integer;
  81. begin
  82.    Randomize;
  83.    with E do
  84.    begin
  85.       Buf := 1 + Random(ColCount);
  86.       Cells[0, 0] := IntToStr(Buf);
  87.       for I := 1 to ColCount - 1 do
  88.       begin
  89.          Buf := 1 + Random(ColCount) + StrToInt(Cells[I - 1, 0]);
  90.          Cells[I, 0] := IntToStr(Buf);
  91.       end;
  92.    end;
  93. end;
  94.  
  95. procedure ClearSG(var E: TStringGrid);
  96. var
  97.    I: Integer;
  98. begin
  99.    with E do
  100.       for I := ColCount - 1 downto 0 do
  101.          Cells[I, 0] := '';
  102.    E.ColCount := 1;
  103.    Exit;
  104.  
  105.    E.Rows[0].Clear;
  106.    E.ColCount := 0;
  107. end;
  108.  
  109. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement