Advertisement
ksyshshot

Лаба 3.2 Формы

Dec 10th, 2022 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.10 KB | Source Code | 0 0
  1. Unit Unit1;
  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.   TSet = Set Of Char;
  11.   TForm1 = Class(TForm)
  12.     MainMenu1: TMainMenu;
  13.     N1: TMenuItem;
  14.     N2: TMenuItem;
  15.     N3: TMenuItem;
  16.     N4: TMenuItem;
  17.     Task: TLabel;
  18.     InputStringLabel: TLabel;
  19.     EditInputString: TEdit;
  20.     ReceivedSetLabel: TLabel;
  21.     EditOutputSet: TEdit;
  22.     ButtonFindElements: TButton;
  23.     OpenDialog1: TOpenDialog;
  24.     SaveDialog1: TSaveDialog;
  25.     procedure ButtonFindElementsClick(Sender: TObject);
  26.     procedure EditInputStringChange(Sender: TObject);
  27.     procedure EditInputStringKeyPress(Sender: TObject; var Key: Char);
  28.     procedure N1Click(Sender: TObject);
  29.     procedure N2Click(Sender: TObject);
  30.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  31.     procedure N3Click(Sender: TObject);
  32.     procedure N4Click(Sender: TObject);
  33.   Private
  34.     { Private declarations }
  35.   Public
  36.     { Public declarations }
  37.   End;
  38.  
  39. Var
  40.   Form1: TForm1;
  41.   Sequence: AnsiString;
  42.  
  43. Implementation
  44.  
  45. {$R *.dfm}
  46.  
  47. uses Unit3, Unit2, Unit4;
  48.  
  49. Function GetSearchСharactersSet(Sequence: AnsiString): TSet;
  50. Const
  51.     SearchCharacters = [#40, #41, #91, #93, #123, #125, #48..#57];
  52. Var
  53.     SearchSet: TSet;
  54.     I: Integer;
  55. Begin
  56.     SearchSet := [];
  57.     For I := 1 To Length(Sequence) Do
  58.         If Sequence[I] In SearchCharacters Then
  59.             Include(SearchSet, Sequence[I]);
  60.     GetSearchСharactersSet := SearchSet;
  61. End;
  62.  
  63. Procedure TForm1.ButtonFindElementsClick(Sender: TObject);
  64. Var
  65.     SearchSet: TSet;
  66.     I: Integer;
  67. Begin
  68.     SearchSet := GetSearchСharactersSet(Sequence);
  69.     If SearchSet <> [] Then
  70.     Begin
  71.         For I := 0 To 255 Do
  72.             If Chr(I) In SearchSet Then
  73.                 EditOutputSet.Text := EditOutputSet.Text + Chr(I) + ' ';
  74.     End
  75.     Else
  76.         EditOutputSet.Text := 'В строке не было найдено скобок и цифр.';
  77.     ButtonFindElements.Enabled := False;
  78.     N2.Enabled := True;
  79. End;
  80.  
  81. procedure TForm1.EditInputStringChange(Sender: TObject);
  82. begin
  83.     ButtonFindElements.Enabled := True;
  84.     N2.Enabled := False;
  85.     EditOutputSet.Text := '';
  86.     If (EditInputString.Text = '') Then
  87.     Begin
  88.         N2.Enabled := False;
  89.         ButtonFindElements.Enabled := False;
  90.         Sequence := '';
  91.     End
  92.     Else
  93.         Sequence := Sequence + EditInputString.Text;
  94.  
  95. end;
  96.  
  97. procedure TForm1.EditInputStringKeyPress(Sender: TObject; var Key: Char);
  98. begin
  99.     If Key = #13 Then
  100.         ButtonFindElementsClick(Sender);
  101. end;
  102.  
  103. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  104. Var
  105.     Res: Integer;
  106. begin
  107.     Res := Unit2.Form2.ShowModal();
  108.     If Res = mrOk Then
  109.         CanClose := True
  110.     Else
  111.         CanClose := False;
  112. end;
  113.  
  114. procedure TForm1.N1Click(Sender: TObject);
  115. var
  116.     F: TextFile;
  117.     Path: String;
  118. begin
  119.     If OpenDialog1.Execute() Then
  120.     Begin
  121.         Path := OpenDialog1.FileName;
  122.         AssignFile(F, Path);
  123.         Try
  124.             Reset(F);
  125.             Try
  126.                 Readln(F, Sequence);
  127.                 EditInputString.Text := Sequence;
  128.             Finally
  129.                 CloseFile(F);
  130.             End;
  131.         Except
  132.             EditInputString.Text := 'Ошибка считывания с файла';
  133.         End;
  134.     End;
  135. end;
  136.  
  137. procedure TForm1.N2Click(Sender: TObject);
  138. Var
  139.     F: TextFile;
  140.     Path: String;
  141. begin
  142.     If SaveDialog1.Execute() Then
  143.     Begin
  144.         Path := SaveDialog1.FileName;
  145.         AssignFile(F, Path);
  146.         Try
  147.             Rewrite(F);
  148.             Try
  149.                 Write(F, EditOutputSet.Text);
  150.             Finally
  151.                 CloseFile(F);
  152.             End;
  153.         Except
  154.             EditOutputSet.Text := 'Ошибка записи в файл';
  155.         End;
  156.  
  157.     End;
  158. end;
  159.  
  160.  
  161.  
  162. procedure TForm1.N3Click(Sender: TObject);
  163. begin
  164.     Unit3.Form3.ShowModal();
  165. end;
  166.  
  167. procedure TForm1.N4Click(Sender: TObject);
  168. begin
  169.     Unit4.Form4.ShowModal();
  170. end;
  171.  
  172. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement