Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit Main;
- Interface
- Uses
- Winapi.Windows,
- Winapi.Messages,
- System.SysUtils,
- System.Variants,
- System.Classes,
- Vcl.Graphics,
- Clipbrd,
- Vcl.Controls,
- Vcl.Forms,
- Vcl.Dialogs,
- Vcl.StdCtrls,
- Vcl.Menus,
- Vcl.ExtCtrls,
- Vcl.Imaging.Pngimage;
- Type
- TMainForm = Class(TForm)
- ResButton: TButton;
- DiskNumber: TEdit;
- MainMenu1: TMainMenu;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- Y1: TLabel;
- Disk: TLabel;
- BalloonHint1: TBalloonHint;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- Output: TMemo;
- LabelTask: TLabel;
- Procedure ResButtonClick(Sender: TObject);
- Procedure N2Click(Sender: TObject);
- Procedure N5Click(Sender: TObject);
- Procedure DiskNumberKeyPress(Sender: TObject; Var Key: Char);
- Procedure N3Click(Sender: TObject);
- Procedure GetPath(Var Path: String);
- Procedure N4Click(Sender: TObject);
- Procedure Checker(Var IsCorrect: Boolean);
- Procedure N1Click(Sender: TObject);
- Procedure DiskNumberChange(Sender: TObject);
- Procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
- Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Procedure DiskNumberContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
- Private
- { Private declarations }
- Public
- { Public declarations }
- End;
- Const
- NumSet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #8, #44, #45, #46];
- Var
- MainForm: TMainForm;
- Implementation
- {$R *.dfm}
- Uses
- Instruction,
- Information;
- Procedure TmainForm.Checker(Var IsCorrect: Boolean);
- Begin
- IsCorrect := True;
- Try
- StrtoFloat(DiskNumber.Text);
- Except
- IsCorrect := False;
- End;
- End;
- Procedure SolveTowerOfHanoi(N, Source, Destination, Auxiliary: Integer; Output: TMemo);
- Var
- TextToAdd: String;
- Begin
- If N > 0 Then
- Begin
- SolveTowerOfHanoi(N - 1, Source, Auxiliary, Destination, Output);
- TextToAdd := 'Переместить диск ' + IntToStr(N) + ' с ' + IntToStr(Source) + ' на ' + IntToStr(Destination);
- Output.Lines.Add(TextToAdd);
- SolveTowerOfHanoi(N - 1, Auxiliary, Destination, Source, Output);
- End;
- End;
- Procedure TMainForm.ResButtonClick(Sender: TObject);
- Var
- NumDisks: Integer;
- Source, Destination, Auxiliary: Integer;
- IsCorrect: Boolean;
- Begin
- NumDisks := StrToInt(DiskNumber.Text);
- Source := 1;
- Destination := 3;
- Auxiliary := 2;
- Checker(IsCorrect);
- If IsCorrect Then
- Begin
- Output.Lines.Clear;
- SolveTowerOfHanoi(NumDisks, Source, Destination, Auxiliary, Output);
- Output.Lines.Add('Головоломка решена.');
- End
- Else
- Begin
- ShowMessage('Заполните все поля.');
- End;
- End;
- Procedure TMainForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Var
- Confirmation: Integer;
- Begin
- Confirmation := Application.MessageBox('Вы действительно хотите выйти?', 'Выход', MB_YESNO);
- CanClose := Confirmation = IDYES;
- End;
- Procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
- Begin
- If (DiskNumber.Text <> '') Then
- ResButton.Enabled := True
- Else
- ResButton.Enabled := False;
- End;
- Procedure TMainForm.DiskNumberChange(Sender: TObject);
- Var
- Value: Double;
- Begin
- If TryStrToFloat(DiskNumber.Text, Value) Then
- Begin
- If (Value < 1) Then
- Begin
- BalloonHint1.Title := 'Неверное значение.';
- BalloonHint1.Description := 'N должно быть больше 0.';
- BalloonHint1.ImageIndex := 1;
- BalloonHint1.ShowHint(DiskNumber);
- DiskNumber.Text := '';
- End;
- End;
- DiskNumber.MaxLength := 5;
- If Length(DiskNumber.Text) = 0 Then
- Begin
- Output.Lines.Clear;
- End;
- End;
- Procedure TMainForm.DiskNumberContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
- Begin
- Handled := True;
- End;
- Procedure TMainForm.DiskNumberKeyPress(Sender: TObject; Var Key: Char);
- Begin
- If Key In NumSet Then
- Begin
- If Key = '0' Then
- Begin
- If Length(DiskNumber.Text) = 1 Then
- Begin
- If DiskNumber.Text[1] In ['0'] Then
- Begin
- Key := #0;
- End;
- End;
- If Length(DiskNumber.Text) = 2 Then
- Begin
- If (DiskNumber.Text[1] = '-') AND (DiskNumber.Text[2] = '0') Then
- Begin
- Key := #0;
- End;
- End;
- End;
- If Key = #45 Then
- Begin
- If Length(DiskNumber.Text) > 0 Then
- Begin
- BalloonHint1.Title := 'Недопустимый символ.';
- BalloonHint1.Description := 'Здесь можно ввести только число.';
- BalloonHint1.ImageIndex := 1;
- BalloonHint1.ShowHint(DiskNumber);
- Key := #0;
- End;
- End;
- If Key In [',', '.'] Then
- Begin
- Key := FormatSettings.DecimalSeparator;
- If Pos(Key, DiskNumber.Text) <> 0 Then
- Begin
- BalloonHint1.Title := 'Недопустимый символ.';
- BalloonHint1.Description := 'Здесь можно ввести только число.';
- BalloonHint1.ImageIndex := 1;
- BalloonHint1.ShowHint(DiskNumber);
- Key := #0;
- End
- Else
- If Length(DiskNumber.Text) > 0 Then
- Begin
- If (DiskNumber.Text[1] = '-') AND (Length(DiskNumber.Text) = 1) Then
- Begin
- Key := #0;
- End
- End
- Else
- If DiskNumber.SelStart = 0 Then
- Begin
- DiskNumber.Text := '0' + DiskNumber.Text;
- DiskNumber.SelStart := 1;
- End;
- End;
- End
- Else
- Begin
- BalloonHint1.Title := 'Недопустимый символ.';
- BalloonHint1.Description := 'Здесь можно ввести только число.';
- BalloonHint1.ShowHint(DiskNumber);
- Key := #0;
- End;
- End;
- Procedure TMainForm.N1Click(Sender: TObject);
- Begin
- If Output.Lines.Count = 0 Then
- Begin
- N4.Enabled := False;
- End
- Else
- Begin
- N4.Enabled := True;
- End;
- End;
- Procedure TMainForm.N2Click(Sender: TObject);
- Begin
- InstructionForm.ShowModal;
- End;
- Procedure TMainForm.GetPath(Var Path: String);
- Begin
- If OpenDialog1.Execute Then
- Begin
- Path := OpenDialog1.FileName;
- End
- Else
- Begin
- ShowMessage('Открытие файла отменено!');
- End;
- End;
- Procedure TMainForm.N3Click(Sender: TObject);
- Var
- IsCorrect, Fail: Boolean;
- Path, Number, Str: String;
- Count: Integer;
- FileElem: TextFile;
- Begin
- GetPath(Path);
- AssignFile(FileElem, Path);
- Fail := False;
- IsCorrect := True;
- Count := 0;
- If Not FileExists(Path) Then
- Begin
- ShowMessage('Файл не найден!');
- End
- Else
- Begin
- Try
- Try
- Reset(FileElem);
- Except
- CloseFile(FileElem);
- IsCorrect := False;
- ShowMessage('Ошибка открытия файла.');
- End;
- If IsCorrect Then
- Begin
- While Not Eof(FileElem) Do
- Begin
- ReadLn(FileElem, Str);
- Inc(Count);
- End;
- CloseFile(FileElem);
- If Count <> 1 Then
- Fail := True;
- End;
- If IsCorrect Then
- Begin
- Reset(FileElem);
- Try
- Readln(FileElem, Number);
- If Not TryStrToInt(Number, Count) Then
- Fail := True;
- Except
- Fail := True;
- End;
- End;
- Finally
- CloseFile(FileElem);
- If Fail Then
- ShowMessage('Проверьте введенные данные и повторите попытку!')
- Else
- Begin
- DiskNumber.Text := Number;
- End;
- ShowMessage('Обработка завершена.');
- End;
- End;
- End;
- Procedure TMainForm.N4Click(Sender: TObject);
- Begin
- If SaveDialog1.Execute Then
- Begin
- Output.Lines.SaveToFile(SaveDialog1.FileName);
- End
- Else
- Begin
- ShowMessage('Сохранение в файл отменено.');
- End;
- End;
- Procedure TMainForm.N5Click(Sender: TObject);
- Begin
- InformationForm.ShowModal;
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement