Advertisement
dxvmxnd

Untitled

May 30th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. unit Unit2;
  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.ComCtrls, Vcl.Grids,
  8. Vcl.Menus;
  9.  
  10. type
  11. TMatrix = Array Of Array Of Integer;
  12. TMainForm = class(TForm)
  13. SizeEdit: TEdit;
  14. UpDownSIze: TUpDown;
  15. CountButton: TButton;
  16. PopupMenu1: TPopupMenu;
  17. MainMenu1: TMainMenu;
  18. OpenDialog1: TOpenDialog;
  19. SaveDialog1: TSaveDialog;
  20. StringGrid1: TStringGrid;
  21. N1: TMenuItem;
  22. N2: TMenuItem;
  23. N3: TMenuItem;
  24. N4: TMenuItem;
  25. N5: TMenuItem;
  26. N6: TMenuItem;
  27. N7: TMenuItem;
  28. N11: TMenuItem;
  29. N12: TMenuItem;
  30. procedure SizeEditChange(Sender: TObject);
  31. procedure N4Click(Sender: TObject);
  32. procedure N5Click(Sender: TObject);
  33. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  34. procedure N7Click(Sender: TObject);
  35. procedure N3Click(Sender: TObject);
  36. private
  37. { Private declarations }
  38. public
  39. { Public declarations }
  40. end;
  41.  
  42. var
  43. MainForm: TMainForm;
  44.  
  45. implementation
  46.  
  47. {$R *.dfm}
  48.  
  49. procedure FillMainMatrix(var Matrix : TMatrix);
  50. var
  51. I, J , Count : Integer;
  52.  
  53. Begin
  54. Count := 1;
  55.  
  56. For I := 0 To High(Matrix) Do
  57. Begin
  58. For J := 0 To High(Matrix) Do
  59. Begin
  60. Matrix[I, J] := Count;
  61. Inc(Count);
  62. End;
  63. End;
  64. End;
  65.  
  66. procedure FillHelpMatrix(var Matrix : TMatrix);
  67. var
  68. I, J , Count : Integer;
  69.  
  70. Begin
  71. Count := 1;
  72.  
  73. For I := High(Matrix) DownTo 0 Do
  74. Begin
  75. For J := High(Matrix) DownTo 0 Do
  76. Begin
  77. Matrix[I, J] := Count;
  78. Inc(Count);
  79. End;
  80. End;
  81. End;
  82.  
  83. Procedure ChangeNumbers(var Matrix, TempHelpMatrix : TMatrix);
  84. var
  85. I, Count : Integer;
  86. Begin
  87. For I := 0 To High(Matrix) Do
  88. Begin
  89. Matrix[I, I] := TempHelpMatrix[I, I];
  90. End;
  91.  
  92. For I := 0 To High(Matrix) Do
  93. Begin
  94. Matrix[I, High(Matrix) - I] := TempHelpMatrix[I, High(Matrix) - I];
  95. End;
  96. End;
  97.  
  98. procedure Count(SizeEdit : TEdit; var StringGrid1 : TStringGrid);
  99. var
  100. Size : Integer;
  101. MainMatrix, HelpMatrix : TMatrix;
  102. I,J,K,L : Integer;
  103. TempMatrix : TMatrix;
  104. TempHelpMatrix : TMatrix;
  105. begin
  106. Size := StrToInt(SizeEdit.Text);
  107.  
  108. SetLength(TempMatrix, 4, 4);
  109. SetLength(MainMatrix, Size, Size);
  110. SetLength(HelpMatrix, Size, Size);
  111. SetLength(TempHelpMatrix, 4, 4);
  112.  
  113. FillMainMatrix(MainMatrix);
  114. FillHelpMatrix(HelpMatrix);
  115. I := 0;
  116. J := 0;
  117.  
  118. While I <= (Size - 4) Do
  119. Begin
  120. While J <= (Size - 4) Do
  121. Begin
  122. For K := 0 To 3 Do
  123. Begin
  124. For L := 0 To 3 Do
  125. Begin
  126. TempMatrix[K, L] := MainMatrix[I+K, J+L];
  127. TempHelpMatrix[K, L] := HelpMatrix[I+K, J+L];
  128. End;
  129. End;
  130. ChangeNumbers(TempMatrix, TempHelpMatrix);
  131. For K := 0 To 3 Do
  132. Begin
  133. For L := 0 To 3 Do
  134. Begin
  135. MainMatrix[I+K, J+L] := TempMatrix[K, L];
  136. End;
  137. End;
  138. Inc(J, 4);
  139. End;
  140. J := 0;
  141. Inc(I, 4);
  142. End;
  143.  
  144.  
  145. For I := 0 To High(MainMatrix) Do
  146. Begin
  147. For J := 0 To High(MainMatrix) Do
  148. Begin
  149. StringGrid1.Cells[J, I] := IntToStr(MainMatrix[I, J]);
  150. End;
  151. End;
  152. end;
  153.  
  154. procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
  155. var
  156. DlgRes:integer;
  157. begin
  158. DlgRes := MessageBox(MainForm.Handle, 'Закрыть программу?', 'Сообщение', MB_YESNO or MB_ICONQUESTION);
  159. if DlgRes = ID_NO then
  160. action :=caNone;
  161. end;
  162.  
  163. procedure TMainForm.N3Click(Sender: TObject);
  164. begin
  165. Application.MessageBox('Непесов Рустам, 351004, @dxvmx', 'О разработчике');
  166. end;
  167.  
  168. procedure TMainForm.N4Click(Sender: TObject);
  169. Var
  170. Num : Integer;
  171. IsCorrect : Boolean;
  172. InputFile : TextFile;
  173. Str : String;
  174. begin
  175.  
  176. If(OpenDialog1.Execute) Then
  177. Begin
  178. Try
  179. AssignFile(InputFile, OpenDialog1.FileName);
  180. Reset(InputFile);
  181. Except
  182. Application.MessageBox('Ошибка при открытии файла!', 'Ошибка!');
  183. Exit;
  184. End;
  185.  
  186. IsCorrect := True;
  187. Try
  188. Readln(InputFile, Str);
  189. Num := StrToInt(Str);
  190. Except
  191. IsCorrect := False;
  192. End;
  193. If(Not IsCorrect) Or (Num mod 4 <> 0) Or (Num < 4) Or (Num > 100) Then
  194. Begin
  195. Application.MessageBox('Ошибка при записи данных из файла! Учтите, что число должно быть кратным 4 и от 4 до 100.','Ошибка');
  196. CloseFile(InputFile);
  197. Exit;
  198. End;
  199. CloseFile(InputFile);
  200. SizeEdit.Text := IntToStr(Num);
  201. End;
  202. end;
  203.  
  204. procedure TMainForm.N5Click(Sender: TObject);
  205. var
  206. OutputFile : TextFile;
  207. I, J : Integer;
  208. begin
  209. If(SaveDialog1.Execute) Then
  210. Begin
  211. Try
  212. AssignFile(OutputFile, SaveDialog1.FileName);
  213. Rewrite(OutputFile);
  214. Except
  215. Application.MessageBox('Ошибка при записи в файл!', 'Ошибка!');
  216. Exit;
  217. End;
  218. Writeln(OutputFile, 'Магический квадрат порядка ' + SizeEdit.Text + ' :');
  219. For I := 0 To (StringGrid1.ColCount - 1) Do
  220. Begin
  221. For J := 0 To (StringGrid1.ColCount - 1) Do
  222. Begin
  223. Write(OutputFile, StringGrid1.Cells[J, I], ' ');
  224. End;
  225. Writeln(OutputFile);
  226. End;
  227. CloseFile(OutputFile);
  228. Application.MessageBox('Данные успешно записаны в файл!', 'Успех!');
  229. End;
  230. end;
  231.  
  232. procedure TMainForm.N7Click(Sender: TObject);
  233. begin
  234. MainForm.Close;
  235. end;
  236.  
  237. procedure TMainForm.SizeEditChange(Sender: TObject);
  238. begin
  239. If(StrToInt(SizeEdit.Text) < 4) then
  240. SizeEdit.Text := '4';
  241. StringGrid1.RowCount := StrToInt(SizeEdit.Text);
  242. StringGrid1.ColCount := StrToInt(SizeEdit.Text);
  243. Count(SizeEdit, StringGrid1);
  244. end;
  245.  
  246. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement