Advertisement
venik2405

Untitled

Apr 28th, 2021
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.74 KB | None | 0 0
  1. unit lab6_2;
  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.Menus, Vcl.StdCtrls,
  8. Vcl.Samples.Spin, Vcl.Grids;
  9.  
  10. type
  11. TCoordinate = Array[0..1] of Integer;
  12. TMatrix = Array of Array of Integer;
  13. TMainForm = class(TForm)
  14. FieldSTRG: TStringGrid;
  15. Calculate: TButton;
  16. SizeSpinEdit: TSpinEdit;
  17. Label2: TLabel;
  18. MainMenu1: TMainMenu;
  19. N1: TMenuItem;
  20. OpenFileButton: TMenuItem;
  21. SaveFileButton: TMenuItem;
  22. SaveFileDialog: TSaveDialog;
  23. OpenFileDialog: TOpenDialog;
  24. Label1: TLabel;
  25. N2: TMenuItem;
  26. FirstXEdit: TEdit;
  27. FirstYEdit: TEdit;
  28. StartLabel: TLabel;
  29. FinishLabel: TLabel;
  30. SecXEdit: TEdit;
  31. SecYEdit: TEdit;
  32. FinalLabel: TLabel;
  33. procedure FormCreate(Sender: TObject);
  34. procedure FieldSTRGSetEditText(Sender: TObject; ACol, ARow: Integer;
  35. const Value: string);
  36. procedure SizeSpinEditChange(Sender: TObject);
  37. procedure N2Click(Sender: TObject);
  38. Procedure CoordinatesEditChange(Sender: TObject);
  39. procedure OpenFileButtonClick(Sender: TObject);
  40. procedure N1Click(Sender: TObject);
  41. procedure CalculateClick(Sender: TObject);
  42. procedure SaveFileButtonClick(Sender: TObject);
  43. private
  44. { Private declarations }
  45. public
  46. { Public declarations }
  47. end;
  48.  
  49. var
  50. MainForm: TMainForm;
  51. Checked: Array of array of boolean;
  52. Matrix: TMatrix;
  53. WayStr: string;
  54. FX, FY: Integer;
  55. WasFound: Boolean;
  56.  
  57. implementation
  58.  
  59. {$R *.dfm}
  60.  
  61. procedure FillMatrix();
  62. Var
  63. I, J, N: Integer;
  64. Begin
  65. N := MainForm.SizeSpinEdit.Value;
  66. for I := 1 to N do
  67. MainForm.FieldSTRG.Cells[0, I] := IntToStr(I);
  68. for I := 1 to N do
  69. MainForm.FieldSTRG.Cells[I, 0] := IntToStr(I);
  70. for I := 1 to N do
  71. for J := 1 to N do
  72. MainForm.FieldSTRG.Cells[J, I] := '0';
  73. End;
  74.  
  75. procedure FillChecked();
  76. var
  77. I, J, Size: Integer;
  78. Begin
  79. Size := MainForm.FieldSTRG.ColCount - 1;
  80. SetLength(Checked, Size, Size);
  81. for I := 0 to Size - 1 do
  82. for J := 0 to Size - 1 do
  83. Checked[I, J] := False;
  84. End;
  85.  
  86. procedure TMainForm.FormCreate(Sender: TObject);
  87. var
  88. I, J: Integer;
  89. begin
  90. MainForm.Height := 500;
  91. FieldSTRG.Height := 280;
  92. FieldSTRG.Width := 280;
  93. FillMatrix();
  94. FillChecked();
  95. end;
  96.  
  97.  
  98. procedure TMainForm.N1Click(Sender: TObject);
  99. begin
  100. SaveFileButton.Enabled := Not (FinalLabel.Caption = '');
  101. end;
  102.  
  103. procedure TMainForm.N2Click(Sender: TObject);
  104. begin
  105. ShowMessage('Программа позволяет проложить путь каравана из точки'+
  106. '(X1, Y1) в точку (X2, Y2), Караван может двигаться только по местности'+
  107. ' параллельно осям Ох и Оу между центрами квадратов и только '+
  108. 'в соседний квадрат с меньшей высотой. '+ #10 +
  109. 'Вводить высоту в диапозоне от -999 до 999');
  110. end;
  111.  
  112. function CheckFile(Path: String): Boolean;
  113. var
  114. FileToCheck: TextFile;
  115. N, I, J, Num: Integer;
  116. begin
  117. AssignFile(FileToCheck, Path);
  118. Reset(FileToCheck);
  119. CheckFile := true;
  120. try
  121. Readln(FileToCheck, N);
  122. except
  123. CheckFile := false;
  124. end;
  125. if ((N < 2) or (N > 7)) then
  126. CheckFile := False;
  127. for J := 0 to N - 1 do
  128. Begin
  129. for I := 0 to N - 1 do
  130. Begin
  131. try
  132. Read(FileToCheck, Num);
  133. except
  134. CheckFile := false;
  135. end;
  136. if ((Num < -999) or (Num > 999)) then
  137. CheckFile := False;
  138. Read(FileToCheck);
  139. End;
  140. Readln(FileToCheck);
  141. End;
  142. CloseFile(FileToCheck);
  143. end;
  144.  
  145. procedure TMainForm.OpenFileButtonClick(Sender: TObject);
  146. var
  147. InputFile: TextFile;
  148. Num, N, I, J: Integer;
  149. IsCorrect: Boolean;
  150. begin
  151. IsCorrect := True;
  152. if OpenFileDialog.Execute then
  153. begin
  154. if CheckFile(OpenFileDialog.FileName) then
  155. begin
  156. AssignFile(InputFile, OpenFileDialog.FileName);
  157. Reset(InputFile);
  158. Readln(InputFile, N);
  159. SizeSpinEdit.Value := N;
  160. SetLength(Matrix, N, N);
  161. for J := 0 to N - 1 do
  162. Begin
  163. for I := 0 to N - 1 do
  164. Begin
  165. Read(InputFile, Matrix[J, I]);
  166. Read(InputFile);
  167. End;
  168. Readln(InputFile);
  169. End;
  170. CloseFile(InputFile);
  171. for J := 1 to N do
  172. for I := 1 to N do
  173. FieldSTRG.Cells[I, J] := IntToStr(Matrix[J - 1, I - 1]);
  174. ShowMessage('Данные из файла успешно загружены');
  175. end
  176. else
  177. Begin
  178. ShowMessage('Данные в файле некорректны. Введите числа.');
  179. IsCorrect := False
  180. End;
  181. if IsCorrect = True then
  182. Begin
  183. Calculate.Click;
  184. End;
  185. end;
  186.  
  187. end;
  188.  
  189. procedure CheckData();
  190. Var
  191. I, J, Num: Integer;
  192. Begin
  193. for I := 1 to MainForm.SizeSpinEdit.Value do
  194. for J := 1 to MainForm.SizeSpinEdit.Value do
  195. Begin
  196. try
  197. Num := StrToInt(MainForm.FieldSTRG.Cells[I, J]);
  198. except
  199. MainForm.Calculate.Enabled := false;
  200. end;
  201. if (Num > 999) Or (Num < -999) then
  202. MainForm.Calculate.Enabled := false;
  203. End;
  204. End;
  205.  
  206. procedure GetCoordinates(X1, Y1: Integer);
  207. Begin
  208. X1 := StrToInt(MainForm.FirstXEdit.Text);
  209. Y1 := StrToInt(MainForm.FirstYEdit.Text);
  210. FX := StrToInt(MainForm.SecXEdit.Text);
  211. FY := StrToInt(MainForm.SecYEdit.Text);
  212. End;
  213.  
  214. Function FindIfExists(X, Y: Integer): Boolean;
  215. Begin
  216. if (X > MainForm.SizeSpinEdit.Value) or (Y > MainForm.SizeSpinEdit.Value) then
  217. FindIfExists := False;
  218. if (X or Y) < 1 then
  219. FindIfExists := False;
  220. FindIfExists := True;
  221. End;
  222.  
  223. Function WasChecked(X, Y: Integer): Boolean;
  224. Begin
  225. WasChecked := Checked[X - 1,Y - 1];
  226. End;
  227.  
  228. Function FindIfPossible(X1, Y1, X2, Y2: Integer): Boolean;
  229. Var
  230. FHigh, SHeigh: Integer;
  231. Begin
  232. FHigh := StrToInt(MainForm.FieldSTRG.Cells[X1, Y1]);
  233. SHeigh := StrToInt(MainForm.FieldSTRG.Cells[X2, Y2]);
  234. if FHigh > Sheigh then
  235. FindIfPossible := True
  236. Else
  237. FindIfPossible := False;
  238. End;
  239.  
  240. procedure FindWay(X, Y: Integer);
  241. Begin
  242. Checked[X - 1, Y - 1] := True;
  243. if ((X = FX) and (Y = FY)) then
  244. Begin
  245. WasFound := True;
  246. WayStr := WayStr + '[' + IntToStr(X) + '|' + IntToStr(Y) + ']';
  247. End;
  248. if Not(WasFound) then
  249. Begin
  250. if (((not(WasChecked(X - 1, Y)) and FindIfPossible(X, Y, X - 1, Y)) or ((WasChecked(X - 1, Y)) and (not FindIfPossible(X, Y, X - 1, Y)))) and FindIfExists(X - 1, Y) ) then
  251. Begin
  252. FindWay(X - 1, Y);
  253. End;
  254. if ((not(WasChecked(X + 1, Y)) and FindIfPossible(X, Y, X + 1, Y)) or ((WasChecked(X + 1, Y)) and (not FindIfPossible(X, Y, X + 1, Y))) and FindIfExists(X + 1, Y)) then
  255. Begin
  256. FindWay(X + 1, Y);
  257. End;
  258. if ((((not(WasChecked(X, Y + 1)) and FindIfPossible(X, Y, X, Y + 1)) or ((WasChecked(X,Y + 1) and (not FindIfPossible(X, Y, X, Y + 1))))) and FindIfExists(X, Y+1))) then
  259. Begin
  260. FindWay(X, Y + 1);
  261. End;
  262. if (((not(WasChecked(X, Y - 1)) and FindIfPossible(X, Y, X, Y - 1)) or ((WasChecked(X,Y - 1)) and (not FindIfPossible(X, Y, X, Y - 1)))) and FindIfExists(X, Y - 1) ) then
  263. Begin
  264. FindWay(X, Y - 1);
  265. End;
  266. End;
  267. if Not WasFound then
  268. WayStr := WayStr + '[' + IntToStr(X) + '|' + IntToStr(Y - 1) + ']';
  269. End;
  270.  
  271. procedure TMainForm.CalculateClick(Sender: TObject);
  272. var
  273. X1, Y1, X2, Y2: Integer;
  274. begin
  275. WasFound := False;
  276. FinalLabel.Caption := '';
  277. X1 := StrToInt(MainForm.FirstXEdit.Text);
  278. Y1 := StrToInt(MainForm.FirstYEdit.Text);
  279. FX := StrToInt(MainForm.SecXEdit.Text);
  280. FY := StrToInt(MainForm.SecYEdit.Text);
  281. WayStr := '[' + IntToStr(X1) + '|' + IntToStr(Y1) + ']';
  282. FillChecked();
  283. FindWay(X1, Y1);
  284. if Not(WasFound) then
  285. WayStr := 'Путь не найден';
  286. FinalLabel.Caption := WayStr;
  287. end;
  288.  
  289. Procedure TMainForm.CoordinatesEditChange(Sender: TObject);
  290. Var
  291. Num, Finish: Integer;
  292. Begin
  293. Finish := MainForm.SizeSpinEdit.Value;
  294. Calculate.Enabled := True;
  295. try
  296. Num := StrToInt(FirstXEdit.Text);
  297. except
  298. Calculate.Enabled := false;
  299. end;
  300. if (Num > Finish) Or (Num < 1) then
  301. Calculate.Enabled := false;
  302. try
  303. Num := StrToInt(FirstYEdit.Text);
  304. except
  305. Calculate.Enabled := false;
  306. end;
  307. if (Num > Finish) Or (Num < 1) then
  308. Calculate.Enabled := false;
  309. try
  310. Num := StrToInt(SecXEdit.Text);
  311. except
  312. Calculate.Enabled := false;
  313. end;
  314. if (Num > Finish) Or (Num < 1) then
  315. Calculate.Enabled := false;
  316. try
  317. Num := StrToInt(SecYEdit.Text);
  318. except
  319. Calculate.Enabled := false;
  320. end;
  321. if (Num > Finish) Or (Num < 1) then
  322. Calculate.Enabled := false;
  323. CheckData();
  324. End;
  325.  
  326. Function IsFileExist(Address: String): Boolean; export;
  327. Var
  328. IsCorrect: Boolean;
  329. Begin
  330. If FileExists(Address) Then
  331. IsCorrect := True
  332. Else
  333. IsCorrect := False;
  334. IsFileExist := IsCorrect;
  335. End;
  336.  
  337. procedure TMainForm.SaveFileButtonClick(Sender: TObject);
  338. var
  339. OutputFile: TextFile;
  340. begin
  341. if SaveFileDialog.Execute() then
  342. Begin
  343. if IsFileExist(SaveFileDialog.FileName) then
  344. Begin
  345. AssignFile(OutputFile, SaveFileDialog.FileName);
  346. Rewrite(OutputFile);
  347. Writeln(OutputFile, FinalLabel.Caption);
  348. CloseFile(OutputFile);
  349. ShowMessage('Успешно сохранено');
  350. End
  351. else
  352. ShowMessage('Файл не существует.')
  353. End;
  354. end;
  355.  
  356. procedure TMainForm.SizeSpinEditChange(Sender: TObject);
  357. begin
  358. FillMatrix();
  359. FieldSTRG.Height := (SizeSpinEdit.Value + 1) * 40;
  360. FieldSTRG.Width := (SizeSpinEdit.Value + 1) * 40;
  361. FieldSTRG.ColCount := SizeSpinEdit.Value + 1;
  362. FieldSTRG.RowCount := SizeSpinEdit.Value + 1;
  363. MainForm.CoordinatesEditChange(Sender);
  364. end;
  365.  
  366. procedure TMainForm.FieldSTRGSetEditText(Sender: TObject; ACol, ARow: Integer;
  367. const Value: string);
  368. begin
  369. MainForm.Calculate.Enabled := True;
  370. CheckData();
  371. CoordinatesEditChange(Sender);
  372. end;
  373.  
  374. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement