Advertisement
gguuppyy

форма33

Mar 11th, 2024 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.90 KB | None | 0 0
  1. Unit form23;
  2.  
  3. Interface
  4.  
  5. Uses
  6.  
  7. Math,
  8. Vcl.ExtDlgs,
  9. Vcl.Dialogs,
  10. Vcl.Menus,
  11. Vcl.StdCtrls,
  12. Vcl.Controls,
  13. Winapi.Windows,
  14. Winapi.Messages,
  15. System.SysUtils,
  16. System.Variants,
  17. System.Classes,
  18. Vcl.Graphics,
  19. Vcl.Forms,
  20. Unit23_1,
  21. Unit23_2,
  22. Vcl.ComCtrls,
  23. Vcl.Grids;
  24.  
  25. Type
  26. TErrorCode = (EcCorrect, EcInvalid_Value, EcInvalid_Range, EcNot_Readable, EcNot_Writeable, EcIncorrect_Amount_lines);
  27.  
  28. Type
  29. TArr = Array Of Array Of Array Of Integer;
  30.  
  31. TMainForm = Class(TForm)
  32. MainMenu: TMainMenu;
  33. FileMenu: TMenuItem;
  34. OpenMenu: TMenuItem;
  35. SaveMenu: TMenuItem;
  36. ExitMenu: TMenuItem;
  37. InstructionMenu: TMenuItem;
  38. DeveloperMenu: TMenuItem;
  39. TaskLabel: TLabel;
  40. NEdit: TEdit;
  41. ResultButton: TButton;
  42. MyOpenTextFileDialog: TOpenTextFileDialog;
  43. MySaveTextFileDialog: TSaveTextFileDialog;
  44. RLabel: TLabel;
  45. XGrid: TStringGrid;
  46. Label1: TLabel;
  47. PopupMenu1: TPopupMenu;
  48. Label2: TLabel;
  49. MEdit: TEdit;
  50. KEdit: TEdit;
  51. Label3: TLabel;
  52. ResultMemo: TMemo;
  53. Procedure NEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  54. Procedure InstructionMenuClick(Sender: TObject);
  55. Procedure DeveloperMenuClick(Sender: TObject);
  56. Procedure OpenMenuClick(Sender: TObject);
  57. Procedure SaveMenuClick(Sender: TObject);
  58. Procedure ResultButtonClick(Sender: TObject);
  59. Procedure ExitMenuClick(Sender: TObject);
  60. procedure CalcResult(Sender: TObject);
  61. Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  62. Function CheckInputFields(Sender: TObject; CurEdit: TEdit; CurStringGrid: TStringGrid): Boolean;
  63. Procedure FileMenuClick(Sender: TObject);
  64. Procedure CheckEdit(Sender: TObject; Var Key: Char; CurEdit: TEdit);
  65. Procedure SelectEdit(Sender: TObject; Var Key: Word);
  66. Procedure FormCreate(Sender: TObject);
  67.  
  68. Function CheckCells(CurStringGrid: TStringGrid): Boolean;
  69. Procedure NEditKeyPress(Sender: TObject; Var Key: Char);
  70. Procedure XGridSetEditText(Sender: TObject; ACol, ARow: Integer; Const Value: String);
  71. Procedure NEditChange(Sender: TObject);
  72. Procedure GridCheck(Sender: TObject);
  73. Procedure XGridKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  74. Procedure SelectEdit2(Sender: TObject; Var Key: Word; CurEdit: TEdit);
  75. Procedure MEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  76. Procedure MEditKeyPress(Sender: TObject; Var Key: Char);
  77. Procedure MEditChange(Sender: TObject);
  78. Procedure KEditChange(Sender: TObject);
  79. Procedure KEditKeyPress(Sender: TObject; Var Key: Char);
  80. Procedure KEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  81. Function EnterArr(): TArr;
  82. Procedure ColorizeColumn(StringGrid: TStringGrid; ColumnIndex: Integer; Color: TColor);
  83. Procedure XGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  84. Private
  85. { Private declarations }
  86. Public
  87. { Public declarations }
  88. End;
  89.  
  90. Const
  91. ERRORS: Array [TErrorCode] Of String = ('', 'Некорректный тип данных внутри файла!', 'Значения не попадают в диапазон!',
  92. 'Файл закрыт для чтения!', 'Файл закрыт для записи!', 'Неверное количество данных в файле');
  93. BACKSPACE = #8;
  94. NONE = #0;
  95. DIGITS = ['0' .. '9'];
  96. MAX_N = 10;
  97. MIN_N = 2;
  98. MAX_X = 1000;
  99. MIN_X = -1000;
  100. FACTOR = 1.247;
  101.  
  102. Var
  103. MainForm: TMainForm;
  104.  
  105. Implementation
  106.  
  107. {$R *.dfm}
  108.  
  109. Function IsAbleToReading(Var F: TextFile): TErrorCode;
  110. Var
  111. Error: TErrorCode;
  112. Begin
  113. Error := EcCorrect;
  114. Try
  115. Reset(F);
  116. CloseFile(F);
  117. Except
  118. Error := EcNot_Readable;
  119. End;
  120. IsAbleToReading := Error;
  121. End;
  122.  
  123. Function FindChar(MyText: String; MyChar: Char): Boolean;
  124. Var
  125. N, I, Counter: Integer;
  126. Begin
  127. N := Length(MyText);
  128. Counter := 0;
  129. For I := 1 To N Do
  130. If MyText[I] = MyChar Then
  131. Inc(Counter);
  132. If Counter = 0 Then
  133. FindChar := False
  134. Else
  135. FindChar := True;
  136. End;
  137.  
  138. Function CheckUserArea(Num: Integer; Const MAX, MIN: Integer): Boolean;
  139.  
  140. Var
  141. IsCorrect: Boolean;
  142. Begin
  143. If (Num < 2) Or (Num > 10) Then
  144. Begin
  145. IsCorrect := False;
  146. End
  147. Else
  148. IsCorrect := True;
  149. CheckUserArea := IsCorrect;
  150. End;
  151.  
  152. Function CheckXArea(Num: Double; Const MAX, MIN: Real): Boolean;
  153.  
  154. Var
  155. IsCorrect: Boolean;
  156. Begin
  157. If (Num < MIN) Or (Num > MAX) Then
  158. Begin
  159. IsCorrect := False;
  160. End
  161. Else
  162. IsCorrect := True;
  163. CheckXArea := IsCorrect;
  164. End;
  165.  
  166. Function CheckFileData(Var F: TextFile; N, M, K1: Integer; Var Error: TErrorCode): TErrorCode;
  167. Var
  168. KLine: String;
  169. K: Real;
  170. Numbers: TArray<String>;
  171. CountN, CountM, I: Integer;
  172. Begin
  173. I := 0;
  174. CountM := 0;
  175. While (Error = EcCorrect) And Not EOF(F) Do
  176. Begin
  177.  
  178. Try
  179. Read(F, KLine);
  180. Numbers := KLine.Split([' ']);
  181. Except
  182. Error := EcInvalid_Value;
  183. End;
  184. If (Error = EcCorrect) And (Length(Numbers) <> N * K1) Then
  185. Begin
  186. Error := EcIncorrect_Amount_lines;
  187. End;
  188. I := 0;
  189. While (Error = EcCorrect) And (I < (Length(Numbers))) Do
  190. Begin
  191. Try
  192. K := StrToFloat(Numbers[I]);
  193. Except
  194. Error := EcInvalid_Value;
  195. End;
  196. Inc(I);
  197. If (Error = EcCorrect) And Not(CheckXArea(K, MAX_X, MIN_X)) Then
  198. Error := EcInvalid_Range;
  199. End;
  200.  
  201. Inc(CountM);
  202. Readln(F);
  203. End;
  204. If (Error = EcCorrect) And (CountM <> M) Then
  205. Begin
  206. Error := EcIncorrect_Amount_lines;
  207. End;
  208. CloseFile(F);
  209. CheckFileData := Error;
  210. End;
  211.  
  212. Function CheckFileData1(Var F: TextFile): TErrorCode;
  213. Var
  214. FLine: String;
  215. I, N, M, J, K1: Integer;
  216. K, S: Double;
  217. Error: TErrorCode;
  218. Begin
  219. Error := EcCorrect;
  220. Reset(F);
  221.  
  222. For I := 1 To 3 Do
  223. Begin
  224. Readln(F, FLine);
  225. Try
  226. Begin
  227. If I = 1 Then
  228. M := StrToInt(FLine)
  229. Else
  230. If I = 2 Then
  231.  
  232. N := StrToInt(FLine)
  233. Else
  234. K1 := StrToInt(FLine)
  235. End;
  236. Except
  237. Error := EcInvalid_Value;
  238. End;
  239.  
  240. If (Error = EcCorrect) And Not(CheckUserArea(StrToInt(FLine), MAX_N, MIN_N)) Then
  241. Error := EcInvalid_Range;
  242.  
  243. End;
  244. If Error = EcCorrect Then
  245. Error := CheckFileData(F, N, M, K1, Error);
  246. CheckFileData1 := Error;
  247. End;
  248.  
  249. Procedure TMainForm.DeveloperMenuClick(Sender: TObject);
  250. Var
  251. DeveloperForm: TDeveloperForm;
  252. Begin
  253. DeveloperForm := TDeveloperForm.Create(Self);
  254. DeveloperForm.ShowModal;
  255. DeveloperForm.Free;
  256. End;
  257.  
  258. Procedure TMainForm.InstructionMenuClick(Sender: TObject);
  259. Var
  260. InstructionForm: TInstructionForm;
  261. Begin
  262.  
  263. InstructionForm := TInstructionForm.Create(Self);
  264. InstructionForm.ShowModal;
  265. InstructionForm.Free;
  266. End;
  267.  
  268. Procedure TMainForm.KEditChange(Sender: TObject);
  269. Var
  270. I, L, K: Integer;
  271. Begin
  272. If (KEdit.Text = '') Then
  273. Begin
  274. For I := 0 To XGrid.ColCount - 1 Do
  275.  
  276. XGrid.Rows[I].Clear;
  277. XGrid.ColCount := 0;
  278. End
  279. Else
  280. Begin
  281. XGrid.ColCount := StrToInt(KEdit.Text) * StrToInt(NEdit.Text);
  282.  
  283. End;
  284.  
  285. End;
  286.  
  287. Procedure TMainForm.KEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  288. Begin
  289. SelectEdit2(Sender, Key, KEdit);
  290. End;
  291.  
  292. Procedure TMainForm.KEditKeyPress(Sender: TObject; Var Key: Char);
  293. Begin
  294. CheckEdit(Sender, Key, KEdit);
  295. End;
  296.  
  297. Procedure TMainForm.ColorizeColumn(StringGrid: TStringGrid; ColumnIndex: Integer; Color: TColor);
  298. Var
  299. RowIndex: Integer;
  300. Begin
  301. If (ColumnIndex >= 0) And (ColumnIndex < StringGrid.ColCount) Then
  302. Begin
  303. For RowIndex := 0 To StringGrid.RowCount - 1 Do
  304. Begin
  305. StringGrid.Canvas.Brush.Color := Color;
  306. StringGrid.Canvas.FillRect(StringGrid.CellRect(ColumnIndex, RowIndex));
  307. StringGrid.Canvas.TextRect(StringGrid.CellRect(ColumnIndex, RowIndex), StringGrid.CellRect(ColumnIndex, RowIndex).Left + 2,
  308. StringGrid.CellRect(ColumnIndex, RowIndex).Top + 2, StringGrid.Cells[ColumnIndex, RowIndex]);
  309. End;
  310. End;
  311. End;
  312.  
  313. Procedure TMainForm.NEditChange(Sender: TObject);
  314. Var
  315. I: Integer;
  316. Begin
  317. If (NEdit.Text = '') Then
  318. Begin
  319. For I := 0 To XGrid.ColCount - 1 Do
  320.  
  321. XGrid.Rows[I].Clear;
  322. XGrid.ColCount := 0;
  323. End
  324. Else
  325. { XGrid.ColCount := StrToInt(KEdit.Text)*StrToInt(NEdit.Text); }
  326. End;
  327.  
  328. Procedure TMainForm.MEditChange(Sender: TObject);
  329. Var
  330. I: Integer;
  331. Begin
  332.  
  333. If (MEdit.Text = '') Then
  334. Begin
  335. For I := 0 To XGrid.RowCount - 1 Do
  336. XGrid.Rows[I].Clear;
  337. XGrid.RowCount := 0;
  338. End
  339. Else
  340. XGrid.RowCount := StrToInt(MEdit.Text);
  341. End;
  342.  
  343. Procedure TMainForm.MEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  344. Begin
  345. SelectEdit2(Sender, Key, MEdit);
  346. End;
  347.  
  348. Procedure TMainForm.MEditKeyPress(Sender: TObject; Var Key: Char);
  349. Begin
  350. CheckEdit(Sender, Key, MEdit);
  351. End;
  352.  
  353. Procedure TMainForm.NEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  354. Begin
  355. SelectEdit2(Sender, Key, NEdit);
  356. End;
  357.  
  358. Procedure TMainForm.NEditKeyPress(Sender: TObject; Var Key: Char);
  359. Begin
  360. CheckEdit(Sender, Key, NEdit);
  361. End;
  362.  
  363. Procedure TMainForm.OpenMenuClick(Sender: TObject);
  364. Var
  365. X1: Real;
  366. FilePath: String;
  367. F: TextFile;
  368. ErrorCode: TErrorCode;
  369. N1, M1, I, J, K1: Integer;
  370. N, M, X, K: String;
  371. Numbers: TArray<String>;
  372. Begin
  373. If MyOpenTextFileDialog.Execute Then
  374. Begin
  375. FilePath := MyOpenTextFileDialog.FileName;
  376. AssignFile(F, FilePath);
  377. ErrorCode := IsAbleToReading(F);
  378. If ErrorCode = EcCorrect Then
  379. Begin
  380. ErrorCode := CheckFileData1(F);
  381. If ErrorCode <> EcCorrect Then
  382. Application.MessageBox(PChar(ERRORS[ErrorCode]), 'Ошибка', MB_OK)
  383. Else
  384. Begin
  385. Reset(F);
  386. Readln(F, M);
  387. MEdit.Text := M;
  388. Readln(F, N);
  389. NEdit.Text := N;
  390. Readln(F, K);
  391. KEdit.Text := K;
  392. N1 := StrToInt(N);
  393. M1 := StrToInt(M);
  394. K1 := StrToInt(K);
  395. For J := 0 To M1 - 1 Do
  396. Begin
  397. Read(F, X);
  398. Numbers := X.Split([' ']);
  399. For I := 0 To (N1 * K1 - 1) Do
  400. Begin
  401. XGrid.Cells[I, J] := Numbers[I];
  402. End;
  403. Readln(F);
  404. End;
  405. CloseFile(F);
  406. End;
  407. End
  408. Else
  409. Application.MessageBox(PChar(ERRORS[ErrorCode]), 'Ошибка', MB_OK);
  410. End;
  411.  
  412. End;
  413.  
  414. Function TMainForm.EnterArr(): TArr;
  415. Var
  416. N, M, K, I, J, L, J1: Integer;
  417. RArr: Tarr;
  418. Begin
  419. N := StrToInt(NEdit.Text);
  420. M := StrToInt(KEdit.Text);
  421. K := StrToInt(MEdit.Text);
  422. SetLength(RArr, K, M, N);
  423. J1 := 0;
  424. For L := 0 To K - 1 Do
  425.  
  426. Begin
  427. For I := 0 To M - 1 Do
  428. Begin
  429. J := 0;
  430. while (J<N)do
  431. Begin
  432.  
  433. RArr[L, I, J] := StrToInt(XGrid.Cells[(J+n*L), I]);
  434.  
  435. Inc(J);
  436. End;
  437. End;
  438. End;
  439. EnterArr := RArr;
  440. End;
  441.  
  442. Function DeletBackspace(EditText: String; Var KeyChar: Char): String;
  443. Begin
  444. If (KeyChar = BACKSPACE) Then
  445. DeletBackspace := EditText
  446. Else
  447. DeletBackspace := EditText + KeyChar;
  448.  
  449. End;
  450.  
  451. Procedure TMainForm.CheckEdit(Sender: TObject; Var Key: Char; CurEdit: TEdit);
  452. Begin
  453. If Not(Key In DIGITS) And Not(Key = BACKSPACE) Then
  454. Key := NONE
  455. Else
  456. Begin
  457. If (Length(CurEdit.Text) < 1) And (Key = '0') Then
  458. Key := NONE;
  459. If (Length(CurEdit.Text) > 1) And Not(CheckUserArea(StrToInt(DeletBackspace(CurEdit.Text, Key)), MAX_N, MIN_N)) And Not(Key = BACKSPACE) Then
  460. Key := NONE;
  461. End;
  462. End;
  463.  
  464. Procedure TMainForm.SelectEdit(Sender: TObject; Var Key: Word);
  465. Begin
  466. With XGrid Do
  467. Begin
  468. If (Key = VK_RIGHT) Then
  469. Begin
  470. If Col < (Colcount - 1) Then
  471. Col := Col + 1
  472. Else
  473. Col := 0;
  474. Key := 0;
  475. End
  476. Else
  477. Begin
  478. If (Key = VK_LEFT) Then
  479. Begin
  480. If Col > 0 Then
  481. Col := Col - 1
  482. Else
  483. Col := Colcount - 1;
  484. Key := 0;
  485. End
  486. Else
  487. Begin
  488. If (Key = VK_Up) Then
  489. Begin
  490. If Row > 0 Then
  491. Row := Row - 1
  492. Else
  493. Row := Rowcount - 1;
  494. Key := 0;
  495. End
  496. Else
  497. Begin
  498. If (Key = VK_Down) Then
  499. Begin
  500. If Row < (Rowcount - 1) Then
  501. Row := Row + 1
  502. Else
  503. Row := 0;
  504. Key := 0;
  505. End;
  506. End;
  507.  
  508. End;
  509.  
  510. End;
  511.  
  512. End;
  513. End;
  514.  
  515. Procedure TMainForm.SelectEdit2(Sender: TObject; Var Key: Word; CurEdit: TEdit);
  516. Begin
  517. With XGrid Do
  518. Begin
  519. If (Key = VK_RIGHT) Or (Key = VK_LEFT) Then
  520. XGrid.SetFocus
  521. Else
  522. If (Key = VK_Up) Then
  523. Begin
  524. SelectNext(CurEdit, True, True);
  525. End
  526. Else
  527. If (Key = VK_Down) Then
  528. Begin
  529. SelectNext(CurEdit, True, False);
  530. End;
  531. End;
  532. End;
  533.  
  534. Function TMainForm.CheckInputFields(Sender: TObject; CurEdit: TEdit; CurStringGrid: TStringGrid): Boolean;
  535. Var
  536. N, M: Byte;
  537. IsCorrect: Boolean;
  538. Begin
  539.  
  540. IsCorrect := True;
  541. If (NEdit.Text = '') Or (MEdit.Text = '') Then
  542. Begin
  543. IsCorrect := False;
  544. CurEdit.SetFocus;
  545. ShowMessage('Введите размер матрицы!');
  546. End
  547. Else
  548. Begin
  549. N := StrToInt(NEdit.Text);
  550. M := StrToInt(MEdit.Text);
  551. If (N < 2) Then
  552. Begin
  553. IsCorrect := False;
  554. CurEdit.Text := '';
  555. CurEdit.SetFocus;
  556. ShowMessage('N не может быть меньше 2!');
  557. End
  558. Else
  559. Begin
  560. If (M < 2) Then
  561. Begin
  562. IsCorrect := False;
  563. CurEdit.Text := '';
  564. CurEdit.SetFocus;
  565. ShowMessage('M не может быть меньше 2!');
  566. End
  567.  
  568. End;
  569.  
  570. End;
  571. If Not(CheckCells(XGrid)) And (IsCorrect) Then
  572. Begin
  573. IsCorrect := False;
  574. CurStringGrid.SetFocus;
  575. ShowMessage('Заполните таблицу до конца!');
  576. End;
  577. CheckInputFields := IsCorrect;
  578.  
  579. End;
  580.  
  581. Procedure TMainForm.XGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  582. Begin
  583. Var
  584. K, I, J, N, I1: Integer;
  585. Begin
  586. If (KEdit.Text <> '') And (Medit.Text <> '') And (NEdit.Text <> '') Then
  587. Begin
  588. K := StrToInt(KEdit.Text);
  589. N := StrToInt(NEdit.Text);
  590. I1 := 0;
  591. For J := 0 To K - 1 Do
  592. Begin
  593. For I := 0 To N - 1 Do
  594.  
  595. Begin
  596. If J Mod 2 = 1 Then
  597. ColorizeColumn(XGrid, I1, ClRed)
  598. Else
  599. If J Mod 2 = 0 Then
  600. ColorizeColumn(XGrid, I1, ClGreen);
  601. Inc(I1);
  602. End;
  603. End;
  604. End;
  605. End;
  606. End;
  607.  
  608. Procedure TMainForm.XGridKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  609. Begin
  610. SelectEdit(Sender, Key)
  611. End;
  612.  
  613. Procedure TMainForm.XGridSetEditText(Sender: TObject; ACol, ARow: Integer; Const Value: String);
  614. Var
  615. I, J: Integer;
  616. Amount: Integer;
  617. Begin
  618. For I := 0 To XGrid.RowCount Do
  619. Begin
  620. For J := 0 To XGrid.ColCount Do
  621. Begin
  622. If ((Not TryStrToInt(XGrid.Cells[I, J], Amount)) And (XGrid.Cells[I, J] <> '') And (XGrid.Cells[I, J] <> '-')) Then
  623. Begin
  624. ShowMessage('Некорректные исходные данные!');
  625. XGrid.Cells[I, J] := '';
  626. End
  627. Else
  628. If (XGrid.Cells[I, J] <> '') And (XGrid.Cells[I, J] <> '-') Then
  629. Begin
  630. If Not(CheckXArea(StrToInt(XGrid.Cells[I, J]), MAX_X, MIN_X)) Then
  631. Begin
  632.  
  633. ShowMessage('Некорректные исходные данные!');
  634. XGrid.Cells[I, J] := '';
  635. End;
  636. End;
  637. End;
  638. End;
  639.  
  640. End;
  641.  
  642. { Procedure TMainForm.CalcResult(Sender: TObject);
  643. Var
  644. Count, I, J: Integer;
  645. IsCorrect: Boolean;
  646. Begin
  647. Count := 0;
  648. IsCorrect := True;
  649. For J := 0 To StrToInt(MEdit.Text) - 1 Do
  650. Begin
  651. For I := 0 To StrToInt(NEdit.Text) - 2 Do
  652. Begin
  653. If (StrToFloat(XGrid.Cells[I, J]) >= (StrToFloat(XGrid.Cells[I+1, J]))) Then
  654. Begin
  655. IsCorrect := False;
  656. End;
  657.  
  658. End;
  659. If IsCorrect Then
  660. Inc(Count);
  661. IsCorrect := True;
  662. End;
  663. ResultEdit.Text:='Количество отсортированных по возрастанию строк матрицы: '+IntToStr(Count);
  664. End; }
  665.  
  666. procedure ShowProcess(Arr: TArr; StepProc: Integer; ResultMemo: TMemo);
  667. var
  668. I, J, K: Integer;
  669. TextToAdd: string;
  670. begin
  671. ResultMemo.Lines.Add('Шаг ' + IntToStr(StepProc) + ':');
  672. for I := 0 to High(Arr) do
  673. begin
  674. for J := 0 to High(Arr[I]) do
  675. begin
  676. TextToAdd := '';
  677. for K := 0 to High(Arr[I][J]) do
  678. TextToAdd := TextToAdd + IntToStr(Arr[I][J][K]) + ' ';
  679. ResultMemo.Lines.Add(TextToAdd);
  680. end;
  681. ResultMemo.Lines.Add('');
  682. end;
  683. end;
  684.  
  685. procedure TMainForm.CalcResult(Sender: TObject);
  686. var
  687. Arr: TArr;
  688. Step: Real;
  689. I, IStep, Temp, AbsLength, X1, Y1, Z1, X2, Y2, Z2, CoefX, CoefY, StepProc: Integer;
  690. begin
  691. // Ваш код для инициализации массива Arr
  692. arr := EnterArr();
  693. AbsLength := Length(Arr) * Length(Arr[0]) * Length(Arr[0][0]);
  694. CoefX := Length(Arr[0]) * Length(Arr[0][0]);
  695. CoefY := Length(Arr[0][0]);
  696. Step := AbsLength - 1;
  697. StepProc := 1;
  698. while Step >= 1 do
  699. begin
  700. IStep := Trunc(Step);
  701. I := 0;
  702. while Step + I < AbsLength do
  703. begin
  704. X1 := I div CoefX;
  705. Y1 := (I - X1 * CoefX) div CoefY;
  706. Z1 := I - X1 * CoefX - Y1 * CoefY;
  707. X2 := (I + IStep) div CoefX;
  708. Y2 := (I + IStep - X2 * CoefX) div CoefY;
  709. Z2 := I + IStep - X2 * CoefX - Y2 * CoefY;
  710. if Arr[X1][Y1][Z1] > Arr[X2][Y2][Z2] then
  711. begin
  712. Temp := Arr[X1][Y1][Z1];
  713. Arr[X1][Y1][Z1] := Arr[X2][Y2][Z2];
  714. Arr[X2][Y2][Z2] := Temp;
  715. ShowProcess(Arr, StepProc, ResultMemo);
  716. Inc(StepProc);
  717. end;
  718. Inc(I);
  719. end;
  720. Step := Step / FACTOR;
  721. end;
  722. end;
  723.  
  724. Procedure TMainForm.GridCheck(Sender: TObject);
  725. Var
  726. I, J: Integer;
  727. Amount: Extended;
  728. IsExistCell: Boolean;
  729. Begin
  730. XGrid.ColCount := strtoint(nedit.text)*strtoint(kedit.text);
  731.  
  732. IsExistCell := True;
  733. For I := 0 To (XGrid.RowCount) - 1 Do
  734. Begin
  735. For J := 0 To XGrid.ColCount - 1 Do
  736. Begin
  737. If (XGrid.Cells[J, I] = '-') Then
  738. Begin
  739. IsExistCell := False;
  740. XGrid.Cells[J, I] := ''
  741. End;
  742. End;
  743. End;
  744. If Not(IsExistCell) Then
  745. ShowMessage('Некорректные исходные данные!');
  746. End;
  747.  
  748. Procedure TMainForm.ResultButtonClick(Sender: TObject);
  749. Begin
  750. GridCheck(Sender);
  751. If CheckInputFields(Sender, NEdit, XGrid) Then
  752. CalcResult(Sender);
  753. End;
  754.  
  755. Var
  756. IsSave: Boolean = False;
  757.  
  758. Procedure TMainForm.SaveMenuClick(Sender: TObject);
  759. Var
  760. FilePath: String;
  761. F: TextFile;
  762. Begin
  763. If (ResultMemo.Text <> '') Then
  764. Begin
  765. If MySaveTextFileDialog.Execute Then
  766. Begin
  767. FilePath := MySaveTextFileDialog.FileName;
  768. AssignFile(F, FilePath);
  769. If FileIsReadOnly(FilePath) Then
  770. Application.MessageBox(PChar(ERRORS[EcNot_Writeable]), 'Ошибка', MB_OK + MB_ICONERROR)
  771. Else
  772. Begin
  773. Rewrite(F);
  774. Write(F, ResultMemo.Text);
  775. CloseFile(F);
  776. IsSave := True;
  777. End;
  778. End;
  779. End;
  780.  
  781. End;
  782.  
  783. Procedure TMainForm.FileMenuClick(Sender: TObject);
  784. Begin
  785. If ResultMemo.Text <> '' Then
  786. SaveMenu.Enabled := True
  787. Else
  788. SaveMenu.Enabled := False;
  789. End;
  790.  
  791. Procedure TMainForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
  792. Var
  793. IsExit: Word;
  794. Begin
  795. If Not IsSave And (ResultMemo.Text <> '') Then
  796. Begin
  797. IsExit := Application.MessageBox('Вы не сохранили файл, хотите ли сохранить?', 'Выход', MB_YESNOCANCEL + MB_ICONQUESTION);
  798. Case IsExit Of
  799. MrYes:
  800. Begin
  801. CanClose := False;
  802. FileMenu.Click;
  803. SaveMenu.Click;
  804. End;
  805.  
  806. MrNo:
  807. CanClose := True;
  808. MrCancel:
  809. CanClose := False;
  810. End;
  811.  
  812. End
  813. Else
  814. Begin
  815. IsExit := Application.MessageBox('Вы действительно хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION);
  816. Case IsExit Of
  817. MrYes:
  818. CanClose := True;
  819. MrNo:
  820. CanClose := False;
  821. End;
  822. End;
  823. End;
  824.  
  825. Procedure TMainForm.FormCreate(Sender: TObject);
  826. Begin
  827. TaskLabel.Caption := 'Данная программа сортирует трехмерный массив по возрастанию.';;
  828. End;
  829.  
  830. Function TMainForm.CheckCells(CurStringGrid: TStringGrid): Boolean;
  831. Var
  832. I, J: Integer;
  833. Begin
  834. Result := True;
  835.  
  836. For I := 0 To CurStringGrid.RowCount - 1 Do
  837. Begin
  838. For J := 0 To CurStringGrid.ColCount - 1 Do
  839. Begin
  840. If CurStringGrid.Cells[J, I] = '' Then
  841. Begin
  842. Result := False;
  843. Exit; //Если найдена пустая ячейка, выходим из функции
  844. End;
  845. End;
  846. End;
  847. End;
  848.  
  849. Procedure TMainForm.ExitMenuClick(Sender: TObject);
  850. Var
  851. IsExit: Word;
  852. Begin
  853. If Not IsSave And (ResultMemo.Text <> '') Then
  854. Begin
  855. IsExit := Application.MessageBox('Вы не сохранили файл, хотите ли сохранить?', 'Выход', MB_YESNOCANCEL + MB_ICONQUESTION);
  856. Case IsExit Of
  857. MrYes:
  858. SaveMenu.Click;
  859. MrNo:
  860. Close;
  861. End;
  862.  
  863. End
  864. Else
  865. Begin
  866. IsExit := Application.MessageBox('Вы действительно хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION);
  867. Case IsExit Of
  868. MrYes:
  869. Close;
  870. End;
  871. End;
  872. End;
  873.  
  874. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement