Advertisement
gguuppyy

lab44finally

Mar 30th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 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. DiskNumber: 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 DiskNumberKeyPress(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 DiskNumberChange(Sender: TObject);
  48. Procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  49. Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  50. Procedure DiskNumberContextPopup(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(DiskNumber.Text);
  79. Except
  80. IsCorrect := False;
  81. End;
  82.  
  83. End;
  84.  
  85. Procedure SolveTowerOfHanoi(N, Source, Destination, Auxiliary: Integer; Output: TMemo);
  86. Var
  87. TextToAdd: String;
  88. Begin
  89. If N > 0 Then
  90. Begin
  91. SolveTowerOfHanoi(N - 1, Source, Auxiliary, Destination, Output);
  92. TextToAdd := 'Переместить диск ' + IntToStr(N) + ' с ' + IntToStr(Source) + ' на ' + IntToStr(Destination);
  93. Output.Lines.Add(TextToAdd);
  94. SolveTowerOfHanoi(N - 1, Auxiliary, Destination, Source, Output);
  95. End;
  96. End;
  97.  
  98. Procedure TMainForm.ResButtonClick(Sender: TObject);
  99. Var
  100. NumDisks: Integer;
  101. Source, Destination, Auxiliary: Integer;
  102. IsCorrect: Boolean;
  103. Begin
  104. NumDisks := StrToInt(DiskNumber.Text);
  105. Source := 1;
  106. Destination := 3;
  107. Auxiliary := 2;
  108. Checker(IsCorrect);
  109. If IsCorrect Then
  110. Begin
  111. Output.Lines.Clear;
  112. SolveTowerOfHanoi(NumDisks, Source, Destination, Auxiliary, Output);
  113. Output.Lines.Add('Головоломка решена.');
  114. End
  115. Else
  116. Begin
  117. ShowMessage('Заполните все поля.');
  118. End;
  119. End;
  120.  
  121. Procedure TMainForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  122. Var
  123. Confirmation: Integer;
  124. Begin
  125. Confirmation := Application.MessageBox('Вы действительно хотите выйти?', 'Выход', MB_YESNO);
  126. CanClose := Confirmation = IDYES;
  127. End;
  128.  
  129. Procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  130. Begin
  131. If (DiskNumber.Text <> '') Then
  132. ResButton.Enabled := True
  133. Else
  134. ResButton.Enabled := False;
  135. End;
  136.  
  137. Procedure TMainForm.DiskNumberChange(Sender: TObject);
  138. Var
  139. Value: Double;
  140. Begin
  141. If TryStrToFloat(DiskNumber.Text, Value) Then
  142. Begin
  143. If (Value < 1) Then
  144. Begin
  145. BalloonHint1.Title := 'Неверное значение.';
  146. BalloonHint1.Description := 'N должно быть больше 0.';
  147. BalloonHint1.ImageIndex := 1;
  148. BalloonHint1.ShowHint(DiskNumber);
  149. DiskNumber.Text := '';
  150. End;
  151. End;
  152. DiskNumber.MaxLength := 5;
  153. If Length(DiskNumber.Text) = 0 Then
  154. Begin
  155. Output.Lines.Clear;
  156. End;
  157. End;
  158.  
  159. Procedure TMainForm.DiskNumberContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  160. Begin
  161. Handled := True;
  162. End;
  163.  
  164. Procedure TMainForm.DiskNumberKeyPress(Sender: TObject; Var Key: Char);
  165. Begin
  166.  
  167. If Key In NumSet Then
  168. Begin
  169. If Key = '0' Then
  170. Begin
  171. If Length(DiskNumber.Text) = 1 Then
  172. Begin
  173. If DiskNumber.Text[1] In ['0'] Then
  174. Begin
  175. Key := #0;
  176. End;
  177. End;
  178. If Length(DiskNumber.Text) = 2 Then
  179. Begin
  180. If (DiskNumber.Text[1] = '-') AND (DiskNumber.Text[2] = '0') Then
  181. Begin
  182. Key := #0;
  183. End;
  184. End;
  185. End;
  186.  
  187. If Key = #45 Then
  188. Begin
  189. If Length(DiskNumber.Text) > 0 Then
  190. Begin
  191. BalloonHint1.Title := 'Недопустимый символ.';
  192. BalloonHint1.Description := 'Здесь можно ввести только число.';
  193. BalloonHint1.ImageIndex := 1;
  194. BalloonHint1.ShowHint(DiskNumber);
  195. Key := #0;
  196. End;
  197. End;
  198.  
  199. If Key In [',', '.'] Then
  200. Begin
  201. Key := FormatSettings.DecimalSeparator;
  202. If Pos(Key, DiskNumber.Text) <> 0 Then
  203. Begin
  204. BalloonHint1.Title := 'Недопустимый символ.';
  205. BalloonHint1.Description := 'Здесь можно ввести только число.';
  206. BalloonHint1.ImageIndex := 1;
  207. BalloonHint1.ShowHint(DiskNumber);
  208. Key := #0;
  209. End
  210. Else
  211. If Length(DiskNumber.Text) > 0 Then
  212. Begin
  213. If (DiskNumber.Text[1] = '-') AND (Length(DiskNumber.Text) = 1) Then
  214. Begin
  215. Key := #0;
  216. End
  217. End
  218. Else
  219. If DiskNumber.SelStart = 0 Then
  220. Begin
  221. DiskNumber.Text := '0' + DiskNumber.Text;
  222. DiskNumber.SelStart := 1;
  223. End;
  224. End;
  225. End
  226. Else
  227. Begin
  228. BalloonHint1.Title := 'Недопустимый символ.';
  229. BalloonHint1.Description := 'Здесь можно ввести только число.';
  230. BalloonHint1.ShowHint(DiskNumber);
  231. Key := #0;
  232. End;
  233. End;
  234.  
  235. Procedure TMainForm.N1Click(Sender: TObject);
  236. Begin
  237. If Output.Lines.Count = 0 Then
  238. Begin
  239. N4.Enabled := False;
  240. End
  241. Else
  242. Begin
  243. N4.Enabled := True;
  244. End;
  245. End;
  246.  
  247. Procedure TMainForm.N2Click(Sender: TObject);
  248. Begin
  249. InstructionForm.ShowModal;
  250. End;
  251.  
  252. Procedure TMainForm.GetPath(Var Path: String);
  253. Begin
  254. If OpenDialog1.Execute Then
  255. Begin
  256. Path := OpenDialog1.FileName;
  257. End
  258. Else
  259. Begin
  260. ShowMessage('Открытие файла отменено!');
  261. End;
  262. End;
  263.  
  264. Procedure TMainForm.N3Click(Sender: TObject);
  265. Var
  266. IsCorrect, Fail: Boolean;
  267. Path, Number, Str: String;
  268. Count: Integer;
  269. FileElem: TextFile;
  270. Begin
  271. GetPath(Path);
  272.  
  273. AssignFile(FileElem, Path);
  274. Fail := False;
  275. IsCorrect := True;
  276. Count := 0;
  277.  
  278. If Not FileExists(Path) Then
  279. Begin
  280. ShowMessage('Файл не найден!');
  281. End
  282. Else
  283. Begin
  284. Try
  285. Try
  286. Reset(FileElem);
  287. Except
  288. CloseFile(FileElem);
  289. IsCorrect := False;
  290. ShowMessage('Ошибка открытия файла.');
  291. End;
  292.  
  293. If IsCorrect Then
  294. Begin
  295. While Not Eof(FileElem) Do
  296. Begin
  297. ReadLn(FileElem, Str);
  298. Inc(Count);
  299. End;
  300.  
  301. CloseFile(FileElem);
  302. If Count <> 1 Then
  303. Fail := True;
  304. End;
  305.  
  306. If IsCorrect Then
  307. Begin
  308. Reset(FileElem);
  309.  
  310. Try
  311. Readln(FileElem, Number);
  312. If Not TryStrToInt(Number, Count) Then
  313. Fail := True;
  314. Except
  315. Fail := True;
  316. End;
  317. End;
  318. Finally
  319. CloseFile(FileElem);
  320. If Fail Then
  321. ShowMessage('Проверьте введенные данные и повторите попытку!')
  322. Else
  323. Begin
  324. DiskNumber.Text := Number;
  325. End;
  326. ShowMessage('Обработка завершена.');
  327. End;
  328. End;
  329. End;
  330.  
  331. Procedure TMainForm.N4Click(Sender: TObject);
  332. Begin
  333.  
  334. If SaveDialog1.Execute Then
  335. Begin
  336.  
  337. Output.Lines.SaveToFile(SaveDialog1.FileName);
  338. End
  339. Else
  340.  
  341. Begin
  342. ShowMessage('Сохранение в файл отменено.');
  343. End;
  344.  
  345. End;
  346.  
  347. Procedure TMainForm.N5Click(Sender: TObject);
  348. Begin
  349. InformationForm.ShowModal;
  350. End;
  351.  
  352. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement