Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MainUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls;
- type
- PRing = ^TRing;
- TRing = record
- Element: Integer;
- PNextEl: PRing;
- end;
- TMainForm = class(TForm)
- btCreate: TButton;
- btAdd: TButton;
- btDelete: TButton;
- MainMenu: TMainMenu;
- Help: TMenuItem;
- About: TMenuItem;
- PopupMenu: TPopupMenu;
- procedure btCreateClick(Sender: TObject);
- procedure btAddClick(Sender: TObject);
- procedure btDeleteClick(Sender: TObject);
- procedure HelpClick(Sender: TObject);
- procedure AboutClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- Head, CurEL: PRing;
- implementation
- {$R *.dfm}
- uses AddUnit, DeleteUnit;
- procedure TMainForm.AboutClick(Sender: TObject);
- begin
- ShowMessage('Zhenya Yakubovich 851001');
- end;
- procedure TMainForm.btAddClick(Sender: TObject);
- begin
- AddForm.ShowModal;
- end;
- procedure TMainForm.btCreateClick(Sender: TObject);
- begin
- Head := nil;
- CurEl := nil;
- btAdd.Enabled := True;
- btDelete.Enabled := False;
- end;
- procedure TMainForm.btDeleteClick(Sender: TObject);
- begin
- DeleteForm.ShowModal;
- end;
- procedure TMainForm.HelpClick(Sender: TObject);
- begin
- ShowMessage('This program implements work with ring data structure.' + #13#10 +
- ' Available features: create a new ring, add, delete, view elements.');
- end;
- end.
- unit AddUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, MainUnit;
- type
- TAddForm = class(TForm)
- AddGrid: TStringGrid;
- Count: TEdit;
- lbCount: TLabel;
- btAddEl: TButton;
- procedure FormActivate(Sender: TObject);
- procedure CountChange(Sender: TObject);
- procedure AddGridKeyPress(Sender: TObject; var Key: Char);
- procedure AddGridSetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- procedure btAddElClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- AddForm: TAddForm;
- procedure AddElement(var CurEl: PRing; Head: PRing; Element: Integer);
- external 'AddElem.dll' name 'AddElement';
- implementation
- {$R *.dfm}
- procedure TAddForm.AddGridKeyPress(Sender: TObject; var Key: Char);
- const
- ValidSymbols = ['0'..'9', '-'];
- var
- i: Integer;
- begin
- if not((Key in ValidSymbols) or (Key = #8)) then
- Key := #0;
- with Sender as TStringGrid do
- begin
- if (Key in ValidSymbols) and (Length(Cells[Col, Row]) > 7) then
- begin
- Key := #0;
- ShowMessage('Maximum length of symbols: 8.');
- end;
- if (Key = '0') and (Pos('0', Cells[Col, Row]) = 1) and (Length(Cells[Col, Row]) = 1) then
- Key := #0;
- if (Key = '-') and (Length(Cells[Col, Row]) >= 1) then
- Key := #0;
- if (Key = '0') and (Pos('-', Cells[Col, Row]) = 1) and
- (Length(Cells[Col, Row]) = 1) then
- Key := #0;
- end;
- end;
- procedure TAddForm.AddGridSetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- var
- i: Byte;
- IsFull: Boolean;
- begin
- IsFull := True;
- with Sender as TStringGrid do
- begin
- for i := 0 to RowCount - 1 do
- if Length(Cells[1, i]) = 0 then
- IsFull := False;
- if IsFull then
- btAddEl.Enabled := True
- else
- btAddEl.Enabled := False;
- end;
- end;
- procedure TAddForm.btAddElClick(Sender: TObject);
- var
- i: Byte;
- begin
- i := 0;
- if Head = nil then
- begin
- New(Head);
- CurEl := Head;
- CurEl^.PNextEl := Head;
- CurEl^.Element := StrToInt(AddGrid.Cells[1, 0]);
- i := 1;
- end;
- while i < AddGrid.RowCount do
- begin
- {CurEl^.PNextEl := nil;
- New(CurEl^.PNextEl);
- CurEl := CurEl^.PNextEl;
- CurEl^.Element := StrToInt(AddGrid.Cells[1, i]);
- CurEl^.PNextEl := Head;}
- AddElement(CurEl, Head, StrToInt(AddGrid.Cells[1, i]));
- Inc(i);
- end;
- MainForm.btDelete.Enabled := True;
- AddForm.Close;
- end;
- procedure TAddForm.CountChange(Sender: TObject);
- var
- i: Byte;
- begin
- with Sender as TEdit do
- begin
- if Length(Text) = 0 then
- begin
- with AddGrid do
- begin
- Cols[0].Clear;
- Cols[1].Clear;
- Cells[0, 0] := '1';
- RowCount := 1;
- ScrollBars := ssNone;
- Width := 102;
- Options := Options - [goEditing];
- end;
- btAddEl.Enabled := False;
- end
- else
- with AddGrid do
- begin
- Options := Options + [goEditing];
- AddGrid.RowCount := StrToInt(Text);
- if AddGrid.RowCount > 3 then
- begin
- AddGrid.ScrollBars := ssVertical;
- AddGrid.Width := 124;
- end;
- for i := 1 to StrToInt(Text) - 1 do
- AddGrid.Cells[0, i] := IntToStr(i + 1);
- end;
- end;
- end;
- procedure TAddForm.FormActivate(Sender: TObject);
- begin
- AddGrid.ColWidths[0] := 20;
- AddGrid.Width := 102;
- AddGrid.Cells[0, 0] := '1';
- end;
- procedure TAddForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- with AddGrid do
- begin
- Cols[0].Clear;
- Cols[1].Clear;
- Cells[0, 0] := '1';
- RowCount := 1;
- ScrollBars := ssNone;
- Width := 102;
- end;
- Count.Text := '';
- btAddEl.Enabled := False;
- end;
- end.
- library AddElem;
- uses
- System.SysUtils,
- System.Classes;
- {$R *.res}
- type
- PRing = ^TRing;
- TRing = record
- Element: Integer;
- PNextEl: PRing;
- end;
- procedure AddElement(var CurEl: PRing; Head: PRing; Element: Integer);
- begin
- CurEl^.PNextEl := nil;
- New(CurEl^.PNextEl);
- CurEl := CurEl^.PNextEl;
- CurEl^.Element := Element;
- CurEl^.PNextEl := Head;
- end;
- exports AddElement;
- begin
- end.
- unit DeleteUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;
- type
- TDeleteForm = class(TForm)
- Count: TEdit;
- lbCount: TLabel;
- DelGrid: TStringGrid;
- btDelEl: TButton;
- procedure CountChange(Sender: TObject);
- procedure btDelElClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- DeleteForm: TDeleteForm;
- implementation
- uses MainUnit;
- {$R *.dfm}
- procedure TDeleteForm.btDelElClick(Sender: TObject);
- var
- i: Byte;
- DelEl, Temp, OldHead: PRing;
- begin
- i := 0;
- OldHead := Head;
- CurEl := Head;
- while (CurEl^.PNextEl <> OldHead) and (i < StrToInt(Count.Text)) do
- begin
- Dispose(Head);
- Head := CurEl^.PNextEl;
- CurEl := Head;
- Inc(i);
- end;
- if (CurEl^.PNextEl = OldHead) and (i < StrToInt(Count.Text)) then
- begin
- Head := nil;
- CurEl := nil;
- ShowMessage('Ring is empty.');
- DelGrid.Visible := False;
- Count.Text := '';
- MainForm.btDelete.Enabled := False;
- DeleteForm.Close;
- end
- else
- begin
- while (CurEl^.PNextEl <> OldHead) do
- CurEl := CurEl^.PNextEl;
- CurEl^.PNextEl := Head;
- CurEl := Head;
- DelGrid.Visible := True;
- Count.Text := '';
- btDelEl.Enabled := False;
- i := 0;
- DelGrid.ColWidths[0] := 20;
- DelGrid.Width := 102;
- DelGrid.ScrollBars := ssNone;
- while CurEl^.PNextEl <> Head do
- begin
- DelGrid.RowCount := i + 1;
- DelGrid.Cells[0, i] := IntToStr(i + 1);
- DelGrid.Cells[1, i] := IntToStr(CurEl^.Element);
- CurEl := CurEl^.PNextEl;
- Inc(i);
- end;
- DelGrid.RowCount := i + 1;
- DelGrid.Cells[0, i] := IntToStr(i + 1);
- DelGrid.Cells[1, i] := IntToStr(CurEl^.Element);
- if i > 3 then
- begin
- DelGrid.Width := 124;
- DelGrid.ScrollBars := ssVertical;
- end;
- end;
- end;
- procedure TDeleteForm.CountChange(Sender: TObject);
- begin
- if Length(Count.Text) = 0 then
- btDelEl.Enabled := False
- else
- btDelEl.Enabled := True;
- end;
- procedure TDeleteForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- with DelGrid do
- begin
- Cols[0].Clear;
- Cols[1].Clear;
- RowCount := 1;
- ScrollBars := ssNone;
- Width := 102;
- Visible := False;
- end;
- Count.Text := '';
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement