Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, Vcl.Menus;
- type
- TForm1 = class(TForm)
- StringGrid1: TStringGrid;
- Button1: TButton;
- Edit1: TEdit;
- Label1: TLabel;
- MainMenu1: TMainMenu;
- SaveDialog1: TSaveDialog;
- OpenDialog1: TOpenDialog;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- N7: TMenuItem;
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit1Change(Sender: TObject);
- procedure N4Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- procedure N7Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- TArr = array of array of Integer;
- var
- Form1: TForm1;
- Path: String;
- IsFileOpen: Boolean;
- implementation
- {$R *.dfm}
- Function magicSquareOfOddOrder(N: Integer): TArr;
- var
- matrix: TArr;
- I, J, Count, Y, X: Integer;
- Begin
- SetLength(matrix, N, N);
- For I := 0 to N - 1 do
- Begin
- For J := 0 to N - 1 do
- Begin
- matrix[I][J] := 0;
- End;
- End;
- Count := 1;
- Y := 0;
- X := N div 2;
- repeat
- matrix[Y][X] := Count;
- Inc(Count);
- if ((Y = 0) And (X >= N - 1)) And (matrix[N - 1][0] <> 0) then
- Inc(Y)
- Else
- Begin
- Dec(Y);
- if Y < 0 then
- Y := N - 1;
- Inc(X);
- if X = N then
- X := 0;
- if matrix[Y][X] <> 0 then
- Begin
- Inc(Y, 2);
- Dec(X);
- End;
- End;
- until (Count = N * N + 1);
- Result := matrix;
- End;
- Function magicSquareOfEvenOddOrder(N: Integer): TArr;
- var
- half, I, J, X, Y, Move, Key: Integer;
- matrix, tempMatrix: TArr;
- Begin
- half := N div 2;
- SetLength(matrix, N, N);
- SetLength(tempMatrix, half, half);
- tempMatrix := magicSquareOfOddOrder(half);
- // 1/4 матрицы
- for I := 0 to half - 1 do
- Begin
- For J := 0 to half - 1 do
- Begin
- matrix[I][J] := tempMatrix[I][J]
- End;
- End;
- // 2/4 матрицы
- for I := 0 to half - 1 do
- Begin
- For J := half to N - 1 do
- Begin
- X := J - half;
- matrix[I][J] := tempMatrix[I][X] + 2 * half * half
- End;
- End;
- // 3/4 матрицы
- for I := half to N - 1 do
- Begin
- For J := 0 to half - 1 do
- Begin
- X := I - half;
- matrix[I][J] := tempMatrix[X][J] + 3 * half * half
- End;
- End;
- // 4/4 матрицы
- for I := half to N - 1 do
- Begin
- For J := half to N - 1 do
- Begin
- X := I - half;
- Y := J - half;
- matrix[I][J] := tempMatrix[X][Y] + half * half
- End;
- End;
- Move := 0;
- For I := 6 to N - 1 do
- Begin
- If (I mod 4 <> 0) And (I mod 2 = 0) Then
- Inc(Move)
- End;
- For J := N div 2 - Move to ((N div 2) + Move - 1) do
- Begin
- For I := 0 to half - 1 do
- Begin
- Key := matrix[I][J];
- matrix[I][J] := matrix[half + I][J];
- matrix[half + I][J] := Key;
- End;
- End;
- For J := 0 to 1 do
- Begin
- If J = 0 Then
- Begin
- Key := matrix[0][0];
- matrix[0][0] := matrix[half][0];
- matrix[half][0] := Key;
- End;
- If J = 1 Then
- Begin
- Key := matrix[half - 1][0];
- matrix[half - 1][0] := matrix[N - 1][0];
- matrix[N - 1][0] := Key;
- End;
- End;
- For J := half + 1 to N - 2 do
- Begin
- For I := 1 to half - 2 do
- Begin
- Key := matrix[I][1];
- matrix[I][1] := matrix[half + I][1];
- matrix[half + I][1] := Key;
- End;
- End;
- Result := matrix;
- End;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- I, J, Size: Integer;
- magicSquare: TArr;
- begin
- Size := StrToInt(Edit1.Text);
- StringGrid1.ColCount := Size;
- StringGrid1.RowCount := Size;
- SetLength(magicSquare, Size, Size);
- If (Size mod 2 = 0) And (Size mod 4 <> 0) Then
- Begin
- magicSquare := magicSquareOfEvenOddOrder(Size);
- End;
- For I := 0 to Size - 1 do
- Begin
- for J := 0 to Size - 1 do
- Begin
- StringGrid1.Cells[J, I] := IntToStr(magicSquare[I, J]);
- End;
- End;
- N5.Enabled := True;
- end;
- procedure TForm1.Edit1Change(Sender: TObject);
- var
- num, I: Integer;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- try
- num := StrToInt(Edit1.Text);
- except
- IsCorrect := False;
- end;
- If IsCorrect And ((num > 25) Or (num <= 2)) Then
- IsCorrect := False;
- if IsCorrect And ((num < 10) And (Length(Edit1.Text) <> 1)) then
- IsCorrect := False;
- if IsCorrect And ((num mod 2 <> 0) Or (num mod 4 = 0)) Then
- IsCorrect := False;
- Button1.Enabled := IsCorrect;
- N5.Enabled := False;
- for I := 0 to StringGrid1.RowCount - 1 do
- StringGrid1.Rows[I].Clear
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- If Not(Key in ['0' .. '9', #13, #8]) Then
- Key := #0;
- If (Key = #13) And (Button1.Enabled = True) Then
- Button1.Click;
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := Application.MessageBox('Вы действительно хотите выйти?',
- 'Выход', MB_ICONQUESTION + MB_YESNO) = ID_YES
- end;
- procedure TForm1.N2Click(Sender: TObject);
- const
- Info1 = 'Разработать программу, которая генерирует магический квадрат'#13#10;
- Info2 = 'чётно-нечётной степени (6, 10, 18, …).'#13#10;
- Info3 = 'Размером квадрата является число чётно-нечётной степени, которое'#13#10;
- Info4 = 'делится на 2 и не делится на 4';
- begin
- Application.MessageBox(Info1 + Info2 + Info3 + Info4, 'Справка', 0)
- end;
- procedure TForm1.N3Click(Sender: TObject);
- begin
- Application.MessageBox('Сымоник Вадим, гр. 251004', 'Разработчик', 0)
- end;
- Function GetSize(var FileInput: TextFile): String;
- Const
- MIN_NUM = 2;
- MAX_NUM = 25;
- Var
- Size, num: Integer;
- Str: String;
- IsCorrect: Boolean;
- Begin
- Size := 0;
- Str := '';
- If Not Eof(FileInput) Then
- Begin
- IsCorrect := True;
- Try
- Read(FileInput, Size);
- Except
- MessageBox(Form1.Handle, PChar('Недопустимый размер квадрата!'),
- 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- Size := 0;
- End;
- End
- Else
- MessageBox(Form1.Handle, PChar('Недостаточно данных в файле!'),
- 'Ошибка', MB_ICONSTOP);
- If (Size > MIN_NUM) And (Size <= MAX_NUM) Then
- If (Size mod 2 = 0) And (Size mod 4 <> 0) Then
- Str := IntToStr(Size)
- Else
- Application.MessageBox('Проверьте корректность данных в файле',
- 'Ошибка', 0);
- GetSize := Str;
- End;
- procedure TForm1.N4Click(Sender: TObject);
- var
- FileInput: TextFile;
- begin
- If OpenDialog1.Execute Then
- Begin
- AssignFile(FileInput, OpenDialog1.FileName);
- Try
- Try
- Reset(FileInput);
- Edit1.Text := GetSize(FileInput);
- if Edit1.Text <> '' then
- Begin
- Button1.Click;
- End;
- Finally
- CloseFile(FileInput);
- End;
- Except
- End;
- End;
- end;
- Function Open(): String;
- begin
- with Form1 Do
- begin
- If SaveDialog1.Execute Then
- begin
- Path := SaveDialog1.FileName;
- IsFileOpen := True;
- end
- Else
- IsFileOpen := False;
- end;
- Open := Path;
- end;
- procedure TForm1.N5Click(Sender: TObject);
- var
- F: TextFile;
- I, J: Integer;
- begin
- Path := Open;
- If IsFileOpen Then
- Begin
- AssignFile(F, Path);
- Rewrite(F);
- For I := 0 to StringGrid1.RowCount - 1 do
- Begin
- for J := 0 to StringGrid1.ColCount do
- Begin
- Write(F, StringGrid1.Cells[J, I] + ' ');
- End;
- Writeln(F)
- End;
- Application.MessageBox('Данные успешно сохранены в файл',
- 'Результат', 0);
- CloseFile(F);
- End;
- end;
- procedure TForm1.N7Click(Sender: TObject);
- begin
- Form1.Close;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement