Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7. System.Classes, Vcl.Graphics,
  8. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.Grids;
  9.  
  10. type
  11. TArray = array of Integer;
  12.  
  13. TForm1 = class(TForm)
  14. mnPanel: TMainMenu;
  15. File1: TMenuItem;
  16. Help1: TMenuItem;
  17. Open1: TMenuItem;
  18. Save1: TMenuItem;
  19. N1: TMenuItem;
  20. Exit1: TMenuItem;
  21. Aboutprogram1: TMenuItem;
  22. Aboutthedeveloper1: TMenuItem;
  23. lbDescription: TLabel;
  24. lbCoordinates: TLabel;
  25. lbAnswer: TLabel;
  26. PopupMenu1: TPopupMenu;
  27. SaveFileDialog: TSaveDialog;
  28. edInputN: TEdit;
  29. lbN: TLabel;
  30. OpenFileDialog: TOpenDialog;
  31. bAnswer: TButton;
  32. sgFillMatrix: TStringGrid;
  33. procedure edInputAKeyPress(Sender: TObject; var Key: Char);
  34. procedure Aboutprogram1Click(Sender: TObject);
  35. procedure Aboutthedeveloper1Click(Sender: TObject);
  36. procedure Exit1Click(Sender: TObject);
  37. procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  38. procedure Save1Click(Sender: TObject);
  39. procedure Open1Click(Sender: TObject);
  40. function FillArrCoords(CoordsArr: TArray; ArrLength: Integer): Boolean;
  41. procedure bAnswerClick(Sender: TObject);
  42. procedure sgFillMatrixDblClvarick(Sender: TObject);
  43. function CheckN(TextN: String): Boolean;
  44. procedure FormCreate(Sender: TObject);
  45. private
  46. { Private declarations }
  47. public
  48. { Public declarations }
  49. end;
  50.  
  51. var
  52. Form1: TForm1;
  53.  
  54. implementation
  55.  
  56. {$R *.dfm}
  57.  
  58. procedure TForm1.Aboutprogram1Click(Sender: TObject);
  59. begin
  60. ShowMessage
  61. ('This program will caluclate the smallest side of a N gone, where N entered from the keyboard');
  62. end;
  63.  
  64. procedure TForm1.Aboutthedeveloper1Click(Sender: TObject);
  65. begin
  66. ShowMessage
  67. ('This program was developed by a student of the group 951007 Alexander Voroshilov');
  68. end;
  69.  
  70. function TForm1.CheckN(TextN: String): Boolean;
  71. var
  72. N: Integer;
  73. IsCorrect: Boolean;
  74. begin
  75. IsCorrect := True;
  76. try
  77. N := StrToInt(TextN);
  78. if (N < 3) or (N > 9) then
  79. begin
  80. ShowMessage('Incorrect value of N. N should be from 3 to 9');
  81. IsCorrect := False;
  82. end;
  83. except
  84. ShowMessage('Empty field or not integer value of N.');
  85. IsCorrect := False;
  86. end;
  87. if not IsCorrect then
  88. edInputN.Text := '';
  89. CheckN := IsCorrect;
  90. end;
  91.  
  92. function CheckPoint(CoordsArr: TArray; i: Integer): Boolean;
  93. const
  94. ErrorMessage = 'Error. Some points are on one point. Try again ';
  95. var
  96. j: Integer;
  97. IsCorrect: Boolean;
  98. begin
  99. IsCorrect := True;
  100. j := 0;
  101. while (j <= i - 2) do
  102. begin
  103. if (CoordsArr[j] = CoordsArr[i]) and (CoordsArr[j + 1] = CoordsArr[i + 1])
  104. then
  105. begin
  106. IsCorrect := False;
  107. ShowMessage(ErrorMessage);
  108. end;
  109. j := j + 2;
  110. end;
  111. Result := IsCorrect;
  112. end;
  113.  
  114. function CheckLine(CoordsArr: TArray; i: Integer): Boolean;
  115. const
  116. ErrorMessage =
  117. 'Error. Some points are on one line, that is not correct for polygon. Try again ';
  118. var
  119. IsCorrect: Boolean;
  120. begin
  121. IsCorrect := True;
  122. // ((y1-y2)*x3+(x2-x1)*y3+(x1*y2-x2*y1)==0) - one line
  123. if ((CoordsArr[i - 3] - CoordsArr[i - 1]) * CoordsArr[i] +
  124. (CoordsArr[i - 2] - CoordsArr[i - 4]) * CoordsArr[i + 1] +
  125. (CoordsArr[i - 4] * CoordsArr[i - 1] - CoordsArr[i - 2] * CoordsArr[i - 3]
  126. ) = 0) then
  127. begin
  128. IsCorrect := False;
  129. ShowMessage(ErrorMessage);
  130. end;
  131. Result := IsCorrect;
  132. end;
  133.  
  134. function TForm1.FillArrCoords(CoordsArr: TArray; ArrLength: Integer): Boolean;
  135. var
  136. IsCorrect1: Boolean;
  137. IsCorrect2: Boolean;
  138. i: Integer;
  139. j: Integer;
  140. begin
  141. i := 0;
  142. j := 1;
  143. IsCorrect1 := True;
  144. IsCorrect2 := True;
  145. while (i < ArrLength * 2 - 1) do
  146. begin
  147. if IsCorrect1 and IsCorrect2 then
  148. begin
  149. try
  150. CoordsArr[i] := StrToInt(sgFillMatrix.Cells[j, (i div 2) + 1]);
  151. CoordsArr[i + 1] :=
  152. StrToInt(sgFillMatrix.Cells[(j + 1), (i div 2) + 1]);
  153. if i >= 2 then
  154. begin
  155. IsCorrect1 := CheckPoint(CoordsArr, i);
  156. if i >= 4 then
  157. IsCorrect2 := CheckLine(CoordsArr, i);
  158. end;
  159. except
  160. ShowMessage('Empty fields or not integer coordinates');
  161. IsCorrect1 := False;
  162. end;
  163. end;
  164. i := i + 2;
  165. end;
  166. if IsCorrect1 and IsCorrect2 then
  167. FillArrCoords := True
  168. else
  169. FillArrCoords := False;
  170. end;
  171.  
  172. function CalculateSize(CoordsFirst: Integer; CoordsSecond: Integer;
  173. CoordsArr: TArray): Double;
  174. begin
  175. // side^2 = (х2 — х1)^2 + (y2 — y1)^2
  176. Result := Sqrt((CoordsArr[CoordsSecond] * CoordsArr[CoordsSecond] +
  177. CoordsArr[CoordsFirst] * CoordsArr[CoordsFirst] - 2 * CoordsArr
  178. [CoordsSecond] * CoordsArr[CoordsFirst]) + (CoordsArr[CoordsSecond + 1] *
  179. CoordsArr[CoordsSecond + 1] + CoordsArr[CoordsFirst + 1] *
  180. CoordsArr[CoordsFirst + 1] - 2 * CoordsArr[CoordsFirst + 1] *
  181. CoordsArr[CoordsSecond + 1]));
  182. end;
  183.  
  184. function FindSide(ArrLength: Integer; CoordsArr: TArray): String;
  185. var
  186. Size: Double;
  187. NumberOfXandY: Integer;
  188. EqualSides: Integer;
  189. TempSide: Double;
  190. LastSide: Double;
  191. FirstPoint: Integer;
  192. SecondPoint: Integer;
  193. i: Integer;
  194. Answer: String;
  195.  
  196. begin
  197. EqualSides := 0;
  198. FirstPoint := 0;
  199. SecondPoint := ArrLength * 2 - 2;
  200. // LastSide = side between (x1,y1) and (xLast,yLast)
  201. LastSide := CalculateSize(FirstPoint, SecondPoint, CoordsArr);
  202. Size := LastSide;
  203. NumberOfXandY := ArrLength;
  204. i := 0;
  205. while i < ArrLength * 2 - 3 do
  206. begin
  207. FirstPoint := i;
  208. SecondPoint := i + 2;
  209. TempSide := CalculateSize(FirstPoint, SecondPoint, CoordsArr);
  210. if (TempSide < Size) then
  211. begin
  212. Size := TempSide;
  213. NumberOfXandY := (i + 2) div 2;
  214. EqualSides := 0;
  215. end
  216. else if (TempSide = Size) then
  217. EqualSides := EqualSides + 1;
  218. i := i + 2;
  219. end;
  220. Answer := 'The smallest side is ';
  221. if NumberOfXandY = ArrLength then
  222. Answer := Answer + '(x' + IntToStr(NumberOfXandY) + ', y' +
  223. IntToStr(NumberOfXandY) + ') and (x1, y1)'
  224. else
  225. Answer := Answer + '(x' + IntToStr(NumberOfXandY) + ', y' +
  226. IntToStr(NumberOfXandY) + ') and (x' + IntToStr(NumberOfXandY + 1) +
  227. ', y' + IntToStr(NumberOfXandY + 1) + ')';
  228. Answer := Answer + ' and equal ' + FloatToStr(Size);
  229. Answer := Answer + #13#10 + IntToStr(EqualSides) +
  230. ' other sides also have this length';
  231. FindSide := Answer;
  232. end;
  233.  
  234. procedure TForm1.bAnswerClick(Sender: TObject);
  235. var
  236. N: Integer;
  237. IsCorrect: Boolean;
  238. CoordsArr: TArray;
  239. begin
  240. N := StrToInt(edInputN.Text);
  241. SetLength(CoordsArr, N * 2);
  242. IsCorrect := FillArrCoords(CoordsArr, N);
  243. if IsCorrect then
  244. lbAnswer.Caption := FindSide(N, CoordsArr);
  245. end;
  246.  
  247. function FileExtensionCheck(var NameOfFile: string): Boolean;
  248. var
  249. Extension: string;
  250. i, j: Integer;
  251. begin
  252. if (pos('.', NameOfFile) = 0) then
  253. begin
  254. ShowMessage
  255. ('Since the extension does not appear in the entered file name, the extension ".txt" is automatically assigned.');
  256. NameOfFile := NameOfFile + '.txt';
  257. FileExtensionCheck := True;
  258. end
  259. else
  260. begin
  261. Extension := '';
  262. j := length(NameOfFile);
  263. for i := pos('.', NameOfFile) to j do
  264. Extension := Extension + NameOfFile[i];
  265. if (Extension <> '.txt') and (Extension <> '.doc') and
  266. (Extension <> '.text') then
  267. begin
  268. ShowMessage
  269. ('Attention, an error has occurred! A file with this extension cannot be used. The program supports the extensions: ".txt", ".doc", ".text".');
  270. FileExtensionCheck := False;
  271. end
  272. else
  273. FileExtensionCheck := True;
  274. end;
  275. end;
  276.  
  277. procedure TForm1.edInputAKeyPress(Sender: TObject; var Key: Char);
  278. begin
  279. if not(Key in [#8, #13, ',', '-', '0' .. '9']) then
  280. begin
  281. ShowMessage('Enter numbers!');
  282. Key := #0;
  283. end;
  284. end;
  285.  
  286. procedure TForm1.Exit1Click(Sender: TObject);
  287. begin
  288. Close;
  289. end;
  290.  
  291. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  292. begin
  293. case Application.MessageBox('Are you sure you want to exit the program?',
  294. 'Exit', MB_YESNO) of
  295. ID_YES:
  296. ;
  297. else
  298. CanClose := False;
  299. end;
  300. end;
  301.  
  302. procedure TForm1.FormCreate(Sender: TObject);
  303. begin
  304.  
  305. end;
  306.  
  307. procedure TForm1.Open1Click(Sender: TObject);
  308. var
  309. SourceFile: TextFile;
  310. FileData, FileName: String;
  311. IsInvalidInput: Boolean;
  312. i: Integer;
  313. N: Integer;
  314. NRow, NCol: Integer;
  315.  
  316. begin
  317. if OpenFileDialog.Execute then
  318. begin
  319. FileName := OpenFileDialog.FileName;
  320. IsInvalidInput := FileExtensionCheck(FileName);
  321. if FileExists(FileName) and IsInvalidInput then
  322. begin
  323. try
  324. AssignFile(SourceFile, FileName);
  325. Reset(SourceFile);
  326. except
  327. ShowMessage
  328. ('Error! Impossible to open the file. Please, check the file.');
  329. end;
  330. if EoF(SourceFile) then
  331. ShowMessage('Error! The file is empty')
  332. else
  333. begin
  334. ReadLn(SourceFile, FileData);
  335. edInputN.Text := FileData;
  336. try
  337. sgFillMatrixDblClvarick(Sender);
  338. if (sgFillMatrix.EditorMode) then
  339. begin
  340. N := StrToInt(FileData);
  341. edInputN.Text := IntToStr(N);
  342. for NRow := 1 to sgFillMatrix.RowCount do
  343. begin
  344. i := 1;
  345. ReadLn(SourceFile, FileData);
  346. for NCol := 1 to N - 1 do
  347. begin
  348. while (FileData[i] in [',', '0' .. '9']) do
  349. begin
  350. sgFillMatrix.Cells[NCol, NRow] :=
  351. sgFillMatrix.Cells[NCol, NRow] + FileData[i];
  352. Inc(i);
  353. end;
  354. Inc(i);
  355. end;
  356. end
  357. end;
  358. except
  359. ShowMessage('Error! Invalid N');
  360. end
  361. end;
  362. CloseFile(SourceFile);
  363. end
  364. else if IsInvalidInput then
  365. ShowMessage('Error! The file is not found.');
  366. end;
  367. end;
  368.  
  369. procedure TForm1.Save1Click(Sender: TObject);
  370. var
  371. AnswerFile: TextFile;
  372. FileName: string;
  373. IsInvalidInput: Boolean;
  374.  
  375. begin
  376. if SaveFileDialog.Execute then
  377. begin
  378. FileName := SaveFileDialog.FileName;
  379. IsInvalidInput := FileExtensionCheck(FileName);
  380. if FileExists(FileName) and IsInvalidInput then
  381. begin
  382. AssignFile(AnswerFile, SaveFileDialog.FileName);
  383. Rewrite(AnswerFile);
  384. Write(AnswerFile, lbAnswer.Caption);
  385. CloseFile(AnswerFile);
  386. end
  387. else if IsInvalidInput then
  388. ShowMessage('Error! The file is not found.');
  389. end;
  390. end;
  391.  
  392. procedure TForm1.sgFillMatrixDblClvarick(Sender: TObject);
  393. var
  394. NRow: Integer;
  395. begin
  396. if (CheckN(edInputN.Text)) then
  397. begin
  398. sgFillMatrix.RowCount := StrToInt(edInputN.Text) + 1;
  399. sgFillMatrix.EditorMode := True;
  400. sgFillMatrix.Options := sgFillMatrix.Options + [goEditing] + [goTabs];
  401. sgFillMatrix.Cells[0, 0] := '=)';
  402. sgFillMatrix.Cells[1, 0] := 'x';
  403. sgFillMatrix.Cells[2, 0] := 'y';
  404. for NRow := 1 to sgFillMatrix.RowCount - 1 do
  405. sgFillMatrix.Cells[0, NRow] := IntToStr(NRow) + ' coord';
  406. bAnswer.Visible := True;
  407. end
  408. end;
  409.  
  410. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement