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,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Menus;
- type
- TArr = array of Byte;
- TArrOfTheSets = array[0..1000] of Int64;
- TLab_5_2 = class(TForm)
- edAmountCells: TEdit;
- lbInf: TLabel;
- edAmountWays: TEdit;
- shLine: TShape;
- btnCountMoves: TButton;
- lbAmountMoves: TLabel;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- Open1: TMenuItem;
- Save1: TMenuItem;
- Help1: TMenuItem;
- Aboutauthor1: TMenuItem;
- Help2: TMenuItem;
- OpenFile: TOpenDialog;
- SaveFile: TSaveDialog;
- procedure edAmountCellsKeyPress(Sender: TObject; var Key: Char);
- procedure edAmountCellsKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure edAmountCellsKeyUp(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure btnCountMovesClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure edAmountCellsChange(Sender: TObject);
- procedure Open1Click(Sender: TObject);
- procedure Save1Click(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure Aboutauthor1Click(Sender: TObject);
- procedure Help2Click(Sender: TObject);
- function IsCorrectNumber(strNumber: string): Boolean;
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- TCount = class
- private
- Arr: TArr;
- ArrOfTheSets: TArrOfTheSets;
- procedure SetArrWithZeros(var ArrOfTheSets: TArrOfTheSets);
- function CountAmountMoves(Arr: TArr): Int64;
- public
- function FindAmountMoves(N: Integer): Int64;
- end;
- var
- Lab_5_2: TLab_5_2;
- CountObj: TCount;
- implementation
- {$R *.dfm}
- procedure TCount.SetArrWithZeros(var ArrOfTheSets: TArrOfTheSets);
- var
- index: Word;
- begin
- for index := 0 to Length(ArrOfTheSets) do
- ArrOfTheSets[index] := 0;
- end;
- function TCount.CountAmountMoves(Arr: TArr): Int64;
- begin
- if (Length(Arr) = 1) or (Length(Arr) = 0) then
- Result := 1
- else
- begin
- if ArrOfTheSets[Length(Arr)] = 0 then
- ArrOfTheSets[Length(Arr)] := CountAmountMoves(copy(Arr, 1, Length(Arr))) + CountAmountMoves(copy(Arr, 2, Length(Arr)));
- Result := ArrOfTheSets[Length(Arr)];
- end;
- end;
- function TCount.FindAmountMoves(N: Integer): Int64;
- begin
- SetLength(Arr, N);
- SetArrWithZeros(ArrOfTheSets);
- ArrOfTheSets[0] := 1;
- ArrOfTheSets[1] := 1;
- Result := CountAmountMoves(Arr);
- end;
- procedure TLab_5_2.Aboutauthor1Click(Sender: TObject);
- begin
- MessageBox(Handle, PChar('The author of this program id Vladislav Mironuk (851001)'),
- PChar('Error.'), MB_ICONINFORMATION);
- end;
- procedure TLab_5_2.btnCountMovesClick(Sender: TObject);
- var
- Arr: TArr;
- begin
- edAmountWays.Text := IntToStr(CountObj.FindAmountMoves(StrToInt(edAmountCells.Text)));
- end;
- procedure TLab_5_2.edAmountCellsChange(Sender: TObject);
- begin
- edAmountWays.Text := '';
- if Length(edAmountCells.Text) = 0 then
- btnCountMoves.Enabled := False
- else
- btnCountMoves.Enabled := True
- end;
- procedure TLab_5_2.edAmountCellsKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- edAmountCells.SelStart := (Length(edAmountCells.Text))
- end;
- procedure TLab_5_2.edAmountCellsKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key = '0') and (Length(edAmountCells.Text) = 0) then
- Key := #0
- end;
- procedure TLab_5_2.edAmountCellsKeyUp(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- edAmountCells.SelStart := (Length(edAmountCells.Text) + 1)
- end;
- procedure TLab_5_2.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- if MessageBox(Handle, PChar('Are you sure?'), PChar('Are you sure?'), MB_ICONINFORMATION + MB_YESNO) = mrNo then
- Action := caNone;
- end;
- procedure TLab_5_2.FormCreate(Sender: TObject);
- begin
- CountObj := TCount.Create;
- end;
- procedure TLab_5_2.Help2Click(Sender: TObject);
- const
- Msg1 = 'There is a strip of checkered paper with a width of one cell and a ';
- Msg2 = 'length of n cells. The first cage is a checker. With one move the ';
- Msg3 = 'piece can be moved one or two cells. This program recursively ';
- Msg4 = 'determines the number of ways to move checkers to the n-th cell.';
- begin
- MessageBox(Handle, PChar(Msg1 + Msg2 + Msg3 + Msg4), PChar('Error.'), MB_ICONINFORMATION);
- end;
- function TLab_5_2.IsCorrectNumber(strNumber: string): Boolean;
- var
- Number: Integer;
- begin
- try
- Number := StrToInt(strNumber);
- if (Number > 9999) then
- begin
- MessageBox(Handle, PChar('File contain incorrect value'), PChar('Error.'), MB_ICONSTOP);
- Result := False;
- end;
- except
- MessageBox(Handle, PChar('File containg incorrect valur'), PChar('Error.'), MB_ICONSTOP);
- Result := False;
- end;
- end;
- procedure TLab_5_2.Open1Click(Sender: TObject);
- var
- txtFile: TextFile;
- Number: Integer;
- strNumber: string;
- begin
- if OpenFile.Execute then
- begin
- try
- AssignFile(txtFile, OpenFile.FileName);
- Reset(txtFile);
- except
- MessageBox(Handle, PChar('Eror opening to file.'), PChar('Error.'), MB_ICONSTOP);
- CloseFile(txtFile);
- Exit
- end;
- if Eof(txtFile) then
- begin
- MessageBox(Handle, PChar('File is empty'), PChar('Error.'), MB_ICONSTOP);
- CloseFile(txtFile);
- Exit
- end;
- Read(txtFile, strNumber);
- if not IsCorrectNumber(strNumber) then
- begin
- MessageBox(Handle, PChar('File is empty'), PChar('Error.'), MB_ICONSTOP);
- CloseFile(txtFile);
- Exit
- end;
- edAmountCells.Text := IntToStr(Number);
- btnCountMoves.Enabled := True;
- CloseFile(txtFile);
- end;
- end;
- procedure TLab_5_2.Save1Click(Sender: TObject);
- var
- txtFile: TextFile;
- begin
- if SaveFile.Execute then
- begin
- try
- AssignFile(txtFile, SaveFile.FileName);
- Rewrite(txtFile);
- except
- MessageBox(Handle, PChar('Eror writing to file.'), PChar('Error.'), MB_ICONSTOP);
- CloseFile(txtFile);
- Exit
- end;
- Writeln(txtFile, 'Count cells: ', edAmountCells.Text);
- Writeln(txtFile, 'Amount ways: ', edAmountWays.Text);
- CloseFile(txtFile);
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement