Advertisement
Vanya_Shestakov

Untitled

Mar 21st, 2021 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.79 KB | None | 0 0
  1. unit MainUnit;
  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.Grids, Vcl.StdCtrls,
  8.   Vcl.Samples.Spin, Vcl.Menus, Vcl.Buttons, System.Generics.Collections, System.RegularExpressions;
  9.  
  10. type
  11.     TMatrix = array of array of Byte;
  12.   TMainForm = class(TForm)
  13.     MatrixGrid: TStringGrid;
  14.     OrderSpinEdit: TSpinEdit;
  15.     Label1: TLabel;
  16.     MainMenu1: TMainMenu;
  17.     About1: TMenuItem;
  18.     File1: TMenuItem;
  19.     Open1: TMenuItem;
  20.     Save1: TMenuItem;
  21.     Label2: TLabel;
  22.     ListMemo: TMemo;
  23.     ConvertBtn: TBitBtn;
  24.     Label3: TLabel;
  25.     OpenDialog: TOpenDialog;
  26.     SaveDialog: TSaveDialog;
  27.     procedure SetSize(Size: Byte);
  28.     procedure OrderSpinEditChange(Sender: TObject);
  29.     procedure GetMatrixFromGrid(var Matrix: TMatrix);
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure ConvertBtnClick(Sender: TObject);
  32.     procedure ConvertToInciedenceList(Matrix: TMatrix);
  33.     procedure MatrixGridSetEditText(Sender: TObject; ACol, ARow: Integer;
  34.       const Value: string);
  35.     function CheckFile(): Boolean;
  36.     procedure Open1Click(Sender: TObject);
  37.     procedure Save1Click(Sender: TObject);
  38.     procedure About1Click(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.   end;
  44.  
  45. var
  46.   MainForm: TMainForm;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. procedure TMainForm.ConvertBtnClick(Sender: TObject);
  53. var
  54.     Matrix: TMatrix;
  55.     Order: Byte;
  56.     IsCorrect: Boolean;
  57. begin
  58.     IsCorrect := True;
  59.     Order := StrToInt(OrderSpinEdit.Text);
  60.     SetLength(Matrix, Order, Order);
  61.     try
  62.         GetMatrixFromGrid(Matrix);
  63.     except
  64.         MessageDlg('Enter all values into the matrix!', mtError, [mbOK], 0);
  65.         IsCorrect := False;
  66.     end;
  67.     if isCorrect then
  68.     begin
  69.         ListMemo.Clear;
  70.         ConvertToInciedenceList(Matrix);
  71.     end;
  72.  
  73. end;
  74.  
  75. procedure TMainForm.ConvertToInciedenceList(Matrix: TMatrix);
  76. var
  77.     I, J: Byte;
  78. begin
  79.     for I := 0 to High(Matrix) do
  80.     begin
  81.         ListMemo.Lines.Add(IntTostr(I) + ': ');
  82.         for J := 0 to High(Matrix) do
  83.         begin
  84.             if Matrix[I][J] = 1 then
  85.             begin
  86.                 ListMemo.Lines[I] := ListMemo.Lines[I] + IntToStr(J) + ' ';
  87.             end;
  88.         end;
  89.     end;
  90.  
  91. end;
  92.  
  93. procedure TMainForm.GetMatrixFromGrid(var Matrix: TMatrix);
  94. var
  95.     I, J: Byte;
  96. begin
  97.     for I := 0 to High(Matrix) do
  98.         for J := 0 to High(Matrix) do
  99.             Matrix[I][J] := StrToInt(MatrixGrid.Cells[J + 1 , I + 1]);
  100. end;
  101.  
  102. procedure TMainForm.FormCreate(Sender: TObject);
  103. begin
  104.    OrderSpinEdit.Text := IntToStr(5);
  105. end;
  106.  
  107. procedure TMainForm.MatrixGridSetEditText(Sender: TObject; ACol, ARow: Integer;
  108.   const Value: string);
  109. begin
  110.     if not TRegEx.IsMatch(Value, '^([01]{1})$') then
  111.         MatrixGrid.Cells[ACol, ARow] := TRegEx.Match(Value, '^([01]{1})').Value;
  112. end;
  113.  
  114. procedure TMainForm.Open1Click(Sender: TObject);
  115. var
  116.     I, J: Integer;
  117.     InputFile: TextFile;
  118.     IsCorrect: Boolean;
  119.     Buff: Integer;
  120. begin
  121.     IsCorrect := CheckFile();
  122.     if IsCorrect then
  123.     begin
  124.         AssignFile(InputFile,OpenDialog.FileName);
  125.         Reset(InputFile);
  126.         Readln(InputFile);
  127.         for I := 1 to MatrixGrid.ColCount - 1  do
  128.         begin
  129.             for J := 1 to MatrixGrid.ColCount - 1  do
  130.             begin
  131.                 Read(InputFile, Buff);
  132.                 MatrixGrid.Cells[J, I] := IntToStr(Buff);
  133.             end;
  134.             Readln(InputFile);
  135.         end;
  136.         CloseFile(InputFile);
  137.     end;
  138. end;
  139.  
  140. procedure TMainForm.OrderSpinEditChange(Sender: TObject);
  141. begin
  142.     SetSize(StrToInt(OrderSpinEdit.Text) + 1);
  143.     ListMemo.Clear;
  144. end;
  145.  
  146. procedure TMainForm.Save1Click(Sender: TObject);
  147. var
  148.     OutputFile: TextFile;
  149. begin
  150.     if SaveDialog.Execute then
  151.     begin
  152.         AssignFile(OutputFile, SaveDialog.FileName);
  153.         Rewrite(OutputFile);
  154.         Write(OutputFile, ListMemo.Text);
  155.         CloseFile(OutputFile);
  156.     end
  157. end;
  158.  
  159. procedure TMainForm.SetSize(Size: Byte);
  160. var
  161.     I: Byte;
  162. begin
  163.     MatrixGrid.ColCount := Size;
  164.     MatrixGrid.RowCount := Size;
  165.     for I := 0 to Size - 1 do
  166.     begin
  167.         MatrixGrid.Rows[I].Clear;
  168.         MatrixGrid.Cols[I].Clear;
  169.     end;
  170.     for I := 0 to Size - 1 do
  171.     begin
  172.         MatrixGrid.Cells[0, I + 1] := IntToStr(I);
  173.     end;
  174.     for I := 0 to Size - 1 do
  175.     begin
  176.         MatrixGrid.Cells[I + 1, 0] := IntToStr(I);
  177.     end;
  178. end;
  179.  
  180. procedure TMainForm.About1Click(Sender: TObject);
  181. const
  182.     TASK = 'The program translates the adjacency matrix to the incidence list' + #13#10 +
  183.     'Only 1 or 0 matrix values are allowed. Maximum matrix size: 5';
  184. begin
  185.     MessageDlg(TASK, mtInformation, [mbOK], 0);
  186. end;
  187.  
  188. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement