gguuppyy

форма 21(нечаянно24)

Mar 2nd, 2024 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. unit form21;
  2.  
  3. interface
  4.  
  5. uses
  6. Math, Vcl.ExtDlgs, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.Controls,Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Forms,Unit21_1, Unit21_2,
  7. Vcl.ComCtrls, Vcl.Grids;
  8.  
  9. type
  10. TErrorCode=(ecCorrect, ecInvalid_Value, ecInvalid_Range, ecNot_Readable, ecNot_Writeable, ecIncorrect_Amount_lines);
  11. TMainForm = class(TForm)
  12. MainMenu: TMainMenu;
  13. FileMenu: TMenuItem;
  14. OpenMenu: TMenuItem;
  15. SaveMenu: TMenuItem;
  16. ExitMenu: TMenuItem;
  17. InstructionMenu: TMenuItem;
  18. DeveloperMenu: TMenuItem;
  19. TaskLabel: TLabel;
  20. NEdit: TEdit;
  21. ResultButton: TButton;
  22. ResultEdit: TEdit;
  23. MyOpenTextFileDialog: TOpenTextFileDialog;
  24. MySaveTextFileDialog: TSaveTextFileDialog;
  25. RLabel: TLabel;
  26. XGrid: TStringGrid;
  27. Label1: TLabel;
  28. PopupMenu1: TPopupMenu;
  29. procedure InstructionMenuClick(Sender: TObject);
  30. procedure DeveloperMenuClick(Sender: TObject);
  31. procedure OpenMenuClick(Sender: TObject);
  32. procedure SaveMenuClick(Sender: TObject);
  33. procedure ResultButtonClick(Sender: TObject);
  34. procedure ExitMenuClick(Sender: TObject);
  35. procedure CalcResult(Sender:TObject);
  36. procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  37. function CheckInputFields(Sender: TObject; CurEdit:TEdit;CurStringGrid:TStringGrid):Boolean;
  38. procedure FileMenuClick(Sender: TObject);
  39. procedure CheckEdit(Sender: TObject; var Key: Char; CurEdit:TEdit);
  40. procedure SelectEdit(Sender: TObject; var Key: Word; CurEdit:TEdit);
  41.  
  42. function CheckCells(Sender: TObject; CurEdit:TEdit;CurStringGrid:TStringGrid):Boolean;
  43. procedure NEditKeyPress(Sender: TObject; var Key: Char);
  44. procedure XGridSetEditText(Sender: TObject; ACol, ARow: Integer;
  45. const Value: string);
  46. procedure NEditChange(Sender: TObject);
  47.  
  48.  
  49.  
  50.  
  51. private
  52. { Private declarations }
  53. public
  54. { Public declarations }
  55. end;
  56.  
  57. Const
  58. ERRORS: Array [TErrorCode] Of String= ('',
  59. 'Некорректный тип данных внутри файла!',
  60. 'Значения не попадают в диапазон!',
  61. 'Файл закрыт для чтения!',
  62. 'Файл закрыт для записи!',
  63. 'Неверное количество данных в файле');
  64. BACKSPACE=#8;
  65. NONE=#0;
  66. DIGITS=['0'..'9'];
  67. MAX_N=90;
  68. MIN_N=3;
  69. MAX_X=1000;
  70. MIN_X=1;
  71.  
  72. var
  73. MainForm: TMainForm;
  74.  
  75.  
  76. implementation
  77.  
  78. {$R *.dfm}
  79. Function IsAbleToReading(Var F: TextFile): TErrorCode;
  80. Var
  81. Error: TErrorCode;
  82. Begin
  83. Error:=ecCorrect;
  84. Try
  85. Reset(F);
  86. CloseFile(F);
  87. Except
  88. Error := ecNot_Readable;
  89. End;
  90. IsAbleToReading := Error;
  91. End;
  92. function FindChar(MyText:String; MyChar:Char):Boolean;
  93. Var
  94. N,I, Counter:Integer;
  95. Begin
  96. N:=Length(MyText);
  97. Counter:=0;
  98. for I := 1 to N do
  99. if MyText[I]=MyChar then
  100. Inc(Counter);
  101. if Counter=0 then
  102. FindChar:= False
  103. Else
  104. FindChar:=True;
  105. End;
  106. Function CheckUserArea(Num: Integer ;const MAX, MIN:Integer): Boolean;
  107.  
  108. Var
  109. IsCorrect: Boolean;
  110. Begin
  111. If (Num < MIN) Or (Num > MAX) Then
  112. Begin
  113. IsCorrect := False;
  114. End
  115. Else
  116. IsCorrect := True;
  117. CheckUserArea := IsCorrect;
  118. End;
  119. Function CheckXArea(Num: Double;const MAX, MIN:Real): Boolean;
  120.  
  121. Var
  122. IsCorrect: Boolean;
  123. Begin
  124. If (Num < MIN) Or (Num > MAX) Then
  125. Begin
  126. IsCorrect := False;
  127. End
  128. Else
  129. IsCorrect := True;
  130. CheckXArea := IsCorrect;
  131. End;
  132.  
  133. Function CheckFileData(Var F: TextFile): TErrorCode;
  134. var
  135. FLine: String;
  136. I, N: Integer;
  137. s: Double;
  138. Error: TErrorCode;
  139. Begin
  140. Error := ecCorrect;
  141. Reset(F);
  142. Readln(F, FLine);
  143. try
  144. N := StrToInt(FLine);
  145. except
  146. Error := ecInvalid_Value;
  147. end;
  148. if (Error = ecCorrect) and not CheckUserArea(N, MAX_N, MIN_N) then
  149. Error := ecInvalid_Range;
  150. if Error = ecCorrect then
  151. begin
  152. for I := 1 to N do
  153. begin
  154. Read(F, FLine);
  155. if not TryStrToFloat(FLine, s) then
  156. begin
  157. Error := ecInvalid_Value;
  158. Break;
  159. end;
  160.  
  161. if not CheckXArea(s, MAX_X, MIN_X) then
  162. begin
  163. Error := ecInvalid_Range;
  164. Break;
  165. end;
  166. Readln(F);
  167. end;
  168. if (Error = ecCorrect) and not EOF(F) then
  169. Error := ecIncorrect_Amount_lines;
  170. end;
  171. CloseFile(F);
  172. CheckFileData := Error;
  173. End;
  174. procedure TMainForm.DeveloperMenuClick(Sender: TObject);
  175. var
  176. DeveloperForm: TDeveloperForm;
  177. begin
  178. DeveloperForm := TDeveloperForm.Create(Self);
  179. DeveloperForm.ShowModal;
  180. DeveloperForm.Free;
  181. end;
  182.  
  183.  
  184.  
  185. procedure TMainForm.InstructionMenuClick(Sender: TObject);
  186. var
  187. InstructionForm: TInstructionForm;
  188. begin
  189.  
  190. InstructionForm := TInstructionForm.Create(Self);
  191. InstructionForm.ShowModal;
  192. InstructionForm.Free;
  193. end;
  194.  
  195.  
  196.  
  197. procedure TMainForm.NEditChange(Sender: TObject);
  198.  
  199.  
  200. begin
  201. if NEdit.Text='' then
  202. Begin
  203. XGrid.Rows[0].Clear;
  204. XGrid.ColCount:=0;
  205. End
  206. Else
  207. XGrid.ColCount:=StrToInt(NEdit.Text)
  208. end;
  209.  
  210. procedure TMainForm.NEditKeyPress(Sender: TObject; var Key: Char);
  211. begin
  212. CheckEdit(Sender, Key,NEdit);
  213. end;
  214.  
  215. procedure TMainForm.OpenMenuClick(Sender: TObject);
  216. Var
  217. FilePath: String;
  218. F:TextFile;
  219. ErrorCode:TErrorCode;
  220. N1,I:Integer;
  221. N,X:String;
  222. begin
  223. if MyOpenTextFileDialog.Execute then
  224. Begin
  225. FilePath:=MyOpenTextFileDialog.FileName;
  226. AssignFile(F,FilePath);
  227. ErrorCode:=IsAbleToReading(F);
  228. if ErrorCode=ecCorrect then
  229. Begin
  230. ErrorCode:=CheckFileData(F);
  231. if ErrorCode<>ecCorrect then
  232. Application.MessageBox(PChar(ERRORS[ErrorCode]),'Ошибка',MB_OK)
  233. Else
  234. Begin
  235. Reset(F);
  236. Readln(F,N);
  237. NEdit.Text:=N;
  238. N1:=StrToInt(N);
  239. for I := 0 to N1-1 do
  240. Begin
  241. Readln(F,X);
  242. XGrid.Cells[I,0]:=X;
  243. End;
  244. CloseFile(F);
  245. End;
  246. End
  247. Else
  248. Application.MessageBox(PChar(ERRORS[ErrorCode]),'Ошибка',MB_OK);
  249. End;
  250.  
  251. end;
  252. Function DeletBackspace( EditText:String; var KeyChar: Char ):String;
  253. Begin
  254. if (KeyChar=BACKSPACE) then
  255. DeletBackspace:=EditText
  256. Else
  257. DeletBackspace:=EditText+KeyChar;
  258.  
  259. End;
  260.  
  261. procedure TMainForm.CheckEdit(Sender: TObject; var Key: Char; CurEdit:TEdit);
  262. Begin
  263. if Not(Key in DIGITS) And Not(Key=BACKSPACE) Then
  264. Key := NONE
  265. Else
  266. Begin
  267. if (Length(CurEdit.Text)<1) and (Key = '0') then
  268. Key := NONE;
  269. if (Length(CurEdit.Text)>1) and Not(CheckUserArea(StrToInt(DeletBackspace(CurEdit.Text,Key)),MAX_N, MIN_N)) then
  270. Key:=NONE;
  271. End;
  272. End;
  273. procedure TMainForm.SelectEdit(Sender: TObject; var Key: Word; CurEdit:TEdit);
  274. Begin
  275. if (Key = VK_UP) Or (Key=VK_RIGHT)then
  276. SelectNext(CurEdit,False,True)
  277. Else
  278. if (Key = VK_DOWN) Or (Key=VK_LEFT) then
  279. SelectNext(CurEdit,True,True);
  280.  
  281. End;
  282.  
  283.  
  284. function TMainForm.CheckInputFields(Sender: TObject; CurEdit:TEdit;CurStringGrid:TStringGrid):Boolean;
  285. var
  286. N:Byte;
  287. begin
  288.  
  289. CheckInputFields := True ;
  290. if(curEdit.Text='') then
  291. Begin
  292. CheckInputFields:=false;
  293. CurEdit.SetFocus;
  294. ShowMessage('Введите N!');
  295. End
  296. Else
  297. Begin
  298. N:=StrToInt(curEdit.Text);
  299. If(N<1) then
  300. Begin
  301. CheckInputFields:=false;
  302. CurEdit.Text:='';
  303. CurEdit.SetFocus;
  304. ShowMessage('N не может быть меньше 1!');
  305. End
  306. Else
  307. if Not(CheckCells(Sender,CurEdit,CurStringGrid)) then
  308. Begin
  309. CheckInputFields:=false;
  310. CurStringGrid.SetFocus;
  311. ShowMessage('Заполните таблицу до конца!');
  312. End ;
  313. End
  314.  
  315.  
  316.  
  317. End;
  318.  
  319. procedure TMainForm.XGridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
  320. var
  321. I: Integer;
  322. amount: Extended;
  323. begin
  324. for I := 0 to XGrid.ColCount - 1 do
  325. begin
  326. if (XGrid.Cells[I, 0] <> '') then
  327. begin
  328. if not TryStrToFloat(XGrid.Cells[I, 0], amount) then
  329. begin
  330. ShowMessage('Некорректные исходные данные!');
  331. XGrid.Cells[I, 0] := '';
  332. end
  333. else if not CheckXArea(amount, MAX_X, MIN_X) then
  334. begin
  335. ShowMessage('Некорректные исходные данные!');
  336. XGrid.Cells[I, 0] := '';
  337. end;
  338. end;
  339. end;
  340. end;
  341.  
  342. Function SumNumbers(Num: Integer): Integer;
  343. Var
  344. Sum: Integer;
  345. Begin
  346. Sum := 0;
  347. While Num > 0 do
  348. Begin
  349. Sum := Sum + Num Mod 10;
  350. Num := Num Div 10;
  351. End;
  352. SumNumbers := Sum;
  353. End;
  354.  
  355.  
  356.  
  357. procedure TMainForm.CalcResult(Sender:TObject);
  358. Var
  359. I,Count, N: Integer;
  360. MaxSum: Integer;
  361. MaxNum, CurrentSum: Integer;
  362. Begin
  363. MaxSum := 0;
  364. MaxNum := 0;
  365. N:=StrToInt(NEdit.Text);
  366. For I := 0 To N - 1 Do
  367. begin
  368. CurrentSum := SumNumbers(strtoint(XGrid.Cells[I,0]));
  369. If CurrentSum > MaxSum Then
  370. Begin
  371. MaxSum := CurrentSum;
  372. MaxNum := strtoint(XGrid.Cells[I,0]);
  373. End;
  374.  
  375. end;
  376.  
  377. ResultEdit.Text:= 'Элемент с максимальной суммой цифр: '+IntToStr(MaxNum);
  378.  
  379.  
  380. End;
  381. procedure TMainForm.ResultButtonClick(Sender: TObject);
  382. begin
  383. If CheckInputFields(Sender,NEdit, XGrid) Then
  384. CalcResult(Sender);
  385. end;
  386. Var
  387. IsSave:Boolean=False;
  388. procedure TMainForm.SaveMenuClick(Sender: TObject);
  389. Var
  390. FilePath: String;
  391. F: TextFile;
  392. begin
  393. if (ResultEdit.Text<>'') then
  394. Begin
  395. if MySaveTextFileDialog.Execute then
  396. Begin
  397. FilePath:=MySaveTextFileDialog.FileName;
  398. AssignFile(F,FilePath);
  399. If FileIsReadOnly(FilePath) Then
  400. Application.MessageBox(PChar(ERRORS[ecNot_Writeable]),'Ошибка',MB_OK+MB_ICONERROR)
  401. Else
  402. Begin
  403. Rewrite(F);
  404. Write(F,'Элемент с максимальной суммой: ', ResultEdit.Text);
  405. CloseFile(F);
  406. IsSave:=True;
  407. End;
  408. End;
  409. End;
  410.  
  411.  
  412. end;
  413. procedure TMainForm.FileMenuClick(Sender: TObject);
  414. begin
  415. if ResultButton.Enabled then
  416. SaveMenu.Enabled:=true
  417. Else
  418. SaveMenu.Enabled:=false;
  419. end;
  420.  
  421. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  422. Var
  423. IsExit:Word;
  424. begin
  425. If Not IsSave And (ResultEdit.Text<>'') Then
  426. Begin
  427. IsExit:= Application.MessageBox('Вы не сохранили файл, хотите ли сохранить?','Выход',MB_YESNOCANCEL+MB_ICONQUESTION);
  428. case IsExit of
  429. mrYes:
  430. Begin
  431. CanClose:=False;
  432. FileMenu.Click;
  433. SaveMenu.Click;
  434. End;
  435.  
  436. mrNo:
  437. CanClose:=True;
  438. mrCancel:
  439. CanClose:=False;
  440. end;
  441.  
  442.  
  443. End
  444. Else
  445. Begin
  446. IsExit:= Application.MessageBox('Вы действительно хотите выйти?','Выход',MB_YESNO+MB_ICONQUESTION);
  447. case IsExit of
  448. mrYes:
  449. CanClose:=True;
  450. mrNo:
  451. CanClose:=False;
  452. end;
  453. End;
  454. end;
  455.  
  456. function TMainForm.CheckCells(Sender: TObject; CurEdit:TEdit;CurStringGrid:TStringGrid):Boolean;
  457. Var
  458. N,I:Integer;
  459. IsNotExist:Boolean;
  460. begin
  461. IsNotExist := true;
  462. for I := 0 to N do
  463. Begin
  464. if(CurStringGrid.Cells[I,0]='') then
  465. Begin
  466. IsNotExist := false;
  467. Break;
  468. End;
  469. End;
  470. CheckCells:=IsNotExist;
  471. end;
  472.  
  473.  
  474. procedure TMainForm.ExitMenuClick(Sender: TObject);
  475. Var
  476. IsExit:Word;
  477. begin
  478. If Not IsSave And (ResultEdit.Text<>'') Then
  479. Begin
  480. IsExit:= Application.MessageBox('Вы не сохранили файл, хотите ли сохранить?','Выход',MB_YESNOCANCEL+MB_ICONQUESTION);
  481. case IsExit of
  482. mrYes:
  483. SaveMenu.Click;
  484. mrNo:
  485. Close;
  486. end;
  487.  
  488.  
  489. End
  490. Else
  491. Begin
  492. IsExit:= Application.MessageBox('Вы действительно хотите выйти?','Выход',MB_YESNO+MB_ICONQUESTION);
  493. case IsExit of
  494. mrYes:
  495. Close;
  496. end;
  497. End;
  498. end;
  499.  
  500. end.
Add Comment
Please, Sign In to add comment