Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit MagicSquareMain;
- Interface
- Uses
- Winapi.Windows,
- Winapi.Messages,
- System.SysUtils,
- System.Variants,
- System.Classes,
- Vcl.Graphics,
- Vcl.Controls,
- Vcl.Forms,
- Vcl.Dialogs,
- Vcl.StdCtrls,
- Vcl.ExtCtrls,
- Vcl.Buttons,
- System.ImageList,
- Vcl.ImgList,
- Vcl.VirtualImageList,
- Vcl.Menus,
- Vcl.Grids,
- Vcl.ComCtrls,
- ClipBrd;
- Type
- TMatrix = Array Of Array Of Integer;
- TMagicSquareControl = Class
- Size, MagicSum: Integer;
- MagicSquare: TMatrix;
- Procedure FindMagicSquare();
- End;
- TLabeledEdit = Class(Vcl.ExtCtrls.TLabeledEdit)
- Protected
- Procedure WMPaste(Var Msg: TMessage); Message WM_PASTE;
- End;
- TfrmMain = Class(TForm)
- BalloonHint: TBalloonHint;
- VilImages_48: TVirtualImageList;
- SvdSaveToFileDialog: TSaveDialog;
- MmMainMenu: TMainMenu;
- MiFileMenu: TMenuItem;
- MiOpenFromFile: TMenuItem;
- MiSaveToFile: TMenuItem;
- MiHelp: TMenuItem;
- MiInfoAboutDeveloper: TMenuItem;
- OpdOpenFromFileDialog: TOpenDialog;
- PgcPages: TPageControl;
- TbstWelcome: TTabSheet;
- TbstMagicSquare: TTabSheet;
- PButtons: TPanel;
- SdbtOpenFromFile: TSpeedButton;
- SdbtStart: TSpeedButton;
- SdbtSaveToFile: TSpeedButton;
- SdbtHelp: TSpeedButton;
- PBack: TPanel;
- LbWelcome: TLabel;
- LbTaskInfo: TLabel;
- LbIncorrectPatricipants: TLabel;
- LbFinalResult: TLabel;
- LbeSize: TLabeledEdit;
- BtConfirmSize: TButton;
- BtFind: TButton;
- StrgrMagicSquare: TStringGrid;
- LbResult: TLabel;
- BtPreviousPage: TButton;
- LbAnswer: TLabel;
- LbMagicSum: TLabel;
- Procedure FormCreate(Sender: TObject);
- Procedure MiOpenFromFileClick(Sender: TObject);
- Procedure BtConfirmSizeClick(Sender: TObject);
- Procedure SdbtOpenFromFileClick(Sender: TObject);
- Procedure LbeSizeChange(Sender: TObject);
- Procedure LbeSizeKeyPress(Sender: TObject; Var Key: Char);
- Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Procedure MiHelpClick(Sender: TObject);
- Procedure MiInfoAboutDeveloperClick(Sender: TObject);
- Procedure SdbtHelpClick(Sender: TObject);
- Procedure BtPreviousPageClick(Sender: TObject);
- Procedure BtFindClick(Sender: TObject);
- Procedure SdbtStartClick(Sender: TObject);
- Procedure MiSaveToFileClick(Sender: TObject);
- End;
- Var
- FrmMain: TfrmMain;
- SquareControl: TMagicSquareControl;
- Implementation
- {$R *.dfm}
- Uses MagicSquareData;
- Function IsFileOfNumberCorrect(Path: String): Boolean; Stdcall; External 'anticlown.dll';
- Procedure GetNumberFromFile(Path: String; Var Size: Integer; Var IsCorrect: Boolean); Stdcall;
- External 'anticlown.dll';
- Procedure TMagicSquareControl.FindMagicSquare();
- Var
- I, J: Integer;
- Begin
- MagicSum := (Size * (Size * Size + 1)) Div 2;
- For I := Low(MagicSquare) To High(MagicSquare) Do
- For J := Low(MagicSquare) To High(MagicSquare) Do
- MagicSquare[I, J] := Size * I + J + 1;
- For I := Low(MagicSquare) To Size Div 4 - 1 Do
- For J := Low(MagicSquare) To Size Div 4 - 1 Do
- MagicSquare[I, J] := Size * Size + 1 - MagicSquare[I, J];
- For I := Low(MagicSquare) To Size Div 4 - 1 Do
- For J := 3 * Size Div 4 To High(MagicSquare) Do
- MagicSquare[I, J] := Size * Size + 1 - MagicSquare[I, J];
- For I := 3 * Size Div 4 To High(MagicSquare) Do
- For J := Low(MagicSquare) To Size Div 4 - 1 Do
- MagicSquare[I, J] := Size * Size + 1 - MagicSquare[I, J];
- For I := 3 * Size Div 4 To High(MagicSquare) Do
- For J := 3 * Size Div 4 To High(MagicSquare) Do
- MagicSquare[I, J] := Size * Size + 1 - MagicSquare[I, J];
- For I := Size Div 4 To 3 * Size Div 4 - 1 Do
- For J := Size Div 4 To 3 * Size Div 4 - 1 Do
- MagicSquare[I, J] := Size * Size + 1 - MagicSquare[I, J];
- End;
- Procedure TfrmMain.FormCreate(Sender: TObject);
- Begin
- PgcPages.ActivePageIndex := 0;
- SquareControl := TMagicSquareControl.Create;
- End;
- Procedure TfrmMain.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Begin
- If Application.MessageBox(PChar('Вы уверены, что хотите выйти?'), PChar('Выход'),
- MB_ICONQUESTION + MB_YESNO + MB_DEFBUTTON1 + MB_TASKMODAL) = IDYES Then
- CanClose := True
- Else
- CanClose := False;
- End;
- Procedure TfrmMain.MiHelpClick(Sender: TObject);
- Const
- FIRST_MESSAGE = '- Вводимыми значениями могут являться только целые числа!' + #13#10;
- SECOND_MESSAGE = '- Диапазон ввода размера квадрата: 4...32' + #13#10;
- THIRD_MESSAGE = '- Для ввода из файла используйте вкладку ''Файл'' - ''Открыть''.' + #13#10;
- FOURTH_MESSAGE =
- '- Для сохранения в файл используйте вкладку ''Файл'' - ''Сохранить''.' + #13#10;
- FIFTH_MESSAGE =
- '- Для удобного использования программы представлен набор кнопок на левой панели.';
- Begin
- Application.MessageBox(FIRST_MESSAGE + SECOND_MESSAGE + THIRD_MESSAGE + FOURTH_MESSAGE +
- FIFTH_MESSAGE, 'Справка');
- End;
- Procedure TfrmMain.MiInfoAboutDeveloperClick(Sender: TObject);
- Const
- FIRST_MESSAGE = 'Ф.И.О.: Карась А.С. a.k.a Clownfish' + #13#10;
- SECOND_MESSAGE = 'Группа: 251004' + #13#10;
- THIRD_MESSAGE = 'Контакты: предварительная запись вживую по адресу' + #13#10;
- FOURTH_MESSAGE = 'г.Гродно, ул.Мостовая, д.31';
- Begin
- Application.MessageBox(FIRST_MESSAGE + SECOND_MESSAGE + THIRD_MESSAGE + FOURTH_MESSAGE,
- 'О разработчике');
- End;
- Procedure TfrmMain.MiOpenFromFileClick(Sender: TObject);
- Var
- IsCorrect: Boolean;
- Size: Integer;
- Begin
- If OpdOpenFromFileDialog.Execute() Then
- If IsFileOfNumberCorrect(OpdOpenFromFileDialog.FileName) Then
- Begin
- GetNumberFromFile(OpdOpenFromFileDialog.FileName, Size, IsCorrect);
- If (IsCorrect) Then
- Begin
- LbeSize.Text := IntToStr(Size);
- BtConfirmSizeClick(Sender);
- End;
- End
- Else
- Application.MessageBox('Данные в файле некорректны, попробуйте ещё раз.', 'Ошибка!',
- MB_ICONERROR);
- End;
- Procedure TfrmMain.SdbtHelpClick(Sender: TObject);
- Begin
- MiHelpClick(Sender);
- End;
- Procedure TfrmMain.SdbtOpenFromFileClick(Sender: TObject);
- Begin
- MiOpenFromFileClick(Sender);
- End;
- Procedure TLabeledEdit.WMPaste(Var Msg: TMessage);
- Const
- MIN_VALUE = 4;
- MAX_VALUE = 32;
- Begin
- If Clipboard.HasFormat(CF_TEXT) Then
- Begin
- Try
- If (StrToInt(Clipboard.AsText) < MIN_VALUE) Or
- (StrToInt(Clipboard.AsText) > MAX_VALUE) Then
- Begin
- Application.MessageBox(Pchar('В буфере обмена содержится неподходящее значение!'),
- 'Ошибка', MB_ICONWARNING);
- Exit;
- End;
- Except
- Application.MessageBox(Pchar('При чтении из буфера произошла ошибка!'), 'Ошибка',
- MB_ICONWARNING);
- Exit;
- End;
- End
- Else
- Begin
- Application.MessageBox(Pchar('В буфере обмена содержатся некорректные данные!'), 'Ошибка',
- MB_ICONWARNING);
- Exit;
- End;
- Inherited;
- End;
- Procedure TfrmMain.LbeSizeChange(Sender: TObject);
- Const
- MIN_VALUE = 4;
- MAX_VALUE = 32;
- Var
- Temp: Integer;
- Begin
- BtFind.Enabled := False;
- If (LbeSize.Text <> '') Then
- Begin
- Temp := StrToInt(LbeSize.Text);
- If ((Temp < MIN_VALUE) Or (Temp > MAX_VALUE) Or (Temp And (Temp - 1) <> 0)) Then
- Begin
- LbIncorrectPatricipants.Visible := True;
- BtConfirmSize.Enabled := False;
- End
- Else
- Begin
- LbIncorrectPatricipants.Visible := False;
- BtConfirmSize.Enabled := True;
- End;
- End
- Else
- Begin
- BtConfirmSize.Enabled := False;
- SdbtStart.Enabled := False;
- End;
- End;
- Procedure TfrmMain.LbeSizeKeyPress(Sender: TObject; Var Key: Char);
- Begin
- If (Not(Key In ['0' .. '9', #08, #127])) Then
- Key := #0;
- If (LbeSize.Text = '0') And (Key = '0') Then
- Key := #0;
- End;
- Procedure TfrmMain.BtConfirmSizeClick(Sender: TObject);
- Begin
- SquareControl.Size := StrToInt(LbeSize.Text);
- SetLength(SquareControl.MagicSquare, SquareControl.Size, SquareControl.Size);
- Case SquareControl.Size Of
- 4:
- With StrgrMagicSquare Do
- Begin
- Left := 280;
- DefaultColWidth := 40;
- DefaultRowHeight := 40;
- StrgrMagicSquare.Width := StrgrMagicSquare.DefaultRowHeight * SquareControl.Size +
- (SquareControl.Size + 3);
- StrgrMagicSquare.Height := StrgrMagicSquare.Width;
- End;
- 8:
- With StrgrMagicSquare Do
- Begin
- Left := 230;
- DefaultColWidth := 30;
- DefaultRowHeight := 30;
- StrgrMagicSquare.Width := StrgrMagicSquare.DefaultColWidth * SquareControl.Size +
- (SquareControl.Size + 3);
- StrgrMagicSquare.Height := StrgrMagicSquare.Width;
- End;
- Else
- With StrgrMagicSquare Do
- Begin
- Left := 30;
- DefaultColWidth := 40;
- DefaultRowHeight := 30;
- StrgrMagicSquare.Width := StrgrMagicSquare.DefaultColWidth * SquareControl.Size +
- (SquareControl.Size + 3);
- StrgrMagicSquare.Height := 400;
- End;
- End;
- StrgrMagicSquare.RowCount := SquareControl.Size;
- StrgrMagicSquare.ColCount := SquareControl.Size;
- BtFind.Enabled := True;
- BtFind.Visible := True;
- SdbtStart.Enabled := True;
- End;
- Procedure TfrmMain.SdbtStartClick(Sender: TObject);
- Begin
- BtFindClick(Sender);
- End;
- Procedure TfrmMain.BtFindClick(Sender: TObject);
- Var
- I, J: Integer;
- Begin
- PgcPages.ActivePageIndex := 1;
- TbstMagicSquare.Enabled := True;
- TbstWelcome.Enabled := False;
- BtPreviousPage.Enabled := True;
- BtFind.Default := False;
- BtPreviousPage.Default := True;
- MiSaveToFile.Enabled := True;
- SdbtSaveToFile.Enabled := True;
- Case SquareControl.Size Of
- 4:
- PgcPages.Height := 400;
- 8:
- PgcPages.Height := 500;
- Else
- PgcPages.Height := 600;
- End;
- SquareControl.FindMagicSquare;
- LbMagicSum.Caption := IntToStr(SquareControl.MagicSum);
- For I := Low(SquareControl.MagicSquare) To High(SquareControl.MagicSquare) Do
- For J := Low(SquareControl.MagicSquare) To High(SquareControl.MagicSquare) Do
- StrgrMagicSquare.Cells[J, I] := IntToStr(SquareControl.MagicSquare[I, J]);
- BtPreviousPage.SetFocus;
- End;
- Procedure TfrmMain.MiSaveToFileClick(Sender: TObject);
- Var
- OutputFile: TextFile;
- I, J: Integer;
- Begin
- If SvdSaveToFileDialog.Execute() And FileExists(SvdSaveToFileDialog.FileName) Then
- Begin
- AssignFile(OutputFile, SvdSaveToFileDialog.FileName);
- Try
- Try
- Rewrite(OutputFile);
- Writeln(OutputFile, 'Входные данные: ');
- Writeln(OutputFile, 'Размер квадрата - ', SquareControl.Size);
- Write(OutputFile, 'Ответ: ' + #13#10);
- Writeln(OutputFile, 'Магическая сумма - ', SquareControl.MagicSum);
- Writeln(OutputFile, 'Магический квадрат:');
- For I := Low(SquareControl.MagicSquare) To High(SquareControl.MagicSquare) Do
- Begin
- For J := Low(SquareControl.MagicSquare) To High(SquareControl.MagicSquare) Do
- Write(OutputFile, SquareControl.MagicSquare[I, J], ' ');
- Write(OutputFile, #13#10);
- End;
- Application.MessageBox('Данные успешно записаны в файл!', 'Сохранение',
- MB_ICONINFORMATION);
- Finally
- CloseFile(OutputFile);
- End;
- Except
- Application.MessageBox('Отказано в доступе! Измените параметры файла! ', 'Ошибка!',
- MB_ICONERROR);
- End;
- End
- Else
- Application.MessageBox('Введено некорректное имя файла или закрыто окно сохранения!',
- 'Ошибка!', MB_ICONERROR);
- End;
- Procedure TfrmMain.BtPreviousPageClick(Sender: TObject);
- Begin
- PgcPages.ActivePageIndex := 0;
- TbstWelcome.Enabled := True;
- TbstMagicSquare.Enabled := False;
- BtPreviousPage.Enabled := False;
- BtFind.Default := True;
- PgcPages.Height := 372;
- End;
- End.
- Unit MagicSquareData;
- Interface
- Uses
- System.SysUtils,
- System.Classes, Vcl.BaseImageCollection, Vcl.ImageCollection,
- System.ImageList, Vcl.ImgList, Vcl.Controls;
- Type
- TdtmdPictures = Class(TDataModule)
- imcImages: TImageCollection;
- imlIcons: TImageList;
- End;
- Var
- DtmdPictures: TdtmdPictures;
- Implementation
- {%CLASSGROUP 'Vcl.Controls.TControl'}
- {$R *.dfm}
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement