Advertisement
Guest User

Untitled

a guest
May 20th, 2019
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 12.38 KB | None | 0 0
  1. unit UnitTransformMatrix;
  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.ComCtrls, Vcl.StdCtrls, Vcl.Grids,
  8.   Vcl.Menus, Vcl.ExtCtrls;
  9.  
  10.  
  11. type
  12.    TMatrix = array of array of Integer;
  13.  
  14. type
  15.   TTransformMatrix = class(TForm)
  16.     Label1: TLabel;
  17.     edtCountOfNodes: TEdit;
  18.     UpDown1: TUpDown;
  19.     btCreateMatrix: TButton;
  20.     sgComplexityMatrix: TStringGrid;
  21.     Label2: TLabel;
  22.     Label3: TLabel;
  23.     cbFirstNode: TComboBox;
  24.     cbSecondNode: TComboBox;
  25.     Label4: TLabel;
  26.     btAddAdge: TButton;
  27.     PopupMenu1: TPopupMenu;
  28.     pmForSG: TPopupMenu;
  29.     ItemAddNode: TMenuItem;
  30.     ItemDeleteNode: TMenuItem;
  31.     InputFile: TOpenDialog;
  32.     SaveFile: TSaveDialog;
  33.     MainMenu: TMainMenu;
  34.     N1: TMenuItem;
  35.     InputData: TMenuItem;
  36.     SaveData: TMenuItem;
  37.     N2: TMenuItem;
  38.     ItemExit: TMenuItem;
  39.     N3: TMenuItem;
  40.     AboutAuthor: TMenuItem;
  41.     AboutProgramm: TMenuItem;
  42.     btClear: TButton;
  43.     btTransform: TButton;
  44.     Label5: TLabel;
  45.     Label6: TLabel;
  46.     sgIncidenceMatrix: TStringGrid;
  47.     procedure btCreateMatrixClick(Sender: TObject);
  48.     procedure edtCountOfNodesKeyPress(Sender: TObject; var Key: Char);
  49.     procedure btAddAdgeClick(Sender: TObject);
  50.     procedure ClearStringGrid;
  51.     procedure ItemDeleteNodeClick(Sender: TObject);
  52.     procedure ItemAddNodeClick(Sender: TObject);
  53.     procedure AboutAuthorClick(Sender: TObject);
  54.     procedure AboutProgrammClick(Sender: TObject);
  55.     procedure InputDataClick(Sender: TObject);
  56.     procedure SaveDataClick(Sender: TObject);
  57.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  58.     procedure ItemExitClick(Sender: TObject);
  59.     procedure btTransformClick(Sender: TObject);
  60.     procedure btClearClick(Sender: TObject);
  61.   private
  62.     { Private declarations }
  63.   public
  64.     { Public declarations }
  65.   end;
  66.  
  67. const
  68.    CellWidth = 29;
  69.    CellHeight = 29;
  70.  
  71. var
  72.    TransformMatrix: TTransformMatrix;
  73.    MyNodes: array of TPoint;
  74.  
  75. implementation
  76.  
  77. {$R *.dfm}
  78.  
  79. procedure TTransformMatrix.btClearClick(Sender: TObject);
  80. begin
  81.    SaveData.Enabled := False;
  82.    ClearStringGrid;
  83.    sgComplexityMatrix.Width := CellWidth;
  84.    sgIncidenceMatrix.Width := CellWidth;
  85.    sgComplexityMatrix.Height := CellHeight;
  86.    sgIncidenceMatrix.Height := CellHeight;
  87.    sgComplexityMatrix.RowCount := 1;
  88.    sgComplexityMatrix.ColCount := 1;
  89.    sgIncidenceMatrix.RowCount := 1;
  90.    sgComplexityMatrix.ColCount := 1;
  91.    cbFirstNode.Clear;
  92.    cbSecondNode.Clear;
  93. end;
  94.  
  95. procedure TTransformMatrix.btCreateMatrixClick(Sender: TObject);
  96. var
  97.    Count, i, j : Integer;
  98. begin
  99.    SaveData.Enabled := False;
  100.    ClearStringGrid;
  101.    if (edtCountOfNodes.Text <> '0') and (edtCountOfNodes.Text <> '') then
  102.    begin
  103.       cbFirstNode.Clear;
  104.       cbSecondNode.Clear;
  105.       Count := StrToInt(edtCountOfNodes.Text) + 1;
  106.       sgComplexityMatrix.Width := Count * CellWidth;
  107.       sgComplexityMatrix.Height := Count * CellHeight;
  108.       sgIncidenceMatrix.Width := CellWidth;
  109.       sgIncidenceMatrix.Height := Count * CellHeight;
  110.       sgComplexityMatrix.RowCount := Count;
  111.       sgComplexityMatrix.ColCount := Count;
  112.       sgIncidenceMatrix.RowCount := Count;
  113.       for i := 0 to Count - 1 do
  114.       begin
  115.          sgComplexityMatrix.Cells[i, 0] := IntToStr(i);
  116.          sgComplexityMatrix.Cells[0, i] := IntToStr(i);
  117.          sgIncidenceMatrix.Cells[0, i] := IntToStr(i);
  118.       end;
  119.       sgComplexityMatrix.Cells[0, 0] := '';
  120.       sgIncidenceMatrix.Cells[0, 0] := '';
  121.       for i := 1 to Count - 1 do
  122.       begin
  123.          cbFirstNode.Items.Add(IntToStr(i));
  124.          cbSecondNode.Items.Add(IntToStr(i));
  125.       end;
  126.    end
  127.    else
  128.       MessageBox(Handle, PChar('Допустипое количество вершин от 1 до 9'), PChar(''), MB_ICONSTOP + MB_OK);
  129. end;
  130.  
  131. procedure TTransformMatrix.btTransformClick(Sender: TObject);
  132. const
  133.    MsgNotAdges = 'В заданном графе нет ни одного ребра!';
  134.    MaxWidth = 350;
  135. var
  136.    Count, i, j, CountOfAdge, NumberOfAdge: Integer;
  137. begin
  138.    Count := sgComplexityMatrix.RowCount - 1;
  139.    CountOfAdge := 0;
  140.    for i := 1 to Count do
  141.       for j := i + 1 to Count do
  142.          if sgComplexityMatrix.Cells[j, i] = '+' then
  143.             Inc(CountOfAdge);
  144.    if CountOfAdge <> 0 then
  145.    begin
  146.       for i := 1 to sgIncidenceMatrix.ColCount - 1 do
  147.          sgIncidenceMatrix.Cols[i].Clear;
  148.       sgIncidenceMatrix.ColCount := CountOfAdge + 1;
  149.       sgIncidenceMatrix.Width := (CountOfAdge + 1) * CellWidth;
  150.       if sgIncidenceMatrix.Width > MaxWidth then
  151.       begin
  152.          sgIncidenceMatrix.Width := MaxWidth;
  153.          sgIncidenceMatrix.ScrollBars := ssHorizontal;
  154.       end
  155.       else
  156.          sgIncidenceMatrix.ScrollBars := ssNone;
  157.       for i := 1 to CountOfAdge do
  158.          sgIncidenceMatrix.Cells[i, 0] := 'e' + IntToStr(i);
  159.       NumberOfAdge := 0;
  160.       for i := 1 to Count do
  161.          for j := i + 1 to Count do
  162.             if sgComplexityMatrix.Cells[j, i] = '+' then
  163.             begin
  164.                Inc(NumberOfAdge);
  165.                sgIncidenceMatrix.Cells[NumberOfAdge, i] := '1';
  166.                sgIncidenceMatrix.Cells[NumberOfAdge, j] := '1';
  167.             end;
  168.    end
  169.    else
  170.       MessageBox(Handle, MsgNotAdges, 'Внимание!', MB_ICONINFORMATION + MB_OK);
  171. end;
  172.  
  173. procedure TTransformMatrix.InputDataClick(Sender: TObject);
  174. var
  175.    Input : TextFile;
  176.    Count, i, j, First, Second: Integer;
  177.    IsCorrect : Boolean;
  178. begin
  179. //   btClearClick(Sender);
  180.    if InputFile.Execute then
  181.    begin
  182.       AssignFile(Input, InputFile.FileName);
  183.       Reset(Input);
  184.       if EoF(Input) then
  185.       begin
  186.          MessageBox(Handle, PChar('Файл не содержит необходимые данные!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
  187.          CloseFile(Input);
  188.          InputDataClick(Sender);
  189.       end
  190.       else
  191.       begin
  192.          Readln(Input);
  193.          if EoF(Input) then
  194.             MessageBox(Handle, PChar('В файле отсутвуют элементы массива!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK)
  195.          else
  196.          begin
  197.             Reset(Input);
  198.             try
  199.                Readln(Input, Count);
  200.                IsCorrect := True;
  201.                if (Count > 0) and (Count < 10) then
  202.                begin
  203.                   edtCountOfNodes.Text := IntToStr(Count);
  204.                   btCreateMatrixClick(Sender);
  205.                end
  206.                else
  207.                   IsCorrect := False;
  208.                i := 1;
  209.                while (not EoF(Input)) and IsCorrect do
  210.                begin
  211.                   Read(Input, First);
  212.                   Readln(Input, Second);
  213.                   if (First > 0) and (First < Count) and (Second > 0) and (Second < Count) then
  214.                   begin
  215.                      if First <> Second then
  216.                      begin
  217.                         sgComplexityMatrix.Cells[First, Second] := '+';
  218.                         sgComplexityMatrix.Cells[Second, First] := '+'
  219.                      end
  220.                      else
  221.                         IsCorrect := False;
  222.                   end
  223.                   else
  224.                      IsCorrect := False;
  225.                end;
  226.                if IsCorrect then
  227.                   CloseFile(Input)
  228.                else
  229.                begin
  230.                   CloseFile(Input);
  231. //                  btClearClick(Sender);
  232.                   MessageBox(Handle, PChar('Присутсвуют некорректные данные!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
  233.                end;
  234.             except
  235. //               btClearClick(Sender);
  236.                MessageBox(Handle, PChar('В файле присутсвуют данные несоответствующего типа!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
  237.                CloseFile(Input);
  238.                InputDataClick(Sender);
  239.             end;
  240.          end;
  241.       end;
  242.    end;
  243. end;
  244.  
  245. procedure TTransformMatrix.ItemAddNodeClick(Sender: TObject);
  246. var
  247.    ACol, Arow : Integer;
  248. begin
  249.    ARow := sgComplexityMatrix.Selection.Top;
  250.    ACol := sgComplexityMatrix.Selection.Left;
  251.    if (ACol > 0) and (ARow > 0) and (ARow <> ACol) then
  252.       if (sgComplexityMatrix.Cells[ACol, ARow] = '') then
  253.       begin
  254.          sgComplexityMatrix.Cells[ACol, ARow] := '+';
  255.          sgComplexityMatrix.Cells[ARow, ACol] := '+';
  256.       end
  257.       else
  258.          MessageBox(Handle, PChar('Между этими вершинами уже есть ребро'),
  259.             PChar('Внимание!'), MB_OK + MB_ICONSTOP);
  260. end;
  261.  
  262. procedure TTransformMatrix.ItemDeleteNodeClick(Sender: TObject);
  263. var
  264.    ACol, Arow : Integer;
  265. begin
  266.    ACol := sgComplexityMatrix.Selection.Left;
  267.    ARow := sgComplexityMatrix.Selection.Top;
  268.    if (ACol > 0) and (ARow > 0) then
  269.       if (sgComplexityMatrix.Cells[ACol, ARow] = '+') then
  270.       begin
  271.          sgComplexityMatrix.Cells[ACol, ARow] := '';
  272.          sgComplexityMatrix.Cells[ARow, ACol] := '';
  273.       end
  274.       else
  275.          MessageBox(Handle, PChar('Выбрана пустая клетка'), PChar('Внимание!'), MB_OK + MB_ICONSTOP);
  276. end;
  277.  
  278. procedure TTransformMatrix.ItemExitClick(Sender: TObject);
  279. begin
  280.    Close;
  281. end;
  282.  
  283. procedure TTransformMatrix.SaveDataClick(Sender: TObject);
  284. const
  285.    MsgRewrite = 'Желаете перезаписать Файл?';
  286. var
  287.    Output: TextFile;
  288.    Count, i, j: Integer;
  289. begin
  290.    if SaveFile.Execute then
  291.    begin
  292.       if MessageBox(Handle, MsgRewrite, 'Внимание!', MB_ICONINFORMATION + MB_YESNO) = mrYes then
  293.       begin
  294.          AssignFile(Output, SaveFIle.FileName);
  295.          Rewrite(Output);
  296.          Writeln(Output, 'Матрица смежности: '#13#10);
  297.          Count := sgComplexityMatrix.RowCount - 1;
  298.          for i := 1 to Count do
  299.          begin
  300.             for j := 1 to Count do
  301.             begin
  302.                if sgComplexityMatrix.Cells[j, i] = '+' then
  303.                   Write(Output, 1, '  ')
  304.                else
  305.                   Write(Output, 0, '  ');
  306.             end;
  307.             Writeln(Output);
  308.          end;
  309.          CloseFile(Output);
  310.       end;
  311.    end;
  312. end;
  313.  
  314. procedure TTransformMatrix.ClearStringGrid;
  315. var
  316.    i : Integer;
  317. begin
  318.    for i := 1 to sgComplexityMatrix.RowCount do
  319.    begin
  320.       sgComplexityMatrix.Rows[i].Clear;
  321.       sgIncidenceMatrix.Rows[i].Clear;
  322.    end;
  323.    sgIncidenceMatrix.ColCount := 1;
  324. end;
  325.  
  326.  
  327. procedure TTransformMatrix.AboutAuthorClick(Sender: TObject);
  328. begin
  329.    MessageBox(Handle, PChar('Программу разработал Быховец Илья (гр. 851001)'),
  330.             PChar(''), MB_ICONSTOP + MB_OK);
  331. end;
  332.  
  333. procedure TTransformMatrix.AboutProgrammClick(Sender: TObject);
  334. begin
  335.    MessageBox(Handle, PChar('Преобразовать матрицу смежности в списки инцидентности!'),
  336.             PChar('Описание!'), MB_ICONSTOP + MB_OK);
  337. end;
  338.  
  339. procedure TTransformMatrix.btAddAdgeClick(Sender: TObject);
  340. var
  341.    i, j : Integer;
  342. begin
  343.    if (cbFirstNode.Text <> '') and (cbSecondNode.Text <> '') then
  344.    begin
  345.       if cbFirstNode.Text <> cbSecondNode.Text then
  346.       begin
  347.          i := StrToInt(cbSecondNode.Text);
  348.          j := StrToInt(cbFirstNode.Text);
  349.          if sgComplexityMatrix.Cells[i, j] = '' then
  350.          begin
  351.             sgComplexityMatrix.Cells[i, j] := '+';
  352.             sgComplexityMatrix.Cells[j, i] := '+';
  353.          end
  354.          else
  355.             MessageBox(Handle, PChar('Такое ребро уже существует!'),
  356.             PChar('Внимание!'), MB_ICONSTOP + MB_OK);
  357.       end
  358.       else
  359.          MessageBox(Handle, PChar('Нельзя построить построить петлю!!'),
  360.             PChar('Внимание!'), MB_ICONSTOP + MB_OK);
  361.    end
  362.    else
  363.       MessageBox(Handle, PChar('Есть пустые поля!'),
  364.          PChar('Внимание!'), MB_ICONSTOP + MB_OK);
  365. end;
  366.  
  367. procedure TTransformMatrix.edtCountOfNodesKeyPress(Sender: TObject; var Key: Char);
  368. const
  369.    ENTER = #13;
  370. begin
  371.    if (Key = ENTER) and (edtCountOfNodes.Text <> '') then
  372.       btCreateMatrix.Click;
  373. end;
  374.  
  375. procedure TTransformMatrix.FormClose(Sender: TObject; var Action: TCloseAction);
  376. begin
  377.    if MessageBox(Handle, 'Вы уверены?', 'Внимание!', MB_ICONERROR + MB_YESNO) = mrNo then
  378.       Action := TCloseAction.caNone;
  379. end;
  380.  
  381. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement