Advertisement
SmnVadik

Lab 7.2 (Delphi)

Sep 13th, 2023 (edited)
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.78 KB | None | 0 0
  1. unit MainUnit;
  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.Menus, Vcl.StdCtrls, Vcl.Grids,
  9.     System.StrUtils;
  10.  
  11. type
  12.     TPVertex = ^TVertex;
  13.  
  14.     TVertex = Record
  15.         VertexNum: Integer;
  16.         Next: TPVertex;
  17.     End;
  18.  
  19.     TMatrix = Array Of Array Of Integer;
  20.  
  21.     TMainForm = class(TForm)
  22.         InfoLabel: TLabel;
  23.         EnterVertexAmountLabel: TLabel;
  24.         EnterVertexAmountEdit: TEdit;
  25.         MainMenu1: TMainMenu;
  26.         N1: TMenuItem;
  27.         OpenFile: TMenuItem;
  28.         SaveMenu: TMenuItem;
  29.         Instruction: TMenuItem;
  30.         DevInfo: TMenuItem;
  31.         ConvertButton: TButton;
  32.     InputList: TStringGrid;
  33.         OutputMemo: TMemo;
  34.         OpenDialog1: TOpenDialog;
  35.         SaveDialog1: TSaveDialog;
  36.         procedure EnterVertexAmountEditChange(Sender: TObject);
  37.         procedure EnterVertexAmountEditKeyPress(Sender: TObject; var Key: Char);
  38.         procedure EnterVertexAmountEditKeyDown(Sender: TObject; var Key: Word;
  39.           Shift: TShiftState);
  40.         procedure ConvertButtonClick(Sender: TObject);
  41.         procedure FormCreate(Sender: TObject);
  42.         procedure DevInfoClick(Sender: TObject);
  43.         procedure InstructionClick(Sender: TObject);
  44.         procedure OpenFileClick(Sender: TObject);
  45.         procedure SaveMenuClick(Sender: TObject);
  46.     private
  47.         { Private declarations }
  48.     public
  49.         { Public declarations }
  50.     end;
  51.  
  52. var
  53.     MainForm: TMainForm;
  54.  
  55. implementation
  56.  
  57. {$R *.dfm}
  58.  
  59. Procedure PrintMatrix(AdjacencyMatrix: TMatrix);
  60. Var
  61.     I, J: Integer;
  62. Begin
  63.     For I := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
  64.     Begin
  65.         For J := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
  66.             MainForm.OutputMemo.Lines.Text := MainForm.OutputMemo.Lines.Text +
  67.               IntToStr(AdjacencyMatrix[I, J]) + ' ';
  68.         MainForm.OutputMemo.Lines.Text :=
  69.           MainForm.OutputMemo.Lines.Text + #10#13;
  70.     End;
  71.     MainForm.SaveMenu.Enabled := True;
  72. End;
  73.  
  74. Procedure SetMatrix(VertexArray: Array Of TPVertex);
  75. Var
  76.     AdjacencyMatrix: TMatrix;
  77.     I, J: Integer;
  78.     Curr: TPVertex;
  79. Begin
  80.     SetLength(AdjacencyMatrix, StrToInt(MainForm.EnterVertexAmountEdit.Text),
  81.       StrToInt(MainForm.EnterVertexAmountEdit.Text));
  82.     For I := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
  83.         For J := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
  84.             AdjacencyMatrix[I, J] := 0;
  85.  
  86.     For I := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
  87.     Begin
  88.         Curr := VertexArray[I].Next;
  89.         While Curr <> Nil Do
  90.         Begin
  91.             J := Curr.VertexNum - 1;
  92.             AdjacencyMatrix[I, J] := 1;
  93.             AdjacencyMatrix[J, I] := 1;
  94.             Curr := Curr.Next;
  95.         End;
  96.     End;
  97.     PrintMatrix(AdjacencyMatrix);
  98. End;
  99.  
  100. Function CheckInput: Boolean;
  101. Var
  102.     I, J: Integer;
  103. Begin
  104.     For I := 0 To MainForm.InputList.RowCount - 1 Do
  105.         For J := 1 To MainForm.InputList.ColCount - 1 Do
  106.             If (MainForm.InputList.Cells[J, I] <> '') And
  107.               (StrToInt(MainForm.InputList.Cells[J, I]) >
  108.               MainForm.InputList.RowCount) And
  109.               (StrToInt(MainForm.InputList.Cells[J, I]) = 0) Then
  110.             Begin
  111.                 CheckInput := False;
  112.                 Exit;
  113.             End;
  114.     For I := 0 To MainForm.InputList.RowCount - 1 Do
  115.     Begin
  116.         If MainForm.InputList.Cells[1, I] = '' Then
  117.         Begin
  118.             CheckInput := False;
  119.             Exit;
  120.         End
  121.         Else If MainForm.InputList.ColCount > 4 Then
  122.             For J := 2 To MainForm.InputList.ColCount - 2 Do
  123.             Begin
  124.                 If (MainForm.InputList.Cells[J - 1, I] = '') And
  125.                   (MainForm.InputList.Cells[J + 1, I] = '') And
  126.                   (MainForm.InputList.Cells[J, I] <> '') Then
  127.                 Begin
  128.                     CheckInput := False;
  129.                     Exit;
  130.                 End;
  131.  
  132.             End;
  133.     End;
  134.     CheckInput := True;
  135. End;
  136.  
  137. procedure TMainForm.ConvertButtonClick(Sender: TObject);
  138. Var
  139.     VertexArray: Array Of TPVertex;
  140.     NewNode, Curr: TPVertex;
  141.     I, J: Integer;
  142. Begin
  143.     OutputMemo.Clear;
  144.     If CheckInput Then
  145.     Begin
  146.         OutputMemo.Visible := True;
  147.         SetLength(VertexArray, StrToInt(EnterVertexAmountEdit.Text));
  148.         For I := 0 To InputList.RowCount - 1 Do
  149.         Begin
  150.             J := 1;
  151.             New(VertexArray[I]);
  152.             VertexArray[I].VertexNum := StrToInt(InputList.Cells[0, I]);
  153.             VertexArray[I].Next := Nil;
  154.             While InputList.Cells[J, I] <> '' Do
  155.             Begin
  156.                 New(NewNode);
  157.                 NewNode.VertexNum := StrToInt(InputList.Cells[J, I]);
  158.                 NewNode.Next := Nil;
  159.                 If VertexArray[I].Next = Nil Then
  160.                     VertexArray[I].Next := NewNode
  161.                 Else
  162.                 Begin
  163.                     Curr := VertexArray[I];
  164.                     While Curr.Next <> Nil Do
  165.                         Curr := Curr.Next;
  166.                     Curr.Next := NewNode;
  167.                 End;
  168.                 Inc(J);
  169.             End;
  170.  
  171.         End;
  172.         SetMatrix(VertexArray);
  173.     End
  174.     Else
  175.     Begin
  176.         Application.MessageBox('Проверьте введенные данные!', 'Ошибка',
  177.           MB_OK + MB_ICONERROR);
  178.     End;
  179. End;
  180.  
  181. procedure TMainForm.DevInfoClick(Sender: TObject);
  182. begin
  183.     Application.MessageBox('Сымоник Вадим, группа 251004', 'Разработчик');
  184. end;
  185.  
  186. procedure TMainForm.EnterVertexAmountEditChange(Sender: TObject);
  187. Var
  188.     I, J: Integer;
  189. Begin
  190.     OutputMemo.Visible := False;
  191.     If ((Length(EnterVertexAmountEdit.Text) <> 0) and
  192.       (Length(EnterVertexAmountEdit.Text) < 2)) Then
  193.     Begin
  194.         SaveMenu.Enabled := False;
  195.         For I := 0 To InputList.RowCount - 1 Do
  196.             For J := 0 To InputList.ColCount - 1 Do
  197.                 InputList.Cells[J, I] := '';
  198.         OutputMemo.Clear;
  199.         InputList.ColCount := StrToInt(EnterVertexAmountEdit.Text) + 1;
  200.         InputList.RowCount := StrToInt(EnterVertexAmountEdit.Text);
  201.         InputList.FixedCols := 1;
  202.         For I := 0 To InputList.RowCount - 1 Do
  203.             InputList.Cells[0, I] := IntToStr(I + 1);
  204.         InputList.Visible := True;
  205.         ConvertButton.Visible := True;
  206.     End
  207.     Else
  208.     Begin
  209.         InputList.Visible := False;
  210.         ConvertButton.Visible := False;
  211.     End;
  212. End;
  213.  
  214. procedure TMainForm.EnterVertexAmountEditKeyDown(Sender: TObject; var Key: Word;
  215.   Shift: TShiftState);
  216. begin
  217.     If (SsShift In Shift) And (Key = VK_Insert) Then
  218.         Abort;
  219. end;
  220.  
  221. procedure TMainForm.EnterVertexAmountEditKeyPress(Sender: TObject;
  222.   var Key: Char);
  223. begin
  224.     If Key <> #08 Then
  225.     Begin
  226.         If Not(Key In ['1' .. '9', #9]) Then
  227.         Begin
  228.             MessageBox(Application.Handle,
  229.               'К вводу доступны только цифры от 1 до 9.', 'Внимание!', MB_OK);
  230.             Key := #0;
  231.         End
  232.         Else
  233.         Begin
  234.             If Length(EnterVertexAmountEdit.Text) = 1 Then
  235.             Begin
  236.                 MessageBox(Application.Handle,
  237.                   'Нельзя ввести больше 1-го символа.', 'Внимание!', MB_OK);
  238.                 Key := #0;
  239.             End;
  240.  
  241.         End;
  242.     End;
  243. end;
  244.  
  245. procedure TMainForm.FormCreate(Sender: TObject);
  246. begin
  247.     InputList.Visible := False;
  248.     OutputMemo.Visible := False;
  249.     SaveMenu.Enabled := False;
  250. end;
  251.  
  252. procedure TMainForm.InstructionClick(Sender: TObject);
  253. const
  254.     Info1 = 'Ввод с клавиатуры:'#13#10;
  255.     Info2 = 'Можно ввести количество вершин от 1 до 9'#13#10;
  256.     Info3 = 'первая клетка должна быть заполнена обязательно, между клетками с цифрами не должно быть пустых клеток'#13#10;
  257.     Info4 = 'значение в клетке должно быть не больше количества вершин'#13#10;
  258.     Info5 = 'Ввод с файла: Первое число - количество вершин'#13#10;
  259.     Info6 = 'Далее в каждой строке идут вершины, которые будут связаны с вершиной, соответствующей номеру строки'#13#10;
  260.     Info7 = 'т.е первая строка вершин после количества вершин соответсвует первой вершине графа и т.д, при том номер самой строки указывать не надо'#13#10;
  261. begin
  262.     Application.MessageBox(Info1 + Info2 + Info3 + Info4 + Info5 + Info6 +
  263.       Info7, 'Справка', MB_OK + MB_ICONINFORMATION);
  264. end;
  265.  
  266. procedure TMainForm.OpenFileClick(Sender: TObject);
  267. Var
  268.     InputFile: TextFile;
  269.     I, J, VertexAmount, Temp: Integer;
  270.     TempArr: TArray<String>;
  271.     Input: String;
  272.     FileWasOpened: Boolean;
  273. Begin
  274.     If OpenDialog1.Execute Then
  275.     Begin
  276.         AssignFile(InputFile, OpenDialog1.FileName);
  277.         Try
  278.             FileWasOpened := True;
  279.             Try
  280.                 Reset(InputFile);
  281.             Except
  282.                 On E: Exception Do
  283.                 Begin
  284.                     FileWasOpened := False;
  285.                     Application.MessageBox
  286.                       (PChar('Ошибка работы с файлом: ' + E.Message), 'Ошибка',
  287.                       MB_OK + MB_ICONERROR);
  288.                 End;
  289.             End;
  290.             If FileWasOpened Then
  291.             Begin
  292.                 Try
  293.                     Readln(InputFile, VertexAmount);
  294.                     EnterVertexAmountEdit.Text := IntToStr(VertexAmount);
  295.                 Except
  296.                     On E: Exception Do
  297.                     Begin
  298.                         Application.MessageBox
  299.                           (PChar('Ошибка работы с файлом: ' + E.Message),
  300.                           'Ошибка', MB_OK + MB_ICONERROR);
  301.                     End;
  302.                 End;
  303.                 For I := 0 To (VertexAmount - 1) Do
  304.                 Begin
  305.                     Readln(InputFile, Input);
  306.                     TempArr := SplitString(Input, ' ');
  307.                     For J := Low(TempArr) To High(TempArr) Do
  308.                         If TryStrToInt(TempArr[J], Temp) Then
  309.                             InputList.Cells[J + 1, I] := TempArr[J];
  310.                 End;
  311.                 Application.MessageBox('Чтение успешно произведено.',
  312.                   'Чтение из файла', MB_OK + MB_ICONINFORMATION);
  313.  
  314.             End;
  315.         Finally
  316.             If FileWasOpened Then
  317.                 CloseFile(InputFile);
  318.         End;
  319.     End
  320.     Else
  321.         Application.MessageBox('Чтение из файла отменено!', 'Чтение из файла',
  322.           MB_OK + MB_ICONINFORMATION);
  323. End;
  324.  
  325. procedure TMainForm.SaveMenuClick(Sender: TObject);
  326. Begin
  327.     If SaveDialog1.Execute Then
  328.         Try
  329.             OutputMemo.Lines.SaveToFile(SaveDialog1.FileName);
  330.             Application.MessageBox('Результат успешно сохранен в файл!',
  331.               'Сохранение в файл', MB_OK + MB_ICONINFORMATION);
  332.         Except
  333.             On E: Exception Do
  334.             Begin
  335.                 Application.MessageBox
  336.                   (PChar('Ошибка работы с файлом: ' + E.Message), 'Ошибка',
  337.                   MB_OK + MB_ICONERROR);
  338.             End;
  339.         End
  340.     Else
  341.         Application.MessageBox('Сохранение в файл отменено!', 'Внимание',
  342.           MB_OK + MB_ICONINFORMATION);
  343. End;
  344.  
  345. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement