Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.60 KB | None | 0 0
  1. unit LAB_4_1_Form;
  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.Grids, Vcl.StdCtrls, Vcl.Menus,
  9.    AboutDev;
  10.  
  11. const
  12.    MAX_GRID_WIDTH_AMOUNT = 8;
  13.    MAX_GRID_HEIGHT_AMOUNT = 8;
  14.    MAX_EDT_LEN = 9;
  15.    BYTE_MAX_VAL = 255;
  16.  
  17. type
  18.    T2DArray = array of array of Integer;
  19.    TSet = set of Byte;
  20.  
  21.    TForm1 = class(TForm)
  22.       sgA: TStringGrid;
  23.       btnCreate: TButton;
  24.       Button1: TButton;
  25.       btnClear: TButton;
  26.       lblN: TLabel;
  27.       edtN: TEdit;
  28.       btnCalc: TButton;
  29.       lblAns: TLabel;
  30.       edtAns: TEdit;
  31.       MainMenu1: TMainMenu;
  32.       N1: TMenuItem;
  33.       N2: TMenuItem;
  34.       N3: TMenuItem;
  35.       N4: TMenuItem;
  36.       N5: TMenuItem;
  37.       N6: TMenuItem;
  38.       odDialog: TOpenDialog;
  39.       sdDialog: TSaveDialog;
  40.       procedure btnCalcClick(Sender: TObject);
  41.       procedure btnCreateClick(Sender: TObject);
  42.       procedure Button1Click(Sender: TObject);
  43.       procedure FormCreate(Sender: TObject);
  44.       procedure btnClearClick(Sender: TObject);
  45.       procedure sgAKeyPress(Sender: TObject; var Key: Char);
  46.       procedure edtNKeyPress(Sender: TObject; var Key: Char);
  47.       procedure N6Click(Sender: TObject);
  48.       procedure N5Click(Sender: TObject);
  49.       procedure N2Click(Sender: TObject);
  50.       procedure N3Click(Sender: TObject);
  51.    private
  52.       { Private declarations }
  53.    public
  54.       { Public declarations }
  55.    end;
  56.  
  57. var
  58.    Form1: TForm1;
  59.    Matr: T2DArray;
  60.  
  61. implementation
  62.  
  63. {$R *.dfm}
  64.  
  65. function Det(M: T2DArray; P: Integer; Exc: TSet): Integer;
  66. var
  67.    I: Byte;
  68.    Ans, Mul, BDet, IP: Integer;
  69. begin
  70.    Ans := 0;
  71.    IP := 0;
  72.    for I := 0 to Length(M[0]) - 1 do
  73.       if not(I in Exc) then
  74.       begin
  75.          if P = Length(M[0]) - 1 then
  76.          begin
  77.             BDet := M[P, I];
  78.             Ans := BDet;
  79.          end
  80.          else
  81.          begin
  82.             if (IP mod 2 = 0) then
  83.                Mul := 1
  84.             else
  85.                Mul := -1;
  86.             Include(Exc, I);
  87.             BDet := Det(M, P + 1, Exc);
  88.             Exclude(Exc, I);
  89.             Ans := Ans + Mul * M[P, I] * BDet;
  90.          end;
  91.          Inc(IP);
  92.       end;
  93.    Result := Ans;
  94. end;
  95.  
  96. procedure CorrectStringGridView(var Grid: TStringGrid);
  97. var
  98.    Mul: Integer;
  99. begin
  100.  
  101.    if Grid.Name = 'sgA' then
  102.       Mul := MAX_GRID_WIDTH_AMOUNT
  103.    else
  104.       Mul := 1;
  105.    if Grid.ColCount > MAX_GRID_WIDTH_AMOUNT then
  106.       Grid.Height := Grid.DefaultRowHeight * Mul +
  107.         GetSystemMetrics(SM_CXHSCROLL) + 3 + Mul
  108.    else
  109.       Grid.Height := Grid.DefaultRowHeight * Mul + 3 + Mul;
  110.    if Grid.RowCount > MAX_GRID_HEIGHT_AMOUNT then
  111.       Grid.Width := Grid.DefaultColWidth * Mul +
  112.         GetSystemMetrics(SM_CXHSCROLL) + 3 + Mul
  113.    else
  114.       Grid.Width := Grid.DefaultColWidth * MAX_GRID_WIDTH_AMOUNT + 3 +
  115.         MAX_GRID_WIDTH_AMOUNT;
  116.  
  117. end;
  118.  
  119. procedure TForm1.btnCreateClick(Sender: TObject);
  120. var
  121.    N: Integer;
  122. begin
  123.    with sgA do
  124.    begin
  125.       try
  126.          N := StrToInt(edtN.Text);
  127.       except
  128.          MessageBox(0, PChar('Поля не дожны быть пустыми!'), 'Внимание',
  129.            MB_ICONWARNING);
  130.          Exit;
  131.       end;
  132.       if N > 11 then
  133.       begin
  134.          MessageBox(0, PChar('Порядок матрицы не должн превышать 11'),
  135.            'Внимание', MB_ICONWARNING);
  136.          Exit;
  137.       end;
  138.       ColCount := N;
  139.       RowCount := N;
  140.    end;
  141.    CorrectStringGridView(sgA);
  142. end;
  143.  
  144. procedure EnableOutElements;
  145. begin
  146.    with Form1 do
  147.    begin
  148.       lblAns.Visible := True;
  149.       edtAns.Visible := True;
  150.    end;
  151. end;
  152.  
  153. procedure DisableOutElements;
  154. begin
  155.    with Form1 do
  156.    begin
  157.       lblAns.Visible := False;
  158.       edtAns.Visible := False;
  159.    end;
  160. end;
  161.  
  162. procedure TForm1.Button1Click(Sender: TObject);
  163. var
  164.    I, J: Integer;
  165. begin
  166.    Randomize;
  167.    with sgA do
  168.    begin
  169.       SetLength(Matr, ColCount, ColCount);
  170.       for I := 0 to ColCount - 1 do
  171.          for J := 0 to ColCount - 1 do
  172.             Cells[I, J] := IntToStr(1 + Random(ColCount * ColCount));
  173.    end;
  174. end;
  175.  
  176. procedure TForm1.btnClearClick(Sender: TObject);
  177. var
  178.    I, J: Integer;
  179. begin
  180.    DisableOutElements;
  181.    with sgA do
  182.    begin
  183.       for I := 0 to ColCount - 1 do
  184.          for J := 0 to ColCount - 1 do
  185.             Cells[J, I] := '';
  186.       ColCount := 0;
  187.       RowCount := 0;
  188.    end;
  189.    edtN.Text := '';
  190. end;
  191.  
  192. procedure TForm1.btnCalcClick(Sender: TObject);
  193. var
  194.    I, J: Integer;
  195. begin
  196.    with sgA do
  197.    begin
  198.       SetLength(Matr, ColCount, ColCount);
  199.       for I := 0 to ColCount - 1 do
  200.          for J := 0 to ColCount - 1 do
  201.             try
  202.                Matr[I, J] := StrToInt(Cells[J, I]);
  203.             except
  204.                MessageBox(0, PChar('Ячейки не должны быть пустыми'),
  205.                  PChar('Внимание!'), MB_ICONERROR);
  206.                Exit;
  207.             end;
  208.    end;
  209.    edtAns.Text := IntToStr(Det(Matr, 0, []));
  210.    EnableOutElements;
  211. end;
  212.  
  213. procedure CorrectExtendedInput(var EditText: TEdit; var Key: Char);
  214. begin
  215.    if not(Key in [#8, #13, '0' .. '9', ',']) then
  216.    begin
  217.       Key := #0;
  218.    end
  219.    else if (Key = ',') and (Pos(Key, EditText.Text) > 0) then
  220.    begin
  221.       Key := #0;
  222.    end
  223. end;
  224.  
  225. procedure TForm1.edtNKeyPress(Sender: TObject; var Key: Char);
  226. begin
  227.    DisableOutElements;
  228.    CorrectExtendedInput(edtN, Key);
  229. end;
  230.  
  231. procedure TForm1.FormCreate(Sender: TObject);
  232. begin
  233.    with sgA do
  234.    begin
  235.       ColWidths[0] := DefaultColWidth;
  236.       Width := DefaultColWidth * MAX_GRID_WIDTH_AMOUNT + 3 +
  237.         MAX_GRID_WIDTH_AMOUNT;
  238.       Height := DefaultRowHeight * MAX_GRID_WIDTH_AMOUNT + 3 +
  239.         MAX_GRID_WIDTH_AMOUNT;
  240.    end;
  241. end;
  242.  
  243. procedure TForm1.N2Click(Sender: TObject);
  244. var
  245.    InputFile: TextFile;
  246.    FileName: String;
  247.    StopReadN: Boolean;
  248.    I, J, ErrorPos, N, Buf: Integer;
  249.    RawInput: String;
  250. begin
  251.    if odDialog.Execute then
  252.    begin
  253.       FileName := odDialog.FileName;
  254.       AssignFile(InputFile, FileName);
  255.       if FileExists(FileName) then
  256.       begin
  257.          Reset(InputFile);
  258.          StopReadN := False;
  259.          Readln(InputFile, RawInput);
  260.          edtN.Text := RawInput;
  261.          Val(RawInput, N, ErrorPos);
  262.          if N > 0 then
  263.          begin
  264.             StopReadN := True;
  265.             edtN.Text := IntToStr(N);
  266.          end
  267.          else
  268.             ShowMessage('Порядок матрицы должен быть больше нуля...');
  269.          if StopReadN then
  270.          begin
  271.             btnCreate.Click;
  272.             CorrectStringGridView(sgA);
  273.             for I := 0 to N - 1 do
  274.             begin
  275.                for J := 0 to N - 1 do
  276.                begin
  277.                   if SeekEof(InputFile) then
  278.                   begin
  279.                      MessageBox(0, PChar('Недостаточно данных.'), 'Warning',
  280.                        MB_OK + MB_ICONERROR);
  281.                      btnClear.Click;
  282.                      Break;
  283.                   end
  284.                   else
  285.                   begin
  286.                      try
  287.                         Read(InputFile, Buf);
  288.                         sgA.Cells[J, I] := IntToStr(Buf);
  289.                      except
  290.                         MessageBox(0,
  291.                           PChar('Файл содержит некорректные данные...'),
  292.                           'Warning', MB_OK + MB_ICONERROR);
  293.                         btnClear.Click;
  294.                         DisableOutElements;
  295.                         Exit;
  296.                      end;
  297.                   end;
  298.                end;
  299.             end;
  300.          end;
  301.          CloseFile(InputFile);
  302.       end;
  303.    end;
  304. end;
  305.  
  306. procedure TForm1.N3Click(Sender: TObject);
  307. var
  308.    OutputFile: TextFile;
  309.    FileName: String;
  310.    buttonSelected: Integer;
  311.    I, J: Integer;
  312. begin
  313.    if sdDialog.Execute then
  314.    begin
  315.       FileName := sdDialog.FileName;
  316.       AssignFile(OutputFile, FileName);
  317.       if FileExists(FileName) then
  318.       begin
  319.          buttonSelected := MessageDlg('Такой фай уже существует, перезаписать?',
  320.            mtCustom, [mbYes, mbNo], 0);
  321.          if buttonSelected = mrNo then
  322.             Append(OutputFile)
  323.          else
  324.          begin
  325.             Rewrite(OutputFile);
  326.          end;
  327.       end
  328.       else
  329.          Rewrite(OutputFile);
  330.       Writeln(OutputFile, IntToStr(sgA.ColCount));
  331.       for I := 0 to sgA.ColCount - 1 do
  332.       begin
  333.          for J := 0 to sgA.ColCount - 1 do
  334.             Write(OutputFile, sgA.Cells[J, I] + ' ');
  335.          Writeln(OutputFile);
  336.       end;
  337.       CloseFile(OutputFile);
  338.    end;
  339. end;
  340.  
  341. procedure TForm1.N5Click(Sender: TObject);
  342. begin
  343.    MessageBox(0, 'Задание:' + #13#13 +
  344.      'Вычислить определитель заданной матрицы, пользуясь формулой разложения по первой строке.',
  345.      'О программе', MB_OK);
  346. end;
  347.  
  348. procedure TForm1.N6Click(Sender: TObject);
  349. begin
  350.    AboutMe.Show;
  351. end;
  352.  
  353. procedure TForm1.sgAKeyPress(Sender: TObject; var Key: Char);
  354. begin
  355.    with sgA do
  356.    begin
  357.       if not(Key in [#9, #8, '0' .. '9', '-']) then
  358.       begin
  359.          Key := #0;
  360.       end
  361.       else if (Key = '-') and (Pos(Key, Cells[Col, Row]) > 0) then
  362.       begin
  363.          Key := #0;
  364.       end
  365.       else if (Key = '-') and (Cells[Col, Row] <> '') then
  366.       begin
  367.          Key := #0;
  368.       end
  369.       else if (Length(Cells[Col, Row]) + 1 > MAX_EDT_LEN) and not(Key = #8) then
  370.          Key := #0
  371.    end;
  372.    DisableOutElements;
  373. end;
  374.  
  375. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement