Advertisement
Egor_Vakar

(Delphi) lab 4.2 MainForm

Feb 23rd, 2022
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 12.04 KB | None | 0 0
  1. unit lab4_2;
  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.StdCtrls, Vcl.Menus,
  8.   Vcl.Imaging.pngimage, Vcl.ExtCtrls, ClipBrd;
  9.  
  10. type
  11.   TMainForm = class(TForm)
  12.     AnsLabel: TLabel;
  13.     Image1: TImage;
  14.     MainMenu: TMainMenu;
  15.     N1: TMenuItem;
  16.     OpenFileMenu: TMenuItem;
  17.     SaveToFileMenu: TMenuItem;
  18.     N4: TMenuItem;
  19.     NValueEdit: TEdit;
  20.     NValueLabel: TLabel;
  21.     outputLabel: TLabel;
  22.     ResultButton: TButton;
  23.     SumEdit: TEdit;
  24.     SumLabel: TLabel;
  25.     OpenFromFile: TOpenDialog;
  26.     SaveToFile: TSaveDialog;
  27.     procedure EditChange(Sender: TObject);
  28.     procedure KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  29.     procedure IntEditPress(Sender: TObject; var Key: Char);
  30.     procedure CutClick(Sender: TObject);
  31.     procedure CopyClick(Sender: TObject);
  32.     procedure DeleteClick(Sender: TObject);
  33.     procedure ResultButtonClick(Sender: TObject);
  34.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  35.     procedure DeveloperInfoClick(Sender: TObject);
  36.     procedure OpenFromFileMenuClick(Sender: TObject);
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure SaveToFileMenuClick(Sender: TObject);
  39.  
  40.  
  41.   private
  42.     { Private declarations }
  43.     id1: Integer;
  44.   public
  45.     { Public declarations }
  46.   end;
  47.  
  48. var
  49.   MainForm: TMainForm;
  50.  
  51. type
  52.     Ind = array of Integer;
  53.     AnswerMatrix = array of array of Integer;
  54.     Boolmatrix = array of array of boolean;
  55.  
  56. implementation
  57.  
  58. {$R *.dfm}
  59.  
  60.  
  61.  
  62. procedure TMainForm.CopyClick(Sender: TObject);
  63. begin
  64.     if PopupMenu.PopupComponent = NValueEdit then
  65.     begin
  66.         ClipBoard.AsText := NValueEdit.Text;
  67.     end;
  68.     if PopupMenu.PopupComponent = SumEdit then
  69.     begin
  70.         ClipBoard.AsText := SumEdit.Text;
  71.     end;
  72. end;
  73.  
  74. procedure TMainForm.CutClick(Sender: TObject);
  75. begin
  76.     if PopupMenu.PopupComponent = NValueEdit then
  77.     begin
  78.         ClipBoard.AsText := NValueEdit.Text;
  79.         NValueEdit.Text := '';
  80.     end;
  81.     if PopupMenu.PopupComponent = SumEdit then
  82.     begin
  83.         ClipBoard.AsText := SumEdit.Text;
  84.         SumEdit.Text := '';
  85.     end;
  86. end;
  87.  
  88. procedure TMainForm.DeleteClick(Sender: TObject);
  89. begin
  90.     if PopupMenu.PopupComponent = NValueEdit then
  91.     begin
  92.         NValueEdit.Text := '';
  93.     end;
  94.     if PopupMenu.PopupComponent = SumEdit then
  95.     begin
  96.         SumEdit.Text := '';
  97.     end;
  98. end;
  99.  
  100. procedure TMainForm.DeveloperInfoClick(Sender: TObject);
  101. begin
  102.     Application.MessageBox('Студент группы 151002 Вакарь Егор', 'О разработчике');
  103. end;
  104.  
  105. procedure TMainForm.EditChange(Sender: TObject);
  106. begin
  107.     OutputLabel.Caption := '';
  108.     MainForm.SaveToFileMenu.Enabled := False;
  109. end;
  110.  
  111. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  112. Var
  113.     WND: HWND;
  114.     lpCaption, lpText: PChar;
  115.     Tip: Integer;
  116. Begin
  117.     WND := MainForm.Handle;
  118.     lpCaption := 'Выход';
  119.     lpText := 'Вы уверены, что хотите выйти?';
  120.     Tip := MB_YESNO + MB_ICONINFORMATION + MB_DEFBUTTON2;
  121.     Case MessageBox(WND, lpText, lpCaption, Tip) Of
  122.         IDYES : CanClose := True;
  123.         IDNO : CanClose := False;
  124.     End
  125. End;
  126.  
  127.  
  128. procedure TMainForm.FormCreate(Sender: TObject);
  129. const
  130.      VK_F1Ex    = $70;
  131. begin
  132.     id1 := GlobalAddAtom('Hotkey1');
  133.     RegisterHotKey(Handle, id1, 0,VK_F1Ex);
  134. end;
  135.  
  136. procedure TMainForm.IntEditPress(Sender: TObject; var Key: Char);
  137. begin
  138.     if (TEdit(Sender).Text = '') and (Key = '0') then
  139.         Key := #0;
  140.     if not(Key in ['0'..'9', #08]) then
  141.         Key := #0;
  142.     if (Length(TEdit(Sender).Text) = 2) and not(Key = #08) then
  143.         Key := #0;
  144. end;
  145.  
  146. procedure TMainForm.KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  147. begin
  148.     TEdit(Sender).ReadOnly := (((Shift=[ssShift]) and (Key = VK_INSERT)) or (Shift=[ssCtrl]) or (Shift=[ssAlt]));
  149. end;
  150.  
  151. procedure TMainForm.OpenFromFileMenuClick(Sender: TObject);
  152. Var
  153.     i: Integer;
  154.     TempString: String;
  155.     IsCorrect: Boolean;
  156.     InputFile: TextFile;
  157. Begin
  158.     isCorrect := False;
  159.     If OpenFromFile.Execute() Then
  160.     Begin
  161.         try
  162.             AssignFile(InputFile, OpenFromFile.FileName);
  163.             Reset(InputFile);
  164.             IsCorrect := True;
  165.             i := 0;
  166.             while IsCorrect and  not Eof(InputFile) do
  167.             Begin
  168.                 Read(InputFile, TempString);
  169.                 try
  170.                     if (i = 0) then
  171.                     begin
  172.                         if (StrToInt(TempString) < 1) or (StrToInt(TempString) > 15) then
  173.                             IsCorrect := False
  174.                         else
  175.                             NValueEdit.Text := TempString;
  176.                     end;
  177.                     if (i = 1) then
  178.                     begin
  179.                         if (StrToInt(TempString) < 1) or (StrToInt(TempString) > 99) then
  180.                             IsCorrect := False
  181.                         else
  182.                             SumEdit.Text := TempString;
  183.                     end;
  184.                 except
  185.                     IsCorrect := False;
  186.                 end;
  187.                 Readln(InputFile);
  188.                 Inc(i);
  189.             end;
  190.         except
  191.             IsCorrect := False
  192.         end;
  193.     End;
  194.     if (i <> 2) or not(IsCorrect) then
  195.     begin
  196.         NValueEdit.Text := '';
  197.         SumEdit.Text := '';
  198.         CloseFile(InputFile);
  199.         Application.MessageBox('Данные в файле некорректны, попробуйте ещё раз.', 'Ошибка!', MB_ICONERROR);
  200.     end
  201.     else
  202.         CloseFile(InputFile);
  203.         ResultButton.Click;
  204. end;
  205.  
  206. procedure findSubsetsRec(arr: Ind; i, sum: Integer; var indexes: Ind; isFine: Boolmatrix; var ans: AnswerMatrix; var Counter: Integer);
  207. var
  208.     j: Integer;
  209. begin
  210.         if ((i = 0) and (sum <> 0) and (isFine[0][sum])) then
  211.         begin
  212.             j:= 0;
  213.             while (indexes[j]<> 0) do
  214.             begin
  215.                 Inc(j);
  216.             end;
  217.             indexes[j] := arr[i];
  218.             j:= 0;
  219.             while (indexes[j]<> 0) do
  220.             begin
  221.                 ans[counter][j] := indexes[j];
  222.                 Inc(j);
  223.             end;
  224.             Inc(Counter);
  225.             j:= 0;
  226.             while (indexes[j]<> 0) do
  227.             begin
  228.                 indexes[j] := 0;
  229.                 Inc(j);
  230.             end;
  231.             Exit;
  232.         end;
  233.         if ((i = 0) and (sum = 0)) then
  234.         begin
  235.             j:= 0;
  236.             while (indexes[j]<> 0) do
  237.             begin
  238.                 ans[counter][j] := indexes[j];
  239.                 Inc(j);
  240.             end;
  241.             Inc(Counter);
  242.             j:= 0;
  243.             while (indexes[j]<> 0) do
  244.             begin
  245.                 indexes[j] := 0;
  246.                 Inc(j);
  247.             end;
  248.             Exit;
  249.         end;
  250.         if (isFine[i - 1][sum]) then
  251.         begin
  252.             findSubsetsRec(arr, i - 1, sum, indexes, isFine, ans, Counter);
  253.         end;
  254.         if ((sum >= arr[i]) and (isFine[i - 1][sum - arr[i]])) then
  255.         begin
  256.             j:= 0;
  257.             while (indexes[j]<> 0) do
  258.             begin
  259.                 Inc(j);
  260.             end;
  261.             indexes[j] := arr[i];
  262.             findSubsetsRec(arr, i - 1, sum - arr[i], indexes, isFine, ans, Counter);
  263.         end;
  264. end;
  265.  
  266. function FindAllSubsets(n, sum: Integer; arr: Ind; isFine: Boolmatrix; var Ans: AnswerMatrix; var counter: Integer): AnswerMatrix;
  267. var
  268.     i, j: Integer;
  269.     indexes: Ind;
  270. begin
  271.     Counter := 0;
  272.     Setlength(indexes, n);
  273.     for i := 0 to n - 1 do
  274.         indexes[i] := 0;
  275.     for i := 0 to (n - 1) do
  276.         isFine[i][0] := True;
  277.     if (arr[0] <= sum) then
  278.         isFine[0][arr[0]] := true;
  279.     for i := 1 to n - 1 do
  280.         for j := 0 to sum do
  281.             if (arr[i] <= j) then
  282.                 isFine[i][j] := (isFine[i - 1][j] or isFine[i - 1][j - arr[i]])
  283.             else
  284.                 isFine[i][j] := isFine[i - 1][j];
  285.         if not(isFine[n - 1][sum]) then
  286.         begin
  287.             findAllSubsets(n, sum - 1, arr, isFine, ans, Counter);
  288.             FindAllSubsets := ans;
  289.         end;
  290.         findSubsetsRec(arr, n - 1, sum, indexes, isFine, ans, Counter);
  291.         FindAllSubsets := ans;
  292. end;
  293.  
  294. Function allSum(n: Integer): Integer;
  295. var
  296.     i, sum: Integer;
  297. begin
  298.     sum := 0;
  299.     for i := 1 to n do
  300.         sum := sum + i;
  301.     allSum := sum;
  302. end;
  303.  
  304. procedure TMainForm.ResultButtonClick(Sender: TObject);
  305. var
  306.     indexes: Ind;
  307.     i, j, n, sum, counter, chek: Integer;
  308.     answer: AnswerMatrix;
  309.     bool: Boolmatrix;
  310.     Temp, TempN, TempA: String;
  311.     isCorrect: Boolean;
  312. begin
  313.     try
  314.         isCorrect := (NValueEdit.Text <> '') and (SumEdit.Text <> '') and (StrToInt(NValueEdit.Text) > 0) and (StrToInt(NValueEdit.Text) < 16) and (StrToInt(SumEdit.Text) > 0) and (StrToInt(SumEdit.Text) < 100);
  315.     except
  316.         isCorrect := False;
  317.     end;
  318.     if(isCorrect) then
  319.     begin
  320.         n := StrToInt(NValueEdit.text);
  321.         sum := StrToInt(SumEdit.text);
  322.         counter := 0;
  323.         Setlength(bool, n, sum + 1);
  324.         SetLength(indexes, n);
  325.         for i := 0 to n - 1 do
  326.             indexes[i] := i + 1;
  327.         Setlength(answer,10000,n);
  328.         answer[0][0] := 0;
  329.         if allSum(n) > sum then
  330.         begin
  331.             answer := FindAllSubsets(n, sum, indexes, bool, answer, counter);
  332.             i := 0;
  333.             j := 0;
  334.             while (answer[i][j] <> 0 ) and (i < 1) do
  335.             begin
  336.                 if i <> 0 then
  337.                 begin
  338.                     Temp := outputLabel.Caption;
  339.                     chek := 0;
  340.                 end;
  341.                 outputLabel.Caption := '';
  342.                 while (answer[i][j] <> 0) do
  343.                 begin
  344.                     chek := chek + answer[i][j];
  345.                     OutputLabel.Caption := outputLabel.Caption + '[' + IntToStr(answer[i][j]) + '] ';
  346.                     outputLabel.Width := outputLabel.Width + 200;
  347.                     Inc(j);
  348.                 end;
  349.             end;
  350.             SaveToFileMenu.Enabled := true;
  351.         end
  352.         else
  353.         begin
  354.             Temp := '';
  355.             outputLabel.Width := outputLabel.Width + 200;
  356.             for i := Length(indexes) - 1 downto 0 do
  357.                 Temp := Temp + '[' + IntToStr(indexes[i]) + '] ';
  358.             OutputLabel.Caption := Temp;
  359.             SaveToFileMenu.Enabled := true;
  360.         end;
  361.     end
  362.     else
  363.     begin
  364.         TempN := NValueEdit.Text;
  365.         TempA := SumEdit.Text;
  366.         NValueEdit.Text := '';
  367.         SumEdit.Text := '';
  368.         if (TempN = '') or (TempA = '') then
  369.             Application.MessageBox('Не все ячейки заполнены.', 'Ошибка!', MB_ICONERROR)
  370.         else
  371.             if (StrToInt(TempN) > 15) or  (StrToInt(TempA) > 99) then
  372.                 Application.MessageBox('Введённые числа не входят в указанный диапазон'#13#10'Величина n: 1..15'#13#10'Величина А: 1..99', 'Ошибка!', MB_ICONERROR);
  373.     end;
  374. end;
  375.  
  376.  
  377.  
  378. procedure TMainForm.SaveToFileMenuClick(Sender: TObject);
  379. Var
  380.     OutputFile: TextFile;
  381. Begin
  382.     If SaveToFile.Execute() And FileExists(SaveToFile.FileName) Then
  383.     Begin
  384.         AssignFile(OutputFile, SaveToFile.FileName);
  385.         Try
  386.             Rewrite(OutputFile);
  387.         Except
  388.             Application.MessageBox('Отказано в доступе! Измените параметры файла! ', 'Ошибка!', MB_ICONERROR);
  389.         End;
  390.         Writeln(OutputFile, NValueLabel.Caption, ' ', NValueEdit.Text, #13#10, SumLabel.Caption, ' ', SumEdit.Text, #13#10, AnsLabel.Caption, ' ', outputLAbel.Caption);
  391.         CloseFile(OutputFile);
  392.         Application.MessageBox('Данные успешно записаны в файл!', 'Сохранение', MB_ICONINFORMATION);
  393.     End
  394.     Else
  395.         Application.MessageBox('Введено некорректное имя файла', 'Ошибка!', MB_ICONERROR);
  396.  
  397. End;
  398.  
  399. end.
  400.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement