Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Grids,
- Vcl.Menus;
- type
- TMatrix = Array Of Array Of Integer;
- TMainForm = class(TForm)
- SizeEdit: TEdit;
- UpDownSIze: TUpDown;
- CountButton: TButton;
- PopupMenu1: TPopupMenu;
- MainMenu1: TMainMenu;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- StringGrid1: TStringGrid;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- N7: TMenuItem;
- N11: TMenuItem;
- N12: TMenuItem;
- procedure SizeEditChange(Sender: TObject);
- procedure N4Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure N7Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- {$R *.dfm}
- procedure FillMainMatrix(var Matrix : TMatrix);
- var
- I, J , Count : Integer;
- Begin
- Count := 1;
- For I := 0 To High(Matrix) Do
- Begin
- For J := 0 To High(Matrix) Do
- Begin
- Matrix[I, J] := Count;
- Inc(Count);
- End;
- End;
- End;
- procedure FillHelpMatrix(var Matrix : TMatrix);
- var
- I, J , Count : Integer;
- Begin
- Count := 1;
- For I := High(Matrix) DownTo 0 Do
- Begin
- For J := High(Matrix) DownTo 0 Do
- Begin
- Matrix[I, J] := Count;
- Inc(Count);
- End;
- End;
- End;
- Procedure ChangeNumbers(var Matrix, TempHelpMatrix : TMatrix);
- var
- I, Count : Integer;
- Begin
- For I := 0 To High(Matrix) Do
- Begin
- Matrix[I, I] := TempHelpMatrix[I, I];
- End;
- For I := 0 To High(Matrix) Do
- Begin
- Matrix[I, High(Matrix) - I] := TempHelpMatrix[I, High(Matrix) - I];
- End;
- End;
- procedure Count(SizeEdit : TEdit; var StringGrid1 : TStringGrid);
- var
- Size : Integer;
- MainMatrix, HelpMatrix : TMatrix;
- I,J,K,L : Integer;
- TempMatrix : TMatrix;
- TempHelpMatrix : TMatrix;
- begin
- Size := StrToInt(SizeEdit.Text);
- SetLength(TempMatrix, 4, 4);
- SetLength(MainMatrix, Size, Size);
- SetLength(HelpMatrix, Size, Size);
- SetLength(TempHelpMatrix, 4, 4);
- FillMainMatrix(MainMatrix);
- FillHelpMatrix(HelpMatrix);
- I := 0;
- J := 0;
- While I <= (Size - 4) Do
- Begin
- While J <= (Size - 4) Do
- Begin
- For K := 0 To 3 Do
- Begin
- For L := 0 To 3 Do
- Begin
- TempMatrix[K, L] := MainMatrix[I+K, J+L];
- TempHelpMatrix[K, L] := HelpMatrix[I+K, J+L];
- End;
- End;
- ChangeNumbers(TempMatrix, TempHelpMatrix);
- For K := 0 To 3 Do
- Begin
- For L := 0 To 3 Do
- Begin
- MainMatrix[I+K, J+L] := TempMatrix[K, L];
- End;
- End;
- Inc(J, 4);
- End;
- J := 0;
- Inc(I, 4);
- End;
- For I := 0 To High(MainMatrix) Do
- Begin
- For J := 0 To High(MainMatrix) Do
- Begin
- StringGrid1.Cells[J, I] := IntToStr(MainMatrix[I, J]);
- End;
- End;
- end;
- procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
- var
- DlgRes:integer;
- begin
- DlgRes := MessageBox(MainForm.Handle, 'Закрыть программу?', 'Сообщение', MB_YESNO or MB_ICONQUESTION);
- if DlgRes = ID_NO then
- action :=caNone;
- end;
- procedure TMainForm.N3Click(Sender: TObject);
- begin
- Application.MessageBox('Непесов Рустам, 351004, @dxvmx', 'О разработчике');
- end;
- procedure TMainForm.N4Click(Sender: TObject);
- Var
- Num : Integer;
- IsCorrect : Boolean;
- InputFile : TextFile;
- Str : String;
- begin
- If(OpenDialog1.Execute) Then
- Begin
- Try
- AssignFile(InputFile, OpenDialog1.FileName);
- Reset(InputFile);
- Except
- Application.MessageBox('Ошибка при открытии файла!', 'Ошибка!');
- Exit;
- End;
- IsCorrect := True;
- Try
- Readln(InputFile, Str);
- Num := StrToInt(Str);
- Except
- IsCorrect := False;
- End;
- If(Not IsCorrect) Or (Num mod 4 <> 0) Or (Num < 4) Or (Num > 100) Then
- Begin
- Application.MessageBox('Ошибка при записи данных из файла! Учтите, что число должно быть кратным 4 и от 4 до 100.','Ошибка');
- CloseFile(InputFile);
- Exit;
- End;
- CloseFile(InputFile);
- SizeEdit.Text := IntToStr(Num);
- End;
- end;
- procedure TMainForm.N5Click(Sender: TObject);
- var
- OutputFile : TextFile;
- I, J : Integer;
- begin
- If(SaveDialog1.Execute) Then
- Begin
- Try
- AssignFile(OutputFile, SaveDialog1.FileName);
- Rewrite(OutputFile);
- Except
- Application.MessageBox('Ошибка при записи в файл!', 'Ошибка!');
- Exit;
- End;
- Writeln(OutputFile, 'Магический квадрат порядка ' + SizeEdit.Text + ' :');
- For I := 0 To (StringGrid1.ColCount - 1) Do
- Begin
- For J := 0 To (StringGrid1.ColCount - 1) Do
- Begin
- Write(OutputFile, StringGrid1.Cells[J, I], ' ');
- End;
- Writeln(OutputFile);
- End;
- CloseFile(OutputFile);
- Application.MessageBox('Данные успешно записаны в файл!', 'Успех!');
- End;
- end;
- procedure TMainForm.N7Click(Sender: TObject);
- begin
- MainForm.Close;
- end;
- procedure TMainForm.SizeEditChange(Sender: TObject);
- begin
- If(StrToInt(SizeEdit.Text) < 4) then
- SizeEdit.Text := '4';
- StringGrid1.RowCount := StrToInt(SizeEdit.Text);
- StringGrid1.ColCount := StrToInt(SizeEdit.Text);
- Count(SizeEdit, StringGrid1);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement