Advertisement
Guest User

Untitled

a guest
May 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.77 KB | None | 0 0
  1. unit MainUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls;
  8.  
  9. type
  10.   PRing = ^TRing;
  11.   TRing = record
  12.      Element: Integer;
  13.      PNextEl: PRing;
  14.   end;
  15.   TMainForm = class(TForm)
  16.     btCreate: TButton;
  17.     btAdd: TButton;
  18.     btDelete: TButton;
  19.     MainMenu: TMainMenu;
  20.     Help: TMenuItem;
  21.     About: TMenuItem;
  22.     PopupMenu: TPopupMenu;
  23.     procedure btCreateClick(Sender: TObject);
  24.     procedure btAddClick(Sender: TObject);
  25.     procedure btDeleteClick(Sender: TObject);
  26.     procedure HelpClick(Sender: TObject);
  27.     procedure AboutClick(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.   public
  31.     { Public declarations }
  32.   end;
  33.  
  34. var
  35.   MainForm: TMainForm;
  36.   Head, CurEL: PRing;
  37.  
  38.  
  39. implementation
  40.  
  41. {$R *.dfm}
  42.  
  43. uses AddUnit, DeleteUnit;
  44.  
  45. procedure TMainForm.AboutClick(Sender: TObject);
  46. begin
  47.    ShowMessage('Zhenya Yakubovich 851001');
  48. end;
  49.  
  50. procedure TMainForm.btAddClick(Sender: TObject);
  51. begin
  52.    AddForm.ShowModal;
  53. end;
  54.  
  55. procedure TMainForm.btCreateClick(Sender: TObject);
  56. begin
  57.    Head := nil;
  58.    CurEl := nil;
  59.    btAdd.Enabled := True;
  60.    btDelete.Enabled := False;
  61. end;
  62.  
  63. procedure TMainForm.btDeleteClick(Sender: TObject);
  64. begin
  65.    DeleteForm.ShowModal;
  66. end;
  67.  
  68. procedure TMainForm.HelpClick(Sender: TObject);
  69. begin
  70.    ShowMessage('This program implements work with ring data structure.' + #13#10 +
  71.      ' Available features: create a new ring, add, delete, view elements.');
  72. end;
  73.  
  74. end.
  75.  
  76. unit AddUnit;
  77.  
  78. interface
  79.  
  80. uses
  81.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  82.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, MainUnit;
  83.  
  84. type
  85.   TAddForm = class(TForm)
  86.     AddGrid: TStringGrid;
  87.     Count: TEdit;
  88.     lbCount: TLabel;
  89.     btAddEl: TButton;
  90.     procedure FormActivate(Sender: TObject);
  91.     procedure CountChange(Sender: TObject);
  92.     procedure AddGridKeyPress(Sender: TObject; var Key: Char);
  93.     procedure AddGridSetEditText(Sender: TObject; ACol, ARow: Integer;
  94.       const Value: string);
  95.     procedure btAddElClick(Sender: TObject);
  96.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  97.   private
  98.     { Private declarations }
  99.   public
  100.     { Public declarations }
  101.   end;
  102.  
  103. var
  104.   AddForm: TAddForm;
  105.   procedure AddElement(var CurEl: PRing; Head: PRing; Element: Integer);
  106.   external 'AddElem.dll' name 'AddElement';
  107.  
  108. implementation
  109.  
  110. {$R *.dfm}
  111.  
  112. procedure TAddForm.AddGridKeyPress(Sender: TObject; var Key: Char);
  113. const
  114.    ValidSymbols = ['0'..'9', '-'];
  115. var
  116.    i: Integer;
  117. begin
  118.    if not((Key in ValidSymbols) or (Key = #8)) then
  119.       Key := #0;
  120.    with Sender as TStringGrid do
  121.    begin
  122.       if (Key in ValidSymbols) and (Length(Cells[Col, Row]) > 7) then
  123.       begin
  124.          Key := #0;
  125.          ShowMessage('Maximum length of symbols: 8.');
  126.       end;
  127.       if (Key = '0') and (Pos('0', Cells[Col, Row]) = 1) and (Length(Cells[Col, Row]) = 1) then
  128.          Key := #0;
  129.       if (Key = '-') and (Length(Cells[Col, Row]) >= 1) then
  130.          Key := #0;
  131.       if (Key = '0') and (Pos('-', Cells[Col, Row]) = 1) and
  132.         (Length(Cells[Col, Row]) = 1) then
  133.          Key := #0;
  134.    end;
  135. end;
  136.  
  137. procedure TAddForm.AddGridSetEditText(Sender: TObject; ACol, ARow: Integer;
  138.   const Value: string);
  139. var
  140.    i: Byte;
  141.    IsFull: Boolean;
  142. begin
  143.    IsFull := True;
  144.    with Sender as TStringGrid do
  145.    begin
  146.       for i := 0 to RowCount - 1 do
  147.          if Length(Cells[1, i]) = 0 then
  148.             IsFull := False;
  149.       if IsFull then
  150.          btAddEl.Enabled := True
  151.       else
  152.          btAddEl.Enabled := False;
  153.    end;
  154. end;
  155.  
  156. procedure TAddForm.btAddElClick(Sender: TObject);
  157. var
  158.    i: Byte;
  159. begin
  160.    i := 0;
  161.    if Head = nil then
  162.    begin
  163.       New(Head);
  164.       CurEl := Head;
  165.       CurEl^.PNextEl := Head;
  166.       CurEl^.Element := StrToInt(AddGrid.Cells[1, 0]);
  167.       i := 1;
  168.    end;
  169.    while i < AddGrid.RowCount do
  170.    begin
  171.       {CurEl^.PNextEl := nil;
  172.       New(CurEl^.PNextEl);
  173.       CurEl := CurEl^.PNextEl;
  174.       CurEl^.Element := StrToInt(AddGrid.Cells[1, i]);
  175.       CurEl^.PNextEl := Head;}
  176.       AddElement(CurEl, Head, StrToInt(AddGrid.Cells[1, i]));
  177.       Inc(i);
  178.    end;
  179.    MainForm.btDelete.Enabled := True;
  180.    AddForm.Close;
  181. end;
  182.  
  183. procedure TAddForm.CountChange(Sender: TObject);
  184. var
  185.    i: Byte;
  186. begin
  187.    with Sender as TEdit do
  188.    begin
  189.       if Length(Text) = 0 then
  190.       begin
  191.          with AddGrid do
  192.          begin
  193.             Cols[0].Clear;
  194.             Cols[1].Clear;
  195.             Cells[0, 0] := '1';
  196.             RowCount := 1;
  197.             ScrollBars := ssNone;
  198.             Width := 102;
  199.             Options := Options - [goEditing];
  200.          end;
  201.          btAddEl.Enabled := False;
  202.       end
  203.       else
  204.       with AddGrid do
  205.       begin
  206.          Options := Options + [goEditing];
  207.          AddGrid.RowCount := StrToInt(Text);
  208.          if AddGrid.RowCount > 3 then
  209.          begin
  210.             AddGrid.ScrollBars := ssVertical;
  211.             AddGrid.Width := 124;
  212.          end;
  213.          for i := 1 to StrToInt(Text) - 1 do
  214.             AddGrid.Cells[0, i] := IntToStr(i + 1);
  215.       end;
  216.    end;
  217. end;
  218.  
  219. procedure TAddForm.FormActivate(Sender: TObject);
  220. begin
  221.    AddGrid.ColWidths[0] := 20;
  222.    AddGrid.Width := 102;
  223.    AddGrid.Cells[0, 0] := '1';
  224. end;
  225.  
  226. procedure TAddForm.FormClose(Sender: TObject; var Action: TCloseAction);
  227. begin
  228.    with AddGrid do
  229.    begin
  230.       Cols[0].Clear;
  231.       Cols[1].Clear;
  232.       Cells[0, 0] := '1';
  233.       RowCount := 1;
  234.       ScrollBars := ssNone;
  235.       Width := 102;
  236.    end;
  237.    Count.Text := '';
  238.    btAddEl.Enabled := False;
  239. end;
  240.  
  241. end.
  242.  
  243. library AddElem;
  244.  
  245. uses
  246.   System.SysUtils,
  247.   System.Classes;
  248.  
  249. {$R *.res}
  250.  
  251. type
  252.    PRing = ^TRing;
  253.    TRing = record
  254.      Element: Integer;
  255.      PNextEl: PRing;
  256.    end;
  257.  
  258. procedure AddElement(var CurEl: PRing; Head: PRing; Element: Integer);
  259. begin
  260.    CurEl^.PNextEl := nil;
  261.    New(CurEl^.PNextEl);
  262.    CurEl := CurEl^.PNextEl;
  263.    CurEl^.Element := Element;
  264.    CurEl^.PNextEl := Head;
  265. end;
  266.  
  267. exports AddElement;
  268.  
  269. begin
  270. end.
  271.  
  272. unit DeleteUnit;
  273.  
  274. interface
  275.  
  276. uses
  277.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  278.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;
  279.  
  280. type
  281.   TDeleteForm = class(TForm)
  282.     Count: TEdit;
  283.     lbCount: TLabel;
  284.     DelGrid: TStringGrid;
  285.     btDelEl: TButton;
  286.     procedure CountChange(Sender: TObject);
  287.     procedure btDelElClick(Sender: TObject);
  288.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  289.   private
  290.     { Private declarations }
  291.   public
  292.     { Public declarations }
  293.   end;
  294.  
  295. var
  296.   DeleteForm: TDeleteForm;
  297.  
  298. implementation
  299.  
  300. uses MainUnit;
  301.  
  302. {$R *.dfm}
  303.  
  304. procedure TDeleteForm.btDelElClick(Sender: TObject);
  305. var
  306.    i: Byte;
  307.    DelEl, Temp, OldHead: PRing;
  308. begin
  309.    i := 0;
  310.    OldHead := Head;
  311.    CurEl := Head;
  312.    while (CurEl^.PNextEl <> OldHead) and (i < StrToInt(Count.Text)) do
  313.    begin
  314.       Dispose(Head);
  315.       Head := CurEl^.PNextEl;
  316.       CurEl := Head;
  317.       Inc(i);
  318.    end;
  319.    if (CurEl^.PNextEl = OldHead) and (i < StrToInt(Count.Text)) then
  320.    begin
  321.       Head := nil;
  322.       CurEl := nil;
  323.       ShowMessage('Ring is empty.');
  324.       DelGrid.Visible := False;
  325.       Count.Text := '';
  326.       MainForm.btDelete.Enabled := False;
  327.       DeleteForm.Close;
  328.    end
  329.    else
  330.    begin
  331.       while (CurEl^.PNextEl <> OldHead) do
  332.          CurEl := CurEl^.PNextEl;
  333.       CurEl^.PNextEl := Head;
  334.       CurEl := Head;
  335.       DelGrid.Visible := True;
  336.       Count.Text := '';
  337.       btDelEl.Enabled := False;
  338.       i := 0;
  339.       DelGrid.ColWidths[0] := 20;
  340.       DelGrid.Width := 102;
  341.       DelGrid.ScrollBars := ssNone;
  342.       while CurEl^.PNextEl <> Head do
  343.       begin
  344.          DelGrid.RowCount := i + 1;
  345.          DelGrid.Cells[0, i] := IntToStr(i + 1);
  346.          DelGrid.Cells[1, i] := IntToStr(CurEl^.Element);
  347.          CurEl := CurEl^.PNextEl;
  348.          Inc(i);
  349.       end;
  350.       DelGrid.RowCount := i + 1;
  351.       DelGrid.Cells[0, i] := IntToStr(i + 1);
  352.       DelGrid.Cells[1, i] := IntToStr(CurEl^.Element);
  353.       if i > 3 then
  354.       begin
  355.          DelGrid.Width := 124;
  356.          DelGrid.ScrollBars := ssVertical;
  357.       end;
  358.    end;
  359. end;
  360.  
  361. procedure TDeleteForm.CountChange(Sender: TObject);
  362. begin
  363.    if Length(Count.Text) = 0 then
  364.       btDelEl.Enabled := False
  365.    else
  366.       btDelEl.Enabled := True;
  367. end;
  368.  
  369. procedure TDeleteForm.FormClose(Sender: TObject; var Action: TCloseAction);
  370. begin
  371.    with DelGrid do
  372.    begin
  373.       Cols[0].Clear;
  374.       Cols[1].Clear;
  375.       RowCount := 1;
  376.       ScrollBars := ssNone;
  377.       Width := 102;
  378.       Visible := False;
  379.    end;
  380.    Count.Text := '';
  381. end;
  382.  
  383. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement