Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit UnitTransformMatrix;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.Grids,
- Vcl.Menus, Vcl.ExtCtrls;
- type
- TMatrix = array of array of Integer;
- type
- TTransformMatrix = class(TForm)
- Label1: TLabel;
- edtCountOfNodes: TEdit;
- UpDown1: TUpDown;
- btCreateMatrix: TButton;
- sgComplexityMatrix: TStringGrid;
- Label2: TLabel;
- Label3: TLabel;
- cbFirstNode: TComboBox;
- cbSecondNode: TComboBox;
- Label4: TLabel;
- btAddAdge: TButton;
- PopupMenu1: TPopupMenu;
- pmForSG: TPopupMenu;
- ItemAddNode: TMenuItem;
- ItemDeleteNode: TMenuItem;
- InputFile: TOpenDialog;
- SaveFile: TSaveDialog;
- MainMenu: TMainMenu;
- N1: TMenuItem;
- InputData: TMenuItem;
- SaveData: TMenuItem;
- N2: TMenuItem;
- ItemExit: TMenuItem;
- N3: TMenuItem;
- AboutAuthor: TMenuItem;
- AboutProgramm: TMenuItem;
- btClear: TButton;
- btTransform: TButton;
- Label5: TLabel;
- Label6: TLabel;
- sgIncidenceMatrix: TStringGrid;
- procedure btCreateMatrixClick(Sender: TObject);
- procedure edtCountOfNodesKeyPress(Sender: TObject; var Key: Char);
- procedure btAddAdgeClick(Sender: TObject);
- procedure ClearStringGrid;
- procedure ItemDeleteNodeClick(Sender: TObject);
- procedure ItemAddNodeClick(Sender: TObject);
- procedure AboutAuthorClick(Sender: TObject);
- procedure AboutProgrammClick(Sender: TObject);
- procedure InputDataClick(Sender: TObject);
- procedure SaveDataClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure ItemExitClick(Sender: TObject);
- procedure btTransformClick(Sender: TObject);
- procedure btClearClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- const
- CellWidth = 29;
- CellHeight = 29;
- var
- TransformMatrix: TTransformMatrix;
- MyNodes: array of TPoint;
- implementation
- {$R *.dfm}
- procedure TTransformMatrix.btClearClick(Sender: TObject);
- begin
- SaveData.Enabled := False;
- ClearStringGrid;
- sgComplexityMatrix.Width := CellWidth;
- sgIncidenceMatrix.Width := CellWidth;
- sgComplexityMatrix.Height := CellHeight;
- sgIncidenceMatrix.Height := CellHeight;
- sgComplexityMatrix.RowCount := 1;
- sgComplexityMatrix.ColCount := 1;
- sgIncidenceMatrix.RowCount := 1;
- sgComplexityMatrix.ColCount := 1;
- cbFirstNode.Clear;
- cbSecondNode.Clear;
- end;
- procedure TTransformMatrix.btCreateMatrixClick(Sender: TObject);
- var
- Count, i, j : Integer;
- begin
- SaveData.Enabled := False;
- ClearStringGrid;
- if (edtCountOfNodes.Text <> '0') and (edtCountOfNodes.Text <> '') then
- begin
- cbFirstNode.Clear;
- cbSecondNode.Clear;
- Count := StrToInt(edtCountOfNodes.Text) + 1;
- sgComplexityMatrix.Width := Count * CellWidth;
- sgComplexityMatrix.Height := Count * CellHeight;
- sgIncidenceMatrix.Width := CellWidth;
- sgIncidenceMatrix.Height := Count * CellHeight;
- sgComplexityMatrix.RowCount := Count;
- sgComplexityMatrix.ColCount := Count;
- sgIncidenceMatrix.RowCount := Count;
- for i := 0 to Count - 1 do
- begin
- sgComplexityMatrix.Cells[i, 0] := IntToStr(i);
- sgComplexityMatrix.Cells[0, i] := IntToStr(i);
- sgIncidenceMatrix.Cells[0, i] := IntToStr(i);
- end;
- sgComplexityMatrix.Cells[0, 0] := '';
- sgIncidenceMatrix.Cells[0, 0] := '';
- for i := 1 to Count - 1 do
- begin
- cbFirstNode.Items.Add(IntToStr(i));
- cbSecondNode.Items.Add(IntToStr(i));
- end;
- end
- else
- MessageBox(Handle, PChar('Допустипое количество вершин от 1 до 9'), PChar(''), MB_ICONSTOP + MB_OK);
- end;
- procedure TTransformMatrix.btTransformClick(Sender: TObject);
- const
- MsgNotAdges = 'В заданном графе нет ни одного ребра!';
- MaxWidth = 350;
- var
- Count, i, j, CountOfAdge, NumberOfAdge: Integer;
- begin
- Count := sgComplexityMatrix.RowCount - 1;
- CountOfAdge := 0;
- for i := 1 to Count do
- for j := i + 1 to Count do
- if sgComplexityMatrix.Cells[j, i] = '+' then
- Inc(CountOfAdge);
- if CountOfAdge <> 0 then
- begin
- for i := 1 to sgIncidenceMatrix.ColCount - 1 do
- sgIncidenceMatrix.Cols[i].Clear;
- sgIncidenceMatrix.ColCount := CountOfAdge + 1;
- sgIncidenceMatrix.Width := (CountOfAdge + 1) * CellWidth;
- if sgIncidenceMatrix.Width > MaxWidth then
- begin
- sgIncidenceMatrix.Width := MaxWidth;
- sgIncidenceMatrix.ScrollBars := ssHorizontal;
- end
- else
- sgIncidenceMatrix.ScrollBars := ssNone;
- for i := 1 to CountOfAdge do
- sgIncidenceMatrix.Cells[i, 0] := 'e' + IntToStr(i);
- NumberOfAdge := 0;
- for i := 1 to Count do
- for j := i + 1 to Count do
- if sgComplexityMatrix.Cells[j, i] = '+' then
- begin
- Inc(NumberOfAdge);
- sgIncidenceMatrix.Cells[NumberOfAdge, i] := '1';
- sgIncidenceMatrix.Cells[NumberOfAdge, j] := '1';
- end;
- end
- else
- MessageBox(Handle, MsgNotAdges, 'Внимание!', MB_ICONINFORMATION + MB_OK);
- end;
- procedure TTransformMatrix.InputDataClick(Sender: TObject);
- var
- Input : TextFile;
- Count, i, j, First, Second: Integer;
- IsCorrect : Boolean;
- begin
- // btClearClick(Sender);
- if InputFile.Execute then
- begin
- AssignFile(Input, InputFile.FileName);
- Reset(Input);
- if EoF(Input) then
- begin
- MessageBox(Handle, PChar('Файл не содержит необходимые данные!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
- CloseFile(Input);
- InputDataClick(Sender);
- end
- else
- begin
- Readln(Input);
- if EoF(Input) then
- MessageBox(Handle, PChar('В файле отсутвуют элементы массива!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK)
- else
- begin
- Reset(Input);
- try
- Readln(Input, Count);
- IsCorrect := True;
- if (Count > 0) and (Count < 10) then
- begin
- edtCountOfNodes.Text := IntToStr(Count);
- btCreateMatrixClick(Sender);
- end
- else
- IsCorrect := False;
- i := 1;
- while (not EoF(Input)) and IsCorrect do
- begin
- Read(Input, First);
- Readln(Input, Second);
- if (First > 0) and (First < Count) and (Second > 0) and (Second < Count) then
- begin
- if First <> Second then
- begin
- sgComplexityMatrix.Cells[First, Second] := '+';
- sgComplexityMatrix.Cells[Second, First] := '+'
- end
- else
- IsCorrect := False;
- end
- else
- IsCorrect := False;
- end;
- if IsCorrect then
- CloseFile(Input)
- else
- begin
- CloseFile(Input);
- // btClearClick(Sender);
- MessageBox(Handle, PChar('Присутсвуют некорректные данные!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
- end;
- except
- // btClearClick(Sender);
- MessageBox(Handle, PChar('В файле присутсвуют данные несоответствующего типа!'), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
- CloseFile(Input);
- InputDataClick(Sender);
- end;
- end;
- end;
- end;
- end;
- procedure TTransformMatrix.ItemAddNodeClick(Sender: TObject);
- var
- ACol, Arow : Integer;
- begin
- ARow := sgComplexityMatrix.Selection.Top;
- ACol := sgComplexityMatrix.Selection.Left;
- if (ACol > 0) and (ARow > 0) and (ARow <> ACol) then
- if (sgComplexityMatrix.Cells[ACol, ARow] = '') then
- begin
- sgComplexityMatrix.Cells[ACol, ARow] := '+';
- sgComplexityMatrix.Cells[ARow, ACol] := '+';
- end
- else
- MessageBox(Handle, PChar('Между этими вершинами уже есть ребро'),
- PChar('Внимание!'), MB_OK + MB_ICONSTOP);
- end;
- procedure TTransformMatrix.ItemDeleteNodeClick(Sender: TObject);
- var
- ACol, Arow : Integer;
- begin
- ACol := sgComplexityMatrix.Selection.Left;
- ARow := sgComplexityMatrix.Selection.Top;
- if (ACol > 0) and (ARow > 0) then
- if (sgComplexityMatrix.Cells[ACol, ARow] = '+') then
- begin
- sgComplexityMatrix.Cells[ACol, ARow] := '';
- sgComplexityMatrix.Cells[ARow, ACol] := '';
- end
- else
- MessageBox(Handle, PChar('Выбрана пустая клетка'), PChar('Внимание!'), MB_OK + MB_ICONSTOP);
- end;
- procedure TTransformMatrix.ItemExitClick(Sender: TObject);
- begin
- Close;
- end;
- procedure TTransformMatrix.SaveDataClick(Sender: TObject);
- const
- MsgRewrite = 'Желаете перезаписать Файл?';
- var
- Output: TextFile;
- Count, i, j: Integer;
- begin
- if SaveFile.Execute then
- begin
- if MessageBox(Handle, MsgRewrite, 'Внимание!', MB_ICONINFORMATION + MB_YESNO) = mrYes then
- begin
- AssignFile(Output, SaveFIle.FileName);
- Rewrite(Output);
- Writeln(Output, 'Матрица смежности: '#13#10);
- Count := sgComplexityMatrix.RowCount - 1;
- for i := 1 to Count do
- begin
- for j := 1 to Count do
- begin
- if sgComplexityMatrix.Cells[j, i] = '+' then
- Write(Output, 1, ' ')
- else
- Write(Output, 0, ' ');
- end;
- Writeln(Output);
- end;
- CloseFile(Output);
- end;
- end;
- end;
- procedure TTransformMatrix.ClearStringGrid;
- var
- i : Integer;
- begin
- for i := 1 to sgComplexityMatrix.RowCount do
- begin
- sgComplexityMatrix.Rows[i].Clear;
- sgIncidenceMatrix.Rows[i].Clear;
- end;
- sgIncidenceMatrix.ColCount := 1;
- end;
- procedure TTransformMatrix.AboutAuthorClick(Sender: TObject);
- begin
- MessageBox(Handle, PChar('Программу разработал Быховец Илья (гр. 851001)'),
- PChar(''), MB_ICONSTOP + MB_OK);
- end;
- procedure TTransformMatrix.AboutProgrammClick(Sender: TObject);
- begin
- MessageBox(Handle, PChar('Преобразовать матрицу смежности в списки инцидентности!'),
- PChar('Описание!'), MB_ICONSTOP + MB_OK);
- end;
- procedure TTransformMatrix.btAddAdgeClick(Sender: TObject);
- var
- i, j : Integer;
- begin
- if (cbFirstNode.Text <> '') and (cbSecondNode.Text <> '') then
- begin
- if cbFirstNode.Text <> cbSecondNode.Text then
- begin
- i := StrToInt(cbSecondNode.Text);
- j := StrToInt(cbFirstNode.Text);
- if sgComplexityMatrix.Cells[i, j] = '' then
- begin
- sgComplexityMatrix.Cells[i, j] := '+';
- sgComplexityMatrix.Cells[j, i] := '+';
- end
- else
- MessageBox(Handle, PChar('Такое ребро уже существует!'),
- PChar('Внимание!'), MB_ICONSTOP + MB_OK);
- end
- else
- MessageBox(Handle, PChar('Нельзя построить построить петлю!!'),
- PChar('Внимание!'), MB_ICONSTOP + MB_OK);
- end
- else
- MessageBox(Handle, PChar('Есть пустые поля!'),
- PChar('Внимание!'), MB_ICONSTOP + MB_OK);
- end;
- procedure TTransformMatrix.edtCountOfNodesKeyPress(Sender: TObject; var Key: Char);
- const
- ENTER = #13;
- begin
- if (Key = ENTER) and (edtCountOfNodes.Text <> '') then
- btCreateMatrix.Click;
- end;
- procedure TTransformMatrix.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- if MessageBox(Handle, 'Вы уверены?', 'Внимание!', MB_ICONERROR + MB_YESNO) = mrNo then
- Action := TCloseAction.caNone;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement