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.Menus, Vcl.StdCtrls, Vcl.Grids,
- Vcl.Samples.Spin;
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- FileButton: TMenuItem;
- AboutButton: TMenuItem;
- ReadButton: TMenuItem;
- SaveButton: TMenuItem;
- LSize: TLabel;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- SGMatrix: TStringGrid;
- Start: TButton;
- ESize: TSpinEdit;
- procedure AboutButtonClick(Sender: TObject);
- procedure OpenDialog1CanClose(Sender: TObject; var CanClose: Boolean);
- procedure SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean);
- procedure SetSize;
- procedure ReadButtonClick(Sender: TObject);
- procedure SaveButtonClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure StartClick(Sender: TObject);
- procedure ESizeChange(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- TMatrix = array of array of Integer;
- var
- Form1: TForm1;
- Size: Integer;
- Ans: Real;
- Matrix, TempMatrix: TMatrix;
- const
- MIN_SIZE = 2;
- MAX_SIZE = 32;
- implementation
- {$R *.dfm}
- procedure TForm1.SetSize;
- var
- ErrMsg: String;
- Err: Integer;
- begin
- val(ESize.Text, Size, Err);
- Size := StrToInt(ESize.Text);
- SGMatrix.ColCount := Size;
- if Size = 2 then
- begin
- SGMatrix.DefaultColWidth := 50;
- SGMatrix.Width := (SGMatrix.DefaultColWidth + 6) * Size;
- SGMatrix.RowCount := Size;
- SGMatrix.DefaultRowHeight := 20;
- SGMatrix.Height := (SGMatrix.DefaultRowHeight + 6) * Size;
- end
- else
- begin
- SGMatrix.DefaultColWidth := 50;
- SGMatrix.Width := (SGMatrix.DefaultColWidth + 2) * Size;
- SGMatrix.RowCount := Size;
- SGMatrix.DefaultRowHeight := 20;
- SGMatrix.Height := (SGMatrix.DefaultRowHeight + 2) * Size;
- end;
- SGMatrix.Visible := True;
- end;
- procedure FillMatrix;
- var
- i, j, Count: Integer;
- begin
- Count := 0;
- for i := 0 to High(Matrix) do
- for j := 0 to High(Matrix) do
- begin
- Inc(Count);
- Matrix[i, j] := Count;
- end;
- end;
- procedure FillMatrixTemp;
- var
- i, j, Count: Integer;
- begin
- Count := sqr(Size);
- for i := 0 to Size - 1 do
- for j := 0 to Size - 1 do
- begin
- TempMatrix[i, j] := Count;
- Dec(Count);
- end;
- end;
- procedure TForm1.StartClick(Sender: TObject);
- var
- Count, i, j, Temp, a, b: Integer;
- begin
- Form1.SetSize;
- SetLength(Matrix, Size, Size);
- SetLength(TempMatrix, Size, Size);
- FillMatrix;
- FillMatrixTemp;
- for i := 0 to Size - 1 do
- for j := 0 to Size - 1 do
- begin
- a := (i + 1) mod 4;
- b := (j + 1) mod 4;
- if ((a = 1) and (b = 0)) or ((a = 0) and (b = 1)) or ((a = 2) and (b = 3))
- or ((a = 3) and (b = 2)) or (a = b) then
- else
- Matrix[i, j] := TempMatrix[i, j];
- end;
- for i := 0 to Size - 1 do
- for j := 0 to Size - 1 do
- SGMatrix.Cells[j, i] := IntToStr(Matrix[i, j]);
- SaveButton.Enabled := True;
- end;
- procedure TForm1.ESizeChange(Sender: TObject);
- begin
- SGMatrix.Visible := False;
- SaveButton.Enabled := False;
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageDlg('Вы уверены, что хотите выйти из программы?' +
- #10#13 + 'Все несохраненные данные будут утеряны.',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes;
- end;
- procedure TForm1.AboutButtonClick(Sender: TObject);
- var
- Task: String;
- begin
- Task := 'Данная программа генерирует магический квадрат' + #10#13 + '(Размер кратен 4)' + #10#13;
- Task := Task + 'Автор - Пестунов Илья, гр. 051007';
- MessageDlg(Task, mtInformation, [mbOK], 0);
- end;
- procedure TForm1.ReadButtonClick(Sender: TObject);
- var
- MyFile: TextFile;
- i, Value, j: Integer;
- begin
- ESize.Text := '';
- SGMatrix.Visible := False;
- Start.Enabled := True;
- with SGMatrix do
- for i := 0 to Size do
- Cols[i].Clear;
- if OpenDialog1.Execute then
- begin
- AssignFile(MyFile, OpenDialog1.FileName);
- Reset(MyFile);
- Read(MyFile, Size);
- ESize.Text := IntToStr(Size);
- SGMatrix.ColCount := Size;
- if Size = 2 then
- begin
- SGMatrix.Width := (SGMatrix.DefaultColWidth + 6) * (Size);
- SGMatrix.RowCount := Size;
- SGMatrix.DefaultRowHeight := 20;
- SGMatrix.Height := (SGMatrix.DefaultRowHeight + 6) * Size;
- end
- else
- begin
- SGMatrix.DefaultColWidth := 50;
- SGMatrix.Width := (SGMatrix.DefaultColWidth + 2) * (Size);
- SGMatrix.RowCount := Size;
- SGMatrix.DefaultRowHeight := 20;
- SGMatrix.Height := (SGMatrix.DefaultRowHeight + 2) * Size;
- end;
- CloseFile(MyFile);
- SGMatrix.Visible := True;
- end;
- end;
- procedure TForm1.SaveButtonClick(Sender: TObject);
- var
- MyFile: TextFile;
- i, j: Integer;
- begin
- if SaveDialog1.Execute then
- begin
- AssignFile(MyFile, SaveDialog1.FileName);
- Rewrite(MyFile);
- Writeln(MyFile, 'Размер магического квадрата: ', Size);
- Writeln(MyFile, 'Магический квадрат: ');
- for i := 0 to SGMatrix.RowCount do
- begin
- for j := 0 to SGMatrix.ColCount do
- begin
- Write(MyFile, SGMatrix.Cells[j, i]:4, ' ');
- end;
- Writeln(MyFile);
- end;
- CloseFile(MyFile);
- MessageDlg('Результат успешно сохранён', mtCustom, [mbOK], 0);
- end;
- end;
- procedure TForm1.OpenDialog1CanClose(Sender: TObject; var CanClose: Boolean);
- var
- IsValid: Boolean;
- N, i, Value, Err, j: Integer;
- MyFile: TextFile;
- Check: String;
- const
- Digit: set of Char = ['1'..'9', '0', ' ', '-'];
- begin
- IsValid := True;
- N := Length(OpenDialog1.FileName);
- if (OpenDialog1.FileName[N] = 't') and (OpenDialog1.FileName[N - 1] = 'x') and (OpenDialog1.FileName[N - 2] = 't') then
- begin
- AssignFile(MyFile, OpenDialog1.FileName);
- Reset(MyFile);
- Read(MyFile, Check);
- CloseFile(MyFile);
- if Length(Check) = 0 then
- begin
- MessageDlg('Файл пуст', mtWarning, [mbOK], 0);
- IsValid := False;
- end
- else
- begin
- AssignFile(MyFile, OpenDialog1.FileName);
- Reset(MyFile);
- try
- Readln(MyFile, Size);
- except
- IsValid := False;
- MessageDlg('Размера магического квадрата должен быть натуральным числом до 32, кратным 4', mtWarning, [mbOK], 0);
- end;
- if ((IsValid) and (Size < MIN_SIZE)) or ((IsValid) and (Size > MAX_SIZE)) or (Size mod 4 <> 0) then
- begin
- IsValid := False;
- MessageDlg('Размера магического квадрата должен быть натуральным числом до 32, кратным 4', mtError, [mbOK], 0);
- end;
- end;
- CloseFile(MyFile);
- end
- else
- begin
- MessageDlg('Файл должен иметь расширение .txt', mtError, [mbOK], 0);
- IsValid := False;
- end;
- if not(IsValid) then
- CanClose := False
- else
- begin
- ESize.Text := IntToStr(Size);
- Form1.Start.Click;
- end;
- end;
- procedure TForm1.SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean);
- var
- N: Integer;
- begin
- N := Length(SaveDialog1.FileName);
- if (SaveDialog1.FileName[N] = 't') and (SaveDialog1.FileName[N - 1] = 'x') and (SaveDialog1.FileName[N - 2] = 't') then
- CanClose := True
- else
- begin
- CanClose := False;
- MessageDlg('Файл должен иметь расширение .txt', mtError, [mbOK], 0);
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement