Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.44 KB | None | 0 0
  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Menus;
  8.  
  9. type
  10.   TArr = array of Byte;
  11.   TArrOfTheSets = array[0..1000] of Int64;
  12.  
  13.   TLab_5_2 = class(TForm)
  14.     edAmountCells: TEdit;
  15.     lbInf: TLabel;
  16.     edAmountWays: TEdit;
  17.     shLine: TShape;
  18.     btnCountMoves: TButton;
  19.     lbAmountMoves: TLabel;
  20.     MainMenu1: TMainMenu;
  21.     File1: TMenuItem;
  22.     Open1: TMenuItem;
  23.     Save1: TMenuItem;
  24.     Help1: TMenuItem;
  25.     Aboutauthor1: TMenuItem;
  26.     Help2: TMenuItem;
  27.     OpenFile: TOpenDialog;
  28.     SaveFile: TSaveDialog;
  29.     procedure edAmountCellsKeyPress(Sender: TObject; var Key: Char);
  30.     procedure edAmountCellsKeyDown(Sender: TObject; var Key: Word;
  31.       Shift: TShiftState);
  32.     procedure edAmountCellsKeyUp(Sender: TObject; var Key: Word;
  33.       Shift: TShiftState);
  34.     procedure btnCountMovesClick(Sender: TObject);
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure edAmountCellsChange(Sender: TObject);
  37.     procedure Open1Click(Sender: TObject);
  38.     procedure Save1Click(Sender: TObject);
  39.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  40.     procedure Aboutauthor1Click(Sender: TObject);
  41.     procedure Help2Click(Sender: TObject);
  42.     function  IsCorrectNumber(strNumber: string): Boolean;
  43.   private
  44.     { Private declarations }
  45.   public
  46.     { Public declarations }
  47.   end;
  48.  
  49.   TCount = class
  50.   private
  51.     Arr: TArr;
  52.     ArrOfTheSets: TArrOfTheSets;
  53.     procedure SetArrWithZeros(var ArrOfTheSets: TArrOfTheSets);
  54.     function CountAmountMoves(Arr: TArr): Int64;
  55.   public
  56.     function FindAmountMoves(N: Integer): Int64;
  57.   end;
  58.  
  59. var
  60.   Lab_5_2: TLab_5_2;
  61.   CountObj: TCount;
  62.  
  63.  
  64. implementation
  65.  
  66. {$R *.dfm}
  67.  
  68. procedure TCount.SetArrWithZeros(var ArrOfTheSets: TArrOfTheSets);
  69. var
  70.    index: Word;
  71. begin
  72.    for index := 0 to Length(ArrOfTheSets) do
  73.       ArrOfTheSets[index] := 0;
  74. end;
  75.  
  76. function TCount.CountAmountMoves(Arr: TArr): Int64;
  77. begin
  78.    if (Length(Arr) = 1) or (Length(Arr) = 0) then
  79.       Result := 1
  80.    else
  81.    begin
  82.       if ArrOfTheSets[Length(Arr)] = 0 then
  83.          ArrOfTheSets[Length(Arr)] := CountAmountMoves(copy(Arr, 1, Length(Arr))) + CountAmountMoves(copy(Arr, 2, Length(Arr)));
  84.       Result := ArrOfTheSets[Length(Arr)];
  85.    end;
  86. end;
  87.  
  88. function TCount.FindAmountMoves(N: Integer): Int64;
  89. begin
  90.    SetLength(Arr, N);
  91.    SetArrWithZeros(ArrOfTheSets);
  92.    ArrOfTheSets[0] := 1;
  93.    ArrOfTheSets[1] := 1;
  94.    Result := CountAmountMoves(Arr);
  95. end;
  96.  
  97. procedure TLab_5_2.Aboutauthor1Click(Sender: TObject);
  98. begin
  99.     MessageBox(Handle, PChar('The author of this program id Vladislav Mironuk (851001)'),
  100.                PChar('Error.'), MB_ICONINFORMATION);
  101. end;
  102.  
  103. procedure TLab_5_2.btnCountMovesClick(Sender: TObject);
  104. var
  105.    Arr: TArr;
  106. begin
  107.    edAmountWays.Text := IntToStr(CountObj.FindAmountMoves(StrToInt(edAmountCells.Text)));
  108. end;
  109.  
  110. procedure TLab_5_2.edAmountCellsChange(Sender: TObject);
  111. begin
  112.    edAmountWays.Text := '';
  113.    if Length(edAmountCells.Text) = 0 then
  114.       btnCountMoves.Enabled := False
  115.    else
  116.       btnCountMoves.Enabled := True
  117. end;
  118.  
  119. procedure TLab_5_2.edAmountCellsKeyDown(Sender: TObject; var Key: Word;
  120.   Shift: TShiftState);
  121. begin
  122.    edAmountCells.SelStart := (Length(edAmountCells.Text))
  123. end;
  124.  
  125. procedure TLab_5_2.edAmountCellsKeyPress(Sender: TObject; var Key: Char);
  126. begin
  127.    if (Key = '0') and (Length(edAmountCells.Text) = 0) then
  128.       Key := #0
  129. end;
  130.  
  131. procedure TLab_5_2.edAmountCellsKeyUp(Sender: TObject; var Key: Word;
  132.   Shift: TShiftState);
  133. begin
  134.    edAmountCells.SelStart := (Length(edAmountCells.Text) + 1)
  135. end;
  136.  
  137. procedure TLab_5_2.FormClose(Sender: TObject; var Action: TCloseAction);
  138. begin
  139.    if MessageBox(Handle, PChar('Are you sure?'), PChar('Are you sure?'), MB_ICONINFORMATION + MB_YESNO) = mrNo then
  140.       Action := caNone;
  141. end;
  142.  
  143. procedure TLab_5_2.FormCreate(Sender: TObject);
  144. begin
  145.    CountObj := TCount.Create;
  146. end;
  147.  
  148. procedure TLab_5_2.Help2Click(Sender: TObject);
  149. const
  150.    Msg1 = 'There is a strip of checkered paper with a width of one cell and a ';
  151.    Msg2 = 'length of n cells. The first cage is a checker. With one move the ';
  152.    Msg3 = 'piece can be moved one or two cells. This program recursively ';
  153.    Msg4 = 'determines the number of ways to move checkers to the n-th cell.';
  154. begin
  155.    MessageBox(Handle, PChar(Msg1 + Msg2 + Msg3 + Msg4), PChar('Error.'), MB_ICONINFORMATION);
  156. end;
  157.  
  158.  
  159. function TLab_5_2.IsCorrectNumber(strNumber: string): Boolean;
  160. var
  161.    Number: Integer;
  162. begin
  163.     try
  164.        Number := StrToInt(strNumber);
  165.        if (Number > 9999) then
  166.        begin
  167.           MessageBox(Handle, PChar('File contain incorrect value'), PChar('Error.'), MB_ICONSTOP);
  168.           Result := False;
  169.        end;
  170.     except
  171.        MessageBox(Handle, PChar('File containg incorrect valur'), PChar('Error.'), MB_ICONSTOP);
  172.        Result := False;
  173.     end;
  174. end;
  175.  
  176. procedure TLab_5_2.Open1Click(Sender: TObject);
  177. var
  178.    txtFile: TextFile;
  179.    Number: Integer;
  180.    strNumber: string;
  181. begin
  182.    if OpenFile.Execute then
  183.    begin
  184.       try
  185.          AssignFile(txtFile, OpenFile.FileName);
  186.          Reset(txtFile);
  187.       except
  188.          MessageBox(Handle, PChar('Eror opening to file.'), PChar('Error.'), MB_ICONSTOP);
  189.          CloseFile(txtFile);
  190.          Exit
  191.       end;
  192.  
  193.       if Eof(txtFile) then
  194.       begin
  195.          MessageBox(Handle, PChar('File is empty'), PChar('Error.'), MB_ICONSTOP);
  196.          CloseFile(txtFile);
  197.          Exit
  198.       end;
  199.  
  200.       Read(txtFile, strNumber);
  201.       if not  IsCorrectNumber(strNumber) then
  202.       begin
  203.          MessageBox(Handle, PChar('File is empty'), PChar('Error.'), MB_ICONSTOP);
  204.          CloseFile(txtFile);
  205.          Exit
  206.       end;
  207.       edAmountCells.Text := IntToStr(Number);
  208.       btnCountMoves.Enabled := True;
  209.       CloseFile(txtFile);
  210.    end;
  211. end;
  212.  
  213. procedure TLab_5_2.Save1Click(Sender: TObject);
  214. var
  215.    txtFile: TextFile;
  216. begin
  217.    if SaveFile.Execute then
  218.    begin
  219.       try
  220.          AssignFile(txtFile, SaveFile.FileName);
  221.          Rewrite(txtFile);
  222.       except
  223.          MessageBox(Handle, PChar('Eror writing to file.'), PChar('Error.'), MB_ICONSTOP);
  224.          CloseFile(txtFile);
  225.          Exit
  226.       end;
  227.       Writeln(txtFile, 'Count cells: ', edAmountCells.Text);
  228.       Writeln(txtFile, 'Amount ways: ', edAmountWays.Text);
  229.       CloseFile(txtFile);
  230.    end;
  231. end;
  232.  
  233. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement