Advertisement
SolahYana

Untitled

May 21st, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.60 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, Grids, ExtCtrls;
  8.  
  9. type
  10.   TClient = record
  11.     Name: string;
  12.     Surname: string;
  13.     RoomNumber: Integer;
  14.   end;
  15.  
  16.   TForm1 = class(TForm)
  17.     StringGrid1: TStringGrid;
  18.     Panel1: TPanel;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Label3: TLabel;
  22.     Edit1: TEdit;
  23.     Edit2: TEdit;
  24.     Edit3: TEdit;
  25.     Button1: TButton;
  26.     Button2: TButton;
  27.     Button3: TButton;
  28.     ComboBox1: TComboBox;
  29.     Label4: TLabel;
  30.     Edit4: TEdit;
  31.     Button4: TButton;
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure Button1Click(Sender: TObject);
  34.     procedure Button2Click(Sender: TObject);
  35.     procedure Button3Click(Sender: TObject);
  36.     procedure ComboBox1Change(Sender: TObject);
  37.     procedure Button4Click(Sender: TObject);
  38.   private
  39.     Clients: array of TClient;
  40.     ClientCount: Integer;
  41.     FilteredClients: array of TClient;
  42.     FilteredCount: Integer;
  43.     procedure AddClient(const Name, Surname: string; RoomNumber: Integer);
  44.     procedure DeleteClient(Index: Integer);
  45.     procedure UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer);
  46.     procedure ApplyFilter;
  47.     procedure DisplayClients(Clients: array of TClient; Count: Integer); // Исправлено: &Count -> Count
  48.   public
  49.     { Public declarations }
  50.   end;
  51.  
  52. var
  53.   Form1: TForm1;
  54.  
  55. implementation
  56.  
  57. {$R *.dfm}
  58.  
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   // Инициализация StringGrid
  62.   StringGrid1.ColCount := 4;
  63.   StringGrid1.RowCount := 2;
  64.   StringGrid1.Cells[0, 0] := 'ID';
  65.   StringGrid1.Cells[1, 0] := 'Имя';
  66.   StringGrid1.Cells[2, 0] := 'Фамилия';
  67.   StringGrid1.Cells[3, 0] := 'Номер комнаты';
  68.  
  69.   // Инициализация ComboBox для фильтрации
  70.   ComboBox1.Items.Add('Все');
  71.   ComboBox1.Items.Add('Имя');
  72.   ComboBox1.Items.Add('Фамилия');
  73.   ComboBox1.ItemIndex := 0;
  74.  
  75.   ClientCount := 0;
  76.   FilteredCount := 0;
  77. end;
  78.  
  79. // Добавление клиента
  80. procedure TForm1.AddClient(const Name, Surname: string; RoomNumber: Integer);
  81. var
  82.   NewClient: TClient;
  83. begin
  84.   NewClient.Name := Name;
  85.   NewClient.Surname := Surname;
  86.   NewClient.RoomNumber := RoomNumber;
  87.  
  88.   SetLength(Clients, ClientCount + 1);
  89.   Clients[ClientCount] := NewClient;
  90.   Inc(ClientCount);
  91. end;
  92.  
  93. // Удаление клиента
  94. procedure TForm1.DeleteClient(Index: Integer);
  95. var
  96.   i: Integer;
  97. begin
  98.   if (Index gt;= 0) and (Index lt; ClientCount) then
  99.   begin
  100.     for i := Index to ClientCount - 2 do
  101.       Clients[i] := Clients[i + 1];
  102.  
  103.     SetLength(Clients, ClientCount - 1);
  104.     Dec(ClientCount);
  105.   end;
  106. end;
  107.  
  108. // Изменение данных клиента
  109. procedure TForm1.UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer);
  110. begin
  111.   if (Index gt;= 0) and (Index lt; ClientCount) then
  112.   begin
  113.     Clients[Index].Name := Name;
  114.     Clients[Index].Surname := Surname;
  115.     Clients[Index].RoomNumber := RoomNumber;
  116.   end;
  117. end;
  118.  
  119. // Применение фильтра
  120. procedure TForm1.ApplyFilter;
  121. var
  122.   i: Integer;
  123. begin
  124.   FilteredCount := 0;
  125.   SetLength(FilteredClients, 0);
  126.  
  127.   case ComboBox1.ItemIndex of
  128.     0: // Все
  129.       begin
  130.         FilteredClients := Copy(Clients, 0, ClientCount);
  131.         FilteredCount := ClientCount;
  132.       end;
  133.     1: // Имя
  134.       begin
  135.         for i := 0 to ClientCount - 1 do
  136.           if Clients[i].Name = Edit4.Text then
  137.           begin
  138.             SetLength(FilteredClients, FilteredCount + 1);
  139.             FilteredClients[FilteredCount] := Clients[i];
  140.             Inc(FilteredCount);
  141.           end;
  142.       end;
  143.     2: // Фамилия
  144.       begin
  145.         for i := 0 to ClientCount - 1 do
  146.           if Clients[i].Surname = Edit4.Text then
  147.           begin
  148.             SetLength(FilteredClients, FilteredCount + 1);
  149.             FilteredClients[FilteredCount] := Clients[i];
  150.             Inc(FilteredCount);
  151.           end;
  152.       end;
  153.   end;
  154. end;
  155.  
  156. // Отображение клиентов в StringGrid
  157. procedure TForm1.DisplayClients(Clients: array of TClient; Count: Integer); // Исправлено: &Count -> Count
  158. var
  159.   i: Integer;
  160. begin
  161.   StringGrid1.RowCount := Count + 1;
  162.   for i := 0 to Count - 1 do
  163.   begin
  164.     StringGrid1.Cells[0, i + 1] := IntToStr(i + 1);
  165.     StringGrid1.Cells[1, i + 1] := Clients[i].Name;
  166.     StringGrid1.Cells[2, i + 1] := Clients[i].Surname;
  167.     StringGrid1.Cells[3, i + 1] := IntToStr(Clients[i].RoomNumber);
  168.   end;
  169. end;
  170.  
  171. // Обработчики событий
  172.  
  173. procedure TForm1.Button1Click(Sender: TObject);
  174. begin
  175.   AddClient(Edit1.Text, Edit2.Text, StrToInt(Edit3.Text));
  176.   ApplyFilter;
  177.   DisplayClients(FilteredClients, FilteredCount);
  178. end;
  179.  
  180. procedure TForm1.Button2Click(Sender: TObject);
  181. begin
  182.   if StringGrid1.Row gt; 1 then
  183.   begin
  184.     DeleteClient(StringGrid1.Row - 2);
  185.     ApplyFilter;
  186.     DisplayClients(FilteredClients, FilteredCount);
  187.   end;
  188. end;
  189.  
  190. procedure TForm1.Button3Click(Sender: TObject);
  191. begin
  192.   if StringGrid1.Row gt; 1 then
  193.   begin
  194.     UpdateClient(StringGrid1.Row - 2, Edit1.Text, Edit2.Text, StrToInt(Edit3.Text));
  195.     ApplyFilter;
  196.     DisplayClients(FilteredClients, FilteredCount);
  197.   end;
  198. end;
  199.  
  200. procedure TForm1.ComboBox1Change(Sender: TObject);
  201. begin
  202.   ApplyFilter;
  203.   DisplayClients(FilteredClients, FilteredCount);
  204. end;
  205.  
  206. procedure TForm1.Button4Click(Sender: TObject);
  207. begin
  208.   ComboBox1.ItemIndex := 0;
  209.   Edit4.Text := '';
  210.   ApplyFilter;
  211.   DisplayClients(FilteredClients, FilteredCount);
  212. end;
  213.  
  214. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement