Advertisement
ksyshshot

Lab.1.4

Jan 30th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.67 KB | Source Code | 0 0
  1. unit Lab_1_4_Forms;
  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.Grids, Vcl.StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     MainMenu1: TMainMenu;
  12.     LabelTask: TLabel;
  13.     LabelGetLength: TLabel;
  14.     EditLengthArr: TEdit;
  15.     ButtonFillArr: TButton;
  16.     SpreadSheep: TStringGrid;
  17.     LabelSum: TLabel;
  18.     EditSum: TEdit;
  19.     ButtonFile: TMenuItem;
  20.     ButtonOpenFile: TMenuItem;
  21.     ButtonSaveFile: TMenuItem;
  22.     ButtonInstruction: TMenuItem;
  23.     ButtonAbout: TMenuItem;
  24.     ButtonFindSum: TButton;
  25.     OpenDialog1: TOpenDialog;
  26.     SaveDialog1: TSaveDialog;
  27.     procedure EditLengthArrKeyPress(Sender: TObject; var Key: Char);
  28.     procedure EditLengthArrChange(Sender: TObject);
  29.     procedure ButtonFillArrClick(Sender: TObject);
  30.     procedure SpreadSheepSelectCell(Sender: TObject; ACol, ARow: Integer;
  31.       var CanSelect: Boolean);
  32.     procedure SpreadSheepKeyPress(Sender: TObject; var Key: Char);
  33.     procedure ButtonFindSumClick(Sender: TObject);
  34.     procedure ButtonOpenFileClick(Sender: TObject);
  35.     procedure ButtonAboutClick(Sender: TObject);
  36.     procedure ButtonInstructionClick(Sender: TObject);
  37.     procedure ButtonSaveFileClick(Sender: TObject);
  38.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.   end;
  44.  
  45. var
  46.   Form1: TForm1;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. uses UnitError, UnitAbout, UnitInstruction, UnitExit;
  53.  
  54. const
  55.     MIN_ELEMENT = -20;
  56.     MAX_ELEMENT = 20;
  57.     MIN_SIZE = 3;
  58.     MAX_SIZE = 20;
  59.  
  60. Type
  61.     TArr = Array Of Integer;
  62. Var
  63.     LengthArr: Integer;
  64.     Arr: TArr;
  65.  
  66. function FillArr(Sender: TObject; ArrLen: Integer; SprSheep: TStringGrid; var IsCorrect: Boolean): TArr;
  67. var
  68.     I: Integer;
  69.     A: TArr;
  70. begin
  71.     SetLength(A, LengthArr);
  72.     I := 1;
  73.     IsCorrect := True;
  74.     while (IsCorrect) and (I <= ArrLen)do
  75.     begin
  76.         try
  77.             A[I - 1] := StrToInt(SprSheep.Cells[I, 1]);
  78.         except
  79.             IsCorrect := False;
  80.             UnitError.FormError.LabelError.Caption := 'Ошибка! Получено некорректное значение элемента массива!';
  81.             UnitError.FormError.ShowModal();
  82.         end;
  83.         Inc(I);
  84.     end;
  85.     FillArr := A;
  86. end;
  87.  
  88. procedure FillNumbersSpreadSheep(Sender: TObject; ArrLen: Integer; SprSheep: TStringGrid);
  89. var
  90.     I: Integer;
  91.     CanChange: Boolean;
  92. begin
  93.     for I := 1 to ArrLen do
  94.     begin
  95.         SprSheep.Cells[I, 0] := '№' + IntToStr(I);
  96.     end;
  97. end;
  98.  
  99. procedure TForm1.ButtonAboutClick(Sender: TObject);
  100. begin
  101.     UnitAbout.FormAbout.ShowModal();
  102. end;
  103.  
  104. procedure TForm1.ButtonFillArrClick(Sender: TObject);
  105. var
  106.     IsCorrect: Boolean;
  107.     I: Integer;
  108. begin
  109.     ButtonFillArr.Enabled := False;
  110.     IsCorrect := True;
  111.     try
  112.         LengthArr := StrToInt(EditLengthArr.Text);
  113.     except
  114.         IsCorrect := False;
  115.         UnitError.FormError.LabelError.Caption := 'Ошибка! Получено некорректное значение длины массива!';
  116.         UnitError.FormError.ShowModal();
  117.     end;
  118.     if (IsCorrect) then
  119.     begin
  120.         SpreadSheep.ColCount := LengthArr + 1;
  121.         SetLength(Arr, LengthArr);
  122.         SpreadSheep.ColWidths[0] := 80;
  123.         SpreadSheep.Cells[0,0] := '№ элемента';
  124.         SpreadSheep.Cells[0,1] := 'Элемент';
  125.         FillNumbersSpreadSheep(Sender, LengthArr, SpreadSheep);
  126.         SpreadSheep.Visible := True;
  127.         SpreadSheep.Enabled := True;
  128.         for I := 1 to LengthArr do
  129.         begin
  130.             SpreadSheepSelectCell(Sender, I, 1, IsCorrect);
  131.         end;
  132.         ButtonFindSum.Visible := True;
  133.         ButtonFindSum.Enabled := True;
  134.     end;
  135. end;
  136.  
  137. procedure TForm1.ButtonFindSumClick(Sender: TObject);
  138. var
  139.     IsFill, IsCorrect: Boolean;
  140.     I, Sum: Integer;
  141. begin
  142.     Sum := 0;
  143.     IsCorrect := True;
  144.     for I := 1 to LengthArr do
  145.         if (SpreadSheep.Cells[I, 1].IsEmpty) then
  146.             IsFill := False
  147.         else
  148.             IsFill := True;
  149.     if (IsFill) then
  150.         Arr := FillArr(Sender, LengthArr, SpreadSheep, IsCorrect)
  151.     else
  152.     begin
  153.         UnitError.FormError.LabelError.Caption := 'Ошибка! Получено недостаточное количество элементов массива!';
  154.         UnitError.FormError.ShowModal();
  155.         IsCorrect := False;
  156.     end;
  157.     if (IsCorrect) then
  158.     begin
  159.         LabelSum.Visible := True;
  160.         for I := 0 to High(Arr) do
  161.             Sum := Sum + (I + 1) * Arr[I];
  162.         EditSum.Text := IntToStr(Sum);
  163.         EditSum.Visible := True;
  164.         ButtonSaveFile.Enabled := True;
  165.     end;
  166. end;
  167.  
  168. procedure TForm1.ButtonInstructionClick(Sender: TObject);
  169. begin
  170.     UnitInstruction.FormInstruction.ShowModal();
  171. end;
  172.  
  173. procedure TForm1.ButtonOpenFileClick(Sender: TObject);
  174. var
  175.     F: TextFile;
  176.     Path, Error: String;
  177.     I, Res, Size: Integer;
  178.     IsCorrect: Boolean;
  179.     A: TArr;
  180. begin
  181.     IsCorrect := True;
  182.     Error := '';
  183.     Res := 0;
  184.     Size := 0;
  185.     If OpenDialog1.Execute() Then
  186.     Begin
  187.         Path := OpenDialog1.FileName;
  188.         AssignFile(F, Path);
  189.         Try
  190.             Reset(F);
  191.             Try
  192.                 Readln(F, Size);
  193.                 if (Size < MIN_SIZE) or (Size > MAX_SIZE) then
  194.                 begin
  195.                     IsCorrect := False;
  196.                     Error := Error + 'Размер массива за пределами диапазона допустимых значений. ';
  197.                 end;
  198.                 I := 0;
  199.                 SetLength(A, Size);
  200.                 While (IsCorrect) and (I < Size) Do
  201.                 Begin
  202.                     Read(F, A[I]);
  203.                     If (A[I] < MIN_ELEMENT) Or (A[I] > MAX_ELEMENT) Then
  204.                     Begin
  205.                         IsCorrect := False;
  206.                         Error := Error + 'Размер элемента за пределами диапазона допустимых значений. ';
  207.                     End;
  208.                     if (I < High(A)) and (EOF(F)) then
  209.                     begin
  210.                         IsCorrect := False;
  211.                         Error := Error + 'Недостаточно элементов в файле. ';
  212.                     end;
  213.                     Inc(I);
  214.                 End;
  215.             Finally
  216.                 CloseFile(F);
  217.             End;
  218.         Except
  219.             IsCorrect := False;
  220.             Error := Error + 'Нет доступа к файлу';
  221.         End;
  222.         If not(IsCorrect) Then
  223.         Begin
  224.             UnitError.FormError.LabelError.Caption := 'Ошибка считывания с файла. ' + Error;
  225.             Res := UnitError.FormError.ShowModal();
  226.         End;
  227.         if Res > 0 Then
  228.             UnitError.FormError.LabelError.Caption := ''
  229.         else
  230.         begin
  231.             LengthArr := Size;
  232.             EditLengthArr.Text := IntToStr(Size);
  233.             ButtonFillArrClick(Sender);
  234.             SpreadSheep.Enabled := False;
  235.             for I := 1 to LengthArr do
  236.             begin
  237.                 SpreadSheep.Cells[I, 1] := IntToStr(A[I - 1]);
  238.             end;
  239.         end;
  240.     End;
  241. end;
  242.  
  243. procedure TForm1.ButtonSaveFileClick(Sender: TObject);
  244. var
  245.     F: TextFile;
  246.     Path, Error: String;
  247.     Res, Answer: Integer;
  248.     IsCorrect: Boolean;
  249. begin
  250.     IsCorrect := True;
  251.     Res := 0;
  252.     Error := '';
  253.     if SaveDialog1.Execute() then
  254.     begin
  255.         Path := SaveDialog1.FileName;
  256.         AssignFile(F, Path);
  257.         Try
  258.             Rewrite(F);
  259.             Try
  260.                 Write(F, 'Полученная сумма: ', EditSum.Text)
  261.             Finally
  262.                 CloseFile(F);
  263.             End;
  264.         Except
  265.             IsCorrect := False;
  266.             Error := 'Нет доступа к файлу';
  267.         End;
  268.         if not(IsCorrect) then
  269.         begin
  270.             UnitError.FormError.LabelError.Caption := 'Ошибка считывания с файла. ' + Error;
  271.             Res := UnitError.FormError.ShowModal();
  272.         end;
  273.         if Res > 0 Then
  274.             UnitError.FormError.LabelError.Caption := '';
  275.     end;
  276. end;
  277.  
  278. procedure TForm1.EditLengthArrChange(Sender: TObject);
  279. var
  280.     I, J: Integer;
  281. begin
  282.     ButtonFillArr.Enabled := True;
  283.     SpreadSheep.Visible := False;
  284.     SpreadSheep.Enabled := False;
  285.     ButtonSaveFile.Enabled := False;
  286.     ButtonFindSum.Visible := False;
  287.     ButtonFindSum.Enabled := False;
  288.     EditSum.Visible := False;
  289.     LabelSum.Visible := False;
  290.     for I := 0 to SpreadSheep.ColCount - 1 do
  291.         for J := 0 to SpreadSheep.RowCount - 1 do
  292.             SpreadSheep.Cells[I, J] := '';
  293.     if EditlengthArr.Text = '' then
  294.     begin
  295.         ButtonFillArr.Enabled := False;
  296.     end;
  297. end;
  298.  
  299. procedure TForm1.EditLengthArrKeyPress(Sender: TObject; var Key: Char);
  300. Var
  301.     Number: Integer;
  302. begin
  303.     if not(Key in ['0'..'9', #8, #13])then
  304.         Key := #0;
  305.     if (Length(EditLengthArr.Text) = 0) and (Key = '0') Then
  306.         Key := #0;
  307.     if (Length(EditLengthArr.Text) = 1) and (EditLengthArr.Text[1] = '2') and (Key in ['1'..'9']) then
  308.         Key := #0;
  309.     if (Length(EditLengthArr.Text) = 1) and not(EditLengthArr.Text[1] in ['2', '1']) and (Key in ['0'..'9']) then
  310.         Key := #0;
  311.     if (Length(EditLengthArr.Text) > 0) and (Key = #13) then
  312.         ButtonFillArrClick(Sender);
  313. end;
  314.  
  315. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  316. Var
  317.     Res: Integer;
  318. begin
  319.     Res := UnitExit.FormExit.ShowModal();
  320.     If Res = mrOk Then
  321.         CanClose := True
  322.     Else
  323.         CanClose := False;
  324. end;
  325.  
  326. procedure TForm1.SpreadSheepKeyPress(Sender: TObject; var Key: Char);
  327. var
  328.     MaxLength: Integer;
  329. begin
  330.     if not(Key in ['0'..'9', '-', #8, #13])then
  331.         Key := #0;
  332.     MaxLength := 2;
  333.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) > 0) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '-') then
  334.         MaxLength := 3;
  335.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) > 0) and (Key = '-') then
  336.         Key := #0;
  337.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) > 0) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '0') and not(Key = #8) Then
  338.         Key := #0;
  339.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 1) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '2') and (Key in ['1'..'9']) then
  340.         Key := #0;
  341.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 1) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] in ['3'..'9']) and (Key in ['0'..'9']) then
  342.         Key := #0;
  343.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 2) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row] = '-2') and not(Key in ['0', #8]) then
  344.         Key := #0;
  345.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 1) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row] = '-') and (Key = '0') then
  346.         Key := #0;
  347.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 2) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '-') and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][2] in ['3'..'9']) and (Key in ['0'..'9']) then
  348.         Key := #0;
  349.     if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = MaxLength) and not(Key = #8)then
  350.         Key := #0;
  351. end;
  352.  
  353. procedure TForm1.SpreadSheepSelectCell(Sender: TObject; ACol, ARow: Integer;
  354.   var CanSelect: Boolean);
  355. begin
  356.     if (ACol = 0) or (ARow = 0) then
  357.         CanSelect := False;
  358. end;
  359.  
  360. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement