Advertisement
SmnVadik

Lab 6.6 (magic square) Delphi

Sep 10th, 2023 (edited)
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.38 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.     Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7.     System.Classes, Vcl.Graphics,
  8.     Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, Vcl.Menus;
  9.  
  10. type
  11.     TForm1 = class(TForm)
  12.         StringGrid1: TStringGrid;
  13.         Button1: TButton;
  14.         Edit1: TEdit;
  15.         Label1: TLabel;
  16.         MainMenu1: TMainMenu;
  17.         SaveDialog1: TSaveDialog;
  18.         OpenDialog1: TOpenDialog;
  19.         N1: TMenuItem;
  20.         N2: TMenuItem;
  21.         N3: TMenuItem;
  22.         N4: TMenuItem;
  23.         N5: TMenuItem;
  24.         N6: TMenuItem;
  25.         N7: TMenuItem;
  26.         procedure Button1Click(Sender: TObject);
  27.         procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  28.         procedure Edit1Change(Sender: TObject);
  29.         procedure N4Click(Sender: TObject);
  30.         procedure N5Click(Sender: TObject);
  31.         procedure N2Click(Sender: TObject);
  32.         procedure N3Click(Sender: TObject);
  33.         procedure N7Click(Sender: TObject);
  34.         procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  35.     private
  36.         { Private declarations }
  37.     public
  38.         { Public declarations }
  39.     end;
  40.  
  41.     TArr = array of array of Integer;
  42.  
  43. var
  44.     Form1: TForm1;
  45.     Path: String;
  46.     IsFileOpen: Boolean;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. Function magicSquareOfOddOrder(N: Integer): TArr;
  53. var
  54.     matrix: TArr;
  55.     I, J, Count, Y, X: Integer;
  56. Begin
  57.     SetLength(matrix, N, N);
  58.     For I := 0 to N - 1 do
  59.     Begin
  60.         For J := 0 to N - 1 do
  61.         Begin
  62.             matrix[I][J] := 0;
  63.         End;
  64.     End;
  65.  
  66.     Count := 1;
  67.     Y := 0;
  68.     X := N div 2;
  69.  
  70.     repeat
  71.         matrix[Y][X] := Count;
  72.         Inc(Count);
  73.  
  74.         if ((Y = 0) And (X >= N - 1)) And (matrix[N - 1][0] <> 0) then
  75.             Inc(Y)
  76.         Else
  77.         Begin
  78.             Dec(Y);
  79.             if Y < 0 then
  80.                 Y := N - 1;
  81.             Inc(X);
  82.             if X = N then
  83.                 X := 0;
  84.             if matrix[Y][X] <> 0 then
  85.             Begin
  86.                 Inc(Y, 2);
  87.                 Dec(X);
  88.             End;
  89.         End;
  90.  
  91.     until (Count = N * N + 1);
  92.  
  93.     Result := matrix;
  94. End;
  95.  
  96. Function magicSquareOfEvenOddOrder(N: Integer): TArr;
  97. var
  98.     half, I, J, X, Y, Move, Key: Integer;
  99.     matrix, tempMatrix: TArr;
  100. Begin
  101.     half := N div 2;
  102.     SetLength(matrix, N, N);
  103.     SetLength(tempMatrix, half, half);
  104.  
  105.     tempMatrix := magicSquareOfOddOrder(half);
  106.  
  107.     // 1/4 матрицы
  108.     for I := 0 to half - 1 do
  109.     Begin
  110.         For J := 0 to half - 1 do
  111.         Begin
  112.             matrix[I][J] := tempMatrix[I][J]
  113.         End;
  114.     End;
  115.  
  116.     // 2/4 матрицы
  117.     for I := 0 to half - 1 do
  118.     Begin
  119.         For J := half to N - 1 do
  120.         Begin
  121.             X := J - half;
  122.             matrix[I][J] := tempMatrix[I][X] + 2 * half * half
  123.         End;
  124.     End;
  125.  
  126.     // 3/4 матрицы
  127.     for I := half to N - 1 do
  128.     Begin
  129.         For J := 0 to half - 1 do
  130.         Begin
  131.             X := I - half;
  132.             matrix[I][J] := tempMatrix[X][J] + 3 * half * half
  133.         End;
  134.     End;
  135.  
  136.     // 4/4 матрицы
  137.     for I := half to N - 1 do
  138.     Begin
  139.         For J := half to N - 1 do
  140.         Begin
  141.             X := I - half;
  142.             Y := J - half;
  143.             matrix[I][J] := tempMatrix[X][Y] + half * half
  144.         End;
  145.     End;
  146.  
  147.     Move := 0;
  148.     For I := 6 to N - 1 do
  149.     Begin
  150.         If (I mod 4 <> 0) And (I mod 2 = 0) Then
  151.             Inc(Move)
  152.     End;
  153.  
  154.     For J := N div 2 - Move to ((N div 2) + Move - 1) do
  155.     Begin
  156.         For I := 0 to half - 1 do
  157.         Begin
  158.             Key := matrix[I][J];
  159.             matrix[I][J] := matrix[half + I][J];
  160.             matrix[half + I][J] := Key;
  161.         End;
  162.     End;
  163.  
  164.     For J := 0 to 1 do
  165.     Begin
  166.         If J = 0 Then
  167.         Begin
  168.             Key := matrix[0][0];
  169.             matrix[0][0] := matrix[half][0];
  170.             matrix[half][0] := Key;
  171.         End;
  172.         If J = 1 Then
  173.         Begin
  174.             Key := matrix[half - 1][0];
  175.             matrix[half - 1][0] := matrix[N - 1][0];
  176.             matrix[N - 1][0] := Key;
  177.         End;
  178.     End;
  179.  
  180.     For J := half + 1 to N - 2 do
  181.     Begin
  182.         For I := 1 to half - 2 do
  183.         Begin
  184.             Key := matrix[I][1];
  185.             matrix[I][1] := matrix[half + I][1];
  186.             matrix[half + I][1] := Key;
  187.         End;
  188.     End;
  189.  
  190.     Result := matrix;
  191. End;
  192.  
  193. procedure TForm1.Button1Click(Sender: TObject);
  194. var
  195.     I, J, Size: Integer;
  196.     magicSquare: TArr;
  197. begin
  198.     Size := StrToInt(Edit1.Text);
  199.     StringGrid1.ColCount := Size;
  200.     StringGrid1.RowCount := Size;
  201.     SetLength(magicSquare, Size, Size);
  202.  
  203.     If (Size mod 2 = 0) And (Size mod 4 <> 0) Then
  204.     Begin
  205.         magicSquare := magicSquareOfEvenOddOrder(Size);
  206.     End;
  207.  
  208.     For I := 0 to Size - 1 do
  209.     Begin
  210.         for J := 0 to Size - 1 do
  211.         Begin
  212.             StringGrid1.Cells[J, I] := IntToStr(magicSquare[I, J]);
  213.         End;
  214.     End;
  215.  
  216.     N5.Enabled := True;
  217. end;
  218.  
  219. procedure TForm1.Edit1Change(Sender: TObject);
  220. var
  221.     num, I: Integer;
  222.     IsCorrect: Boolean;
  223. begin
  224.     IsCorrect := True;
  225.     try
  226.         num := StrToInt(Edit1.Text);
  227.     except
  228.         IsCorrect := False;
  229.     end;
  230.  
  231.     If IsCorrect And ((num > 25) Or (num <= 2)) Then
  232.         IsCorrect := False;
  233.  
  234.     if IsCorrect And ((num < 10) And (Length(Edit1.Text) <> 1)) then
  235.         IsCorrect := False;
  236.  
  237.     if IsCorrect And ((num mod 2 <> 0) Or (num mod 4 = 0)) Then
  238.         IsCorrect := False;
  239.     Button1.Enabled := IsCorrect;
  240.     N5.Enabled := False;
  241.     for I := 0 to StringGrid1.RowCount - 1 do
  242.         StringGrid1.Rows[I].Clear
  243. end;
  244.  
  245. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  246. begin
  247.     If Not(Key in ['0' .. '9', #13, #8]) Then
  248.         Key := #0;
  249.     If (Key = #13) And (Button1.Enabled = True) Then
  250.         Button1.Click;
  251. end;
  252.  
  253. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  254. begin
  255.     CanClose := Application.MessageBox('Вы действительно хотите выйти?',
  256.       'Выход', MB_ICONQUESTION + MB_YESNO) = ID_YES
  257. end;
  258.  
  259. procedure TForm1.N2Click(Sender: TObject);
  260. const
  261.     Info1 = 'Разработать программу, которая генерирует магический квадрат'#13#10;
  262.     Info2 = 'чётно-нечётной степени (6, 10, 18, …).'#13#10;
  263.     Info3 = 'Размером квадрата является число чётно-нечётной степени, которое'#13#10;
  264.     Info4 = 'делится на 2 и не делится на 4';
  265. begin
  266.     Application.MessageBox(Info1 + Info2 + Info3 + Info4, 'Справка', 0)
  267. end;
  268.  
  269. procedure TForm1.N3Click(Sender: TObject);
  270. begin
  271.     Application.MessageBox('Сымоник Вадим, гр. 251004', 'Разработчик', 0)
  272. end;
  273.  
  274. Function GetSize(var FileInput: TextFile): String;
  275. Const
  276.     MIN_NUM = 2;
  277.     MAX_NUM = 25;
  278. Var
  279.     Size, num: Integer;
  280.     Str: String;
  281.     IsCorrect: Boolean;
  282. Begin
  283.     Size := 0;
  284.     Str := '';
  285.     If Not Eof(FileInput) Then
  286.     Begin
  287.         IsCorrect := True;
  288.         Try
  289.             Read(FileInput, Size);
  290.         Except
  291.             MessageBox(Form1.Handle, PChar('Недопустимый размер квадрата!'),
  292.               'Ошибка', MB_ICONSTOP);
  293.             IsCorrect := False;
  294.             Size := 0;
  295.         End;
  296.     End
  297.     Else
  298.         MessageBox(Form1.Handle, PChar('Недостаточно данных в файле!'),
  299.           'Ошибка', MB_ICONSTOP);
  300.     If (Size > MIN_NUM) And (Size <= MAX_NUM) Then
  301.         If (Size mod 2 = 0) And (Size mod 4 <> 0) Then
  302.             Str := IntToStr(Size)
  303.         Else
  304.             Application.MessageBox('Проверьте корректность данных в файле',
  305.               'Ошибка', 0);
  306.     GetSize := Str;
  307. End;
  308.  
  309. procedure TForm1.N4Click(Sender: TObject);
  310. var
  311.     FileInput: TextFile;
  312. begin
  313.     If OpenDialog1.Execute Then
  314.     Begin
  315.         AssignFile(FileInput, OpenDialog1.FileName);
  316.         Try
  317.             Try
  318.                 Reset(FileInput);
  319.                 Edit1.Text := GetSize(FileInput);
  320.                 if Edit1.Text <> '' then
  321.                 Begin
  322.                     Button1.Click;
  323.                 End;
  324.             Finally
  325.                 CloseFile(FileInput);
  326.             End;
  327.         Except
  328.  
  329.         End;
  330.     End;
  331. end;
  332.  
  333. Function Open(): String;
  334. begin
  335.     with Form1 Do
  336.     begin
  337.         If SaveDialog1.Execute Then
  338.         begin
  339.             Path := SaveDialog1.FileName;
  340.             IsFileOpen := True;
  341.         end
  342.         Else
  343.             IsFileOpen := False;
  344.     end;
  345.     Open := Path;
  346. end;
  347.  
  348. procedure TForm1.N5Click(Sender: TObject);
  349. var
  350.     F: TextFile;
  351.     I, J: Integer;
  352. begin
  353.     Path := Open;
  354.     If IsFileOpen Then
  355.     Begin
  356.         AssignFile(F, Path);
  357.         Rewrite(F);
  358.         For I := 0 to StringGrid1.RowCount - 1 do
  359.         Begin
  360.             for J := 0 to StringGrid1.ColCount do
  361.             Begin
  362.                 Write(F, StringGrid1.Cells[J, I] + ' ');
  363.             End;
  364.             Writeln(F)
  365.         End;
  366.         Application.MessageBox('Данные успешно сохранены в файл',
  367.           'Результат', 0);
  368.         CloseFile(F);
  369.     End;
  370. end;
  371.  
  372. procedure TForm1.N7Click(Sender: TObject);
  373. begin
  374.     Form1.Close;
  375. end;
  376.  
  377. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement