Advertisement
gguuppyy

LAB62

Apr 21st, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. Unit Main;
  2.  
  3. Interface
  4.  
  5. Uses
  6. Winapi.Windows,
  7. Winapi.Messages,
  8. System.SysUtils,
  9. System.Variants,
  10. System.Classes,
  11. Vcl.Graphics,
  12. Clipbrd,
  13. Vcl.Controls,
  14. Vcl.Forms,
  15. Vcl.Dialogs,
  16. Vcl.StdCtrls,
  17. Vcl.Menus,
  18. Vcl.ExtCtrls,
  19. Vcl.Imaging.Pngimage;
  20.  
  21. Type
  22. TMainForm = Class(TForm)
  23. ResButton: TButton;
  24. CellNumber: TEdit;
  25. MainMenu1: TMainMenu;
  26. N1: TMenuItem;
  27. N2: TMenuItem;
  28. N3: TMenuItem;
  29. N4: TMenuItem;
  30. N5: TMenuItem;
  31. Y1: TLabel;
  32. Disk: TLabel;
  33. BalloonHint1: TBalloonHint;
  34. OpenDialog1: TOpenDialog;
  35. SaveDialog1: TSaveDialog;
  36. Output: TMemo;
  37. LabelTask: TLabel;
  38. Procedure ResButtonClick(Sender: TObject);
  39. Procedure N2Click(Sender: TObject);
  40. Procedure N5Click(Sender: TObject);
  41. Procedure CellNumberKeyPress(Sender: TObject; Var Key: Char);
  42. Procedure N3Click(Sender: TObject);
  43. Procedure GetPath(Var Path: String);
  44. Procedure N4Click(Sender: TObject);
  45. Procedure Checker(Var IsCorrect: Boolean);
  46. Procedure N1Click(Sender: TObject);
  47. Procedure CellNumberChange(Sender: TObject);
  48. Procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X: Integer);
  49. Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  50. Procedure CellNumberContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  51.  
  52. Private
  53. { Private declarations }
  54. Public
  55. { Public declarations }
  56. End;
  57.  
  58. Const
  59. NumSet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #8, #44, #45, #46];
  60.  
  61. Var
  62. MainForm: TMainForm;
  63.  
  64. Implementation
  65.  
  66. {$R *.dfm}
  67.  
  68. Uses
  69. Instruction,
  70. Information;
  71.  
  72. Procedure TmainForm.Checker(Var IsCorrect: Boolean);
  73.  
  74. Begin
  75. IsCorrect := True;
  76.  
  77. Try
  78. StrtoFloat(CellNumber.Text);
  79. Except
  80. IsCorrect := False;
  81. End;
  82.  
  83. End;
  84.  
  85. Function CountWays(N: Integer): Integer;
  86. Begin
  87. If N = 1 Then
  88. Result := 1
  89. Else
  90. If N = 2 Then
  91. Result := 2
  92. Else
  93. Result := CountWays(N - 1) + CountWays(N - 2);
  94. End;
  95.  
  96. Procedure TMainForm.ResButtonClick(Sender: TObject);
  97. Var
  98. NumDisks: Integer;
  99. Begin
  100. Try
  101. NumDisks := StrToInt(CellNumber.Text);
  102. Except
  103. NumDisks := 0;
  104. End;
  105.  
  106. Output.Lines.Clear;
  107. Output.Lines.Add('Количество способов продвижения на ' + IntToStr(NumDisks) + '-ю клетку: ' + IntToStr(CountWays(NumDisks)));
  108. End;
  109.  
  110. Procedure TMainForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  111. Var
  112. Confirmation: Integer;
  113. Begin
  114. Confirmation := Application.MessageBox('Вы действительно хотите выйти?', 'Выход', MB_YESNO);
  115. CanClose := Confirmation = IDYES;
  116. End;
  117.  
  118. Procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X: Integer);
  119. Begin
  120. If (CellNumber.Text <> '') Then
  121. ResButton.Enabled := True
  122. Else
  123. ResButton.Enabled := False;
  124. End;
  125.  
  126. Procedure TMainForm.CellNumberChange(Sender: TObject);
  127. Var
  128. Value: Double;
  129. Begin
  130. If TryStrToFloat(CellNumber.Text, Value) Then
  131. Begin
  132. If (Value < 1) Or (Value > 30) Then
  133. Begin
  134. BalloonHint1.Title := 'Неверное значение.';
  135. BalloonHint1.Description := 'N должно быть больше 0 и меньше 30.';
  136. BalloonHint1.ImageIndex := 1;
  137. BalloonHint1.ShowHint(CellNumber);
  138. CellNumber.Text := '';
  139. End;
  140. End;
  141. CellNumber.MaxLength := 2;
  142. If Length(CellNumber.Text) = 0 Then
  143. Begin
  144. Output.Lines.Clear;
  145. End;
  146. End;
  147.  
  148. Procedure TMainForm.CellNumberContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  149. Begin
  150. Handled := True;
  151. End;
  152.  
  153. Procedure TMainForm.CellNumberKeyPress(Sender: TObject; Var Key: Char);
  154. Begin
  155.  
  156. If Key In NumSet Then
  157. Begin
  158. If Key = '0' Then
  159. Begin
  160. If Length(CellNumber.Text) = 1 Then
  161. Begin
  162. If CellNumber.Text[1] In ['0'] Then
  163. Begin
  164. Key := #0;
  165. End;
  166. End;
  167. If Length(CellNumber.Text) = 2 Then
  168. Begin
  169. If (CellNumber.Text[1] = '-') AND (CellNumber.Text[2] = '0') Then
  170. Begin
  171. Key := #0;
  172. End;
  173. End;
  174. End;
  175.  
  176. If Key = #45 Then
  177. Begin
  178. If Length(CellNumber.Text) > 0 Then
  179. Begin
  180. BalloonHint1.Title := 'Недопустимый символ.';
  181. BalloonHint1.Description := 'Здесь можно ввести только число.';
  182. BalloonHint1.ImageIndex := 1;
  183. BalloonHint1.ShowHint(CellNumber);
  184. Key := #0;
  185. End;
  186. End;
  187.  
  188. If Key In [',', '.'] Then
  189. Begin
  190. Key := FormatSettings.DecimalSeparator;
  191. If Pos(Key, CellNumber.Text) <> 0 Then
  192. Begin
  193. BalloonHint1.Title := 'Недопустимый символ.';
  194. BalloonHint1.Description := 'Здесь можно ввести только число.';
  195. BalloonHint1.ImageIndex := 1;
  196. BalloonHint1.ShowHint(CellNumber);
  197. Key := #0;
  198. End
  199. Else
  200. If Length(CellNumber.Text) > 0 Then
  201. Begin
  202. If (CellNumber.Text[1] = '-') AND (Length(CellNumber.Text) = 1) Then
  203. Begin
  204. Key := #0;
  205. End
  206. End
  207. Else
  208. If CellNumber.SelStart = 0 Then
  209. Begin
  210. CellNumber.Text := '0' + CellNumber.Text;
  211. CellNumber.SelStart := 1;
  212. End;
  213. End;
  214. End
  215. Else
  216. Begin
  217. BalloonHint1.Title := 'Недопустимый символ.';
  218. BalloonHint1.Description := 'Здесь можно ввести только число.';
  219. BalloonHint1.ShowHint(CellNumber);
  220. Key := #0;
  221. End;
  222. End;
  223.  
  224. Procedure TMainForm.N1Click(Sender: TObject);
  225. Begin
  226. If Output.Lines.Count = 0 Then
  227. Begin
  228. N4.Enabled := False;
  229. End
  230. Else
  231. Begin
  232. N4.Enabled := True;
  233. End;
  234. End;
  235.  
  236. Procedure TMainForm.N2Click(Sender: TObject);
  237. Begin
  238. InstructionForm.ShowModal;
  239. End;
  240.  
  241. Procedure TMainForm.GetPath(Var Path: String);
  242. Begin
  243. If OpenDialog1.Execute Then
  244. Begin
  245. Path := OpenDialog1.FileName;
  246. End
  247. Else
  248. Begin
  249. ShowMessage('Открытие файла отменено!');
  250. End;
  251. End;
  252.  
  253. Procedure TMainForm.N3Click(Sender: TObject);
  254. Var
  255. IsCorrect, Fail: Boolean;
  256. Path, Number, Str: String;
  257. Count: Integer;
  258. FileElem: TextFile;
  259. Begin
  260. GetPath(Path);
  261.  
  262. AssignFile(FileElem, Path);
  263. Fail := False;
  264. IsCorrect := True;
  265. Count := 0;
  266.  
  267. If Not FileExists(Path) Then
  268. Begin
  269. ShowMessage('Файл не найден!');
  270. End
  271. Else
  272. Begin
  273. Try
  274. Try
  275. Reset(FileElem);
  276. Except
  277. CloseFile(FileElem);
  278. IsCorrect := False;
  279. ShowMessage('Ошибка открытия файла.');
  280. End;
  281.  
  282. If IsCorrect Then
  283. Begin
  284. While Not Eof(FileElem) Do
  285. Begin
  286. ReadLn(FileElem, Str);
  287. Inc(Count);
  288. End;
  289.  
  290. CloseFile(FileElem);
  291. If Count <> 1 Then
  292. Fail := True;
  293. End;
  294.  
  295. If IsCorrect Then
  296. Begin
  297. Reset(FileElem);
  298.  
  299. Try
  300. Readln(FileElem, Number);
  301. If Not TryStrToInt(Number, Count) Then
  302. Fail := True;
  303. Except
  304. Fail := True;
  305. End;
  306. End;
  307. Finally
  308. CloseFile(FileElem);
  309. If Fail Then
  310. ShowMessage('Проверьте введенные данные и повторите попытку!')
  311. Else
  312. Begin
  313. CellNumber.Text := Number;
  314. End;
  315. ShowMessage('Обработка завершена.');
  316. End;
  317. End;
  318. End;
  319.  
  320. Procedure TMainForm.N4Click(Sender: TObject);
  321. Begin
  322.  
  323. If SaveDialog1.Execute Then
  324. Begin
  325.  
  326. Output.Lines.SaveToFile(SaveDialog1.FileName);
  327. End
  328. Else
  329.  
  330. Begin
  331. ShowMessage('Сохранение в файл отменено.');
  332. End;
  333.  
  334. End;
  335.  
  336. Procedure TMainForm.N5Click(Sender: TObject);
  337. Begin
  338. InformationForm.ShowModal;
  339. End;
  340.  
  341. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement