Advertisement
Egor_Vakar

(Delphi) lab7.2

May 28th, 2022
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.94 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.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  8.     Vcl.Samples.Spin, Vcl.Menus, Vcl.Grids, System.Generics.Collections,System.RegularExpressions,
  9.     Vcl.Imaging.jpeg, Vcl.ExtDlgs, Vcl.Imaging.pngimage;
  10.  
  11. type
  12.     TMatrix = Array of Array of Integer;
  13.  
  14.   TFormMain = class(TForm)
  15.     MainMenu1: TMainMenu;
  16.     N1: TMenuItem;
  17.     N2: TMenuItem;
  18.     InfoLabel: TLabel;
  19.     InfoLabel1: TLabel;
  20.     SpinEdit: TSpinEdit;
  21.     StringGrid: TStringGrid;
  22.     StringGrid1: TStringGrid;
  23.     Button1: TButton;
  24.     A1: TMenuItem;
  25.     N3: TMenuItem;
  26.     N4: TMenuItem;
  27.     OpenFromFile: TOpenDialog;
  28.     SaveDialog: TSaveDialog;
  29.     procedure AboutClick(Sender: TObject);
  30.     procedure DeveloperInfoClick(Sender: TObject);
  31.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  32.     procedure StringGridSelectCell(Sender: TObject; ACol, ARow: Integer;
  33.       var CanSelect: Boolean);
  34.     procedure SpinEditChange(Sender: TObject);
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure StringGridKeyPress(Sender: TObject; var Key: Char);
  37.     procedure ClearStringGrid;
  38.     procedure Button1Click(Sender: TObject);
  39.     procedure FindList;
  40.     procedure FillStringGrid;
  41.     procedure ClearGrid;
  42.     procedure ClearList;
  43.     procedure OpenFromFileMenuClick(Sender: TObject);
  44.     procedure SaveToFileMenuClick(Sender: TObject);
  45.     function IsInFileCorrect(const Path: String): Boolean;
  46.     function IsMatrixCorrect(Matrix: TMatrix): Boolean;
  47.   private
  48.     { Private declarations }
  49.   public
  50.     { Public declarations }
  51.   end;
  52.  
  53. var
  54.   FormMain: TFormMain;
  55.  
  56. implementation
  57.  
  58. {$R *.dfm}
  59.  
  60. type
  61.     TNode =^ Node;
  62.     Node = Record
  63.       pNumber: Integer;
  64.       pNext: TNode;
  65. end;
  66.  
  67. var
  68.     Arr: Array of TNode;
  69.  
  70. procedure TFormMain.AboutClick(Sender: TObject);
  71. begin
  72.     application.MessageBox('Данная программа преобразовывает матрицу смежности в списки инцидентности', 'О программе');
  73. end;
  74.  
  75. procedure TFormMain.DeveloperInfoClick(Sender: TObject);
  76. begin
  77.     Application.MessageBox('Данная программа написана Вакарём Егором'#13#10'студентом группы 151002.','О разработчике');
  78. end;
  79.  
  80. procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  81. var
  82.     WND: HWND;
  83.     lpCaption, lpText: PChar;
  84.     Tip: Integer;
  85. begin
  86.     WND := FormMain.Handle;
  87.     lpCaption := 'Выход';
  88.     lpText := 'Вы уверены, что хотите выйти?';
  89.     Tip := MB_YESNO + MB_ICONINFORMATION + MB_DEFBUTTON2;
  90.     case MessageBox(WND, lpText, lpCaption, Tip) Of
  91.         IDYES : CanClose := True;
  92.         IDNO : CanClose := False;
  93.     end
  94. end;
  95.  
  96. procedure TFormMain.ClearGrid;
  97. var
  98.     i,j: Integer;
  99. begin
  100.     for i := 1 to StringGrid1.ColCount do
  101.         for j := 1 to StringGrid1.RowCount do
  102.             StringGrid1.Cells[i, j] := '';
  103. end;
  104.  
  105. procedure TFormMain.FormCreate(Sender: TObject);
  106. var
  107.     i: Integer;
  108. begin
  109.     for i := 1 to 2 do
  110.     begin
  111.         StringGrid1.Cells[0, i] := IntToStr(i);
  112.         StringGrid.Cells[i, 0] := IntToStr(i);
  113.         StringGrid.Cells[0, i] := IntToStr(i);
  114.     end;
  115.     ClearStringGrid;
  116. end;
  117.  
  118. function TFormMain.IsInFileCorrect(const Path: String): Boolean;
  119. const
  120.     MIN_SIZE = 1;
  121.     MAX_SIZE = 13;
  122. var
  123.     InFile: TextFile;
  124.     Size, Temp, i, j: Integer;
  125.     IsCorrect: Boolean;
  126. begin
  127.     IsCorrect := True;
  128.     Try
  129.         AssignFile(InFile, Path);
  130.         Reset(InFile);
  131.         Size := 0;
  132.         try
  133.             Read(InFile,Size);
  134.         except
  135.             IsCorrect := False;
  136.         end;
  137.     except
  138.         IsCorrect := False;
  139.     End;
  140.     if (IsCorrect) then
  141.     begin
  142.         if (Size < MIN_SIZE) or (Size > MAX_SIZE) then
  143.         begin
  144.             IsCorrect := False;
  145.         end
  146.     end;
  147.     i := 0;
  148.     if IsCorrect then
  149.     begin
  150.         While (IsCorrect and (i < Size) and (not Eof(InFile))) do
  151.         begin
  152.             try
  153.                 for j := 0 to Size - 1 do
  154.                     Read(InFile, Temp);
  155.                 if not ((Temp = 1) or (Temp = 0))then
  156.                     IsCorrect := False;
  157.             except
  158.                 IsCorrect := False;
  159.             end;
  160.             Inc(i);
  161.         end;
  162.     end;
  163.     if (IsCorrect and ((i < Size))) then
  164.     begin
  165.         IsCorrect := False;
  166.     end;
  167.     try
  168.         CloseFile(InFile);
  169.     except
  170.         IsCorrect := False;
  171.     end;
  172.     IsInFileCorrect := IsCorrect;
  173. end;
  174.  
  175. function TFormMain.IsMatrixCorrect(Matrix: TMatrix): Boolean;
  176. var
  177.     i,j: Integer;
  178.     Answer: Boolean;
  179. begin
  180.     Answer := True;
  181.     i := 0;
  182.     j := 0;
  183.     while (Answer and (i < Length(Matrix)))  do
  184.     begin
  185.         while (Answer and (j < Length(Matrix[0])))do
  186.         begin
  187.             if ((Matrix[i,j] = 1) and (Matrix[j,i] <> 1)) then
  188.                 Answer := False;
  189.             Inc(j);
  190.         end;
  191.         Inc(i);
  192.     end;
  193.     Result := Answer;
  194. end;
  195.  
  196. procedure TFormMain.OpenFromFileMenuClick(Sender: TObject);
  197. var
  198.     i,j, Size: Integer;
  199.     inFile: TextFile;
  200.     Matrix: TMatrix;
  201.     IsCorrect: Boolean;
  202. begin
  203.     IsCorrect := True;
  204.     if OpenFromFile.Execute() then
  205.     begin
  206.         if (IsInFileCorrect(OpenFromFile.FileName)) then
  207.         begin
  208.             AssignFile(InFile, OpenFromFile.FileName);
  209.             Reset(InFile);
  210.             Read(InFile, Size);
  211.             SpinEdit.Value := Size;
  212.             SetLength(Matrix, Size, Size);
  213.             for i := 0 to Size - 1 do
  214.             begin
  215.                 for j := 0 to Size - 1 do
  216.                 begin
  217.                     Read(InFile,Matrix[i,j]);
  218.                     StringGrid.Cells[j + 1,i + 1] := IntToStr(Matrix[i,j]);
  219.                 end;
  220.  
  221.             end;
  222.             CloseFile(InFile);
  223.             if IsMatrixCorrect(Matrix) then
  224.             begin
  225.                 N4.Enabled := True;
  226.                 Button1.Click;
  227.             end
  228.             else
  229.             begin
  230.                 FormCreate(FormMain);
  231.                 SpinEdit.Value := 2;
  232.             end;
  233.         end
  234.         else
  235.             IsCorrect := False;
  236.     end
  237.     else
  238.         IsCorrect := False;
  239.     if not IsCorrect then
  240.         Application.MessageBox('Работа с файлом некорректна', 'Ошибка', MB_ICONERROR);
  241. end;
  242.  
  243. procedure TFormMain.FindList;
  244. var
  245.     i,j: Integer;
  246.     Current, Temp: TNode;
  247. begin
  248.     SetLength(Arr,SpinEdit.Value);
  249.     for i := 0 to Length(Arr) - 1 do
  250.     begin
  251.         for j := 1 to Length(Arr) do
  252.             if StringGrid.Cells[j, i + 1] = '1' then
  253.             begin
  254.                 New(Temp);
  255.                 Temp.pNumber := j;
  256.                 Temp.pNext := nil;
  257.                 if Arr[i] = nil then
  258.                     Arr[i] := Temp
  259.                 else
  260.                 begin
  261.                     Current := Arr[i];
  262.                     while Current.pNext <> nil do
  263.                         Current := Current.pNext;
  264.                     Current.pNext := Temp
  265.                 end;
  266.             end;
  267.     end;
  268. end;
  269.  
  270. procedure TFormMain.FillStringGrid;
  271. var
  272.     Current: TNode;
  273.     i, Counter: Integer;
  274. begin
  275.     StringGrid1.Enabled := True;
  276.     for i := 0 to Length(Arr)  - 1 do
  277.     begin
  278.         Counter := 1;
  279.         Current := Arr[i];
  280.         while Current <> nil do
  281.         begin
  282.             StringGrid1.Cells[Counter, i + 1] := IntToStr(Current.pNumber);
  283.             Inc(Counter);
  284.             Current := Current.pNext;
  285.         end;
  286.     end;
  287.     StringGrid1.Enabled := False;
  288. end;
  289.  
  290. procedure TFormMain.ClearList;
  291. var
  292.     Current, Prev: TNode;
  293.     i: Integer;
  294. begin
  295.     for i := 0 to Length(Arr) - 1 do
  296.     begin
  297.         while Arr[i] <> nil do
  298.         begin  
  299.             Current := Arr[i];
  300.             while Current.pNext <> nil do
  301.             begin
  302.                 Prev := Current;
  303.                 Current := Current.pNext;
  304.             end;
  305.             if Current <> Arr[i] then
  306.             begin
  307.                 Prev.pNext := nil;
  308.             end
  309.             else
  310.                 Arr[i] := nil;
  311.             Dispose(Current);    
  312.         end;
  313.     end;
  314. end;
  315.  
  316. procedure TFormMain.Button1Click(Sender: TObject);
  317. begin
  318.     ClearGrid;
  319.     ClearList;
  320.     FindList;
  321.     FillStringGrid;
  322.     N4.Enabled := True;
  323. end;
  324.  
  325. procedure TFormMain.ClearStringGrid;
  326. var
  327.     i,j: Integer;
  328. begin
  329.     for i := 1 to StringGrid.ColCount do
  330.         for j := 1 to StringGrid.RowCount do
  331.             StringGrid.Cells[i, j] := '0';
  332. end;
  333.  
  334. procedure TFormMain.SaveToFileMenuClick(Sender: TObject);
  335. var
  336.     OutputFile: TextFile;
  337.     i,j: Integer;
  338. begin
  339.     if SaveDialog.Execute() and FileExists(SaveDialog.FileName) then
  340.     begin
  341.         AssignFile(OutputFile, SaveDialog.FileName);
  342.         try
  343.             Rewrite(OutputFile);
  344.             Writeln(OutputFile,'Списки инцинденций:');
  345.             for i := 1 to SpinEdit.Value do
  346.             begin
  347.                 j := 1;
  348.                 Write(OutputFile,IntToStr(i) + ': ');
  349.                 while (StringGrid1.Cells[j +1 ,i] <> '') do
  350.                 begin
  351.                     Write(OutputFile,StringGrid1.Cells[j,i] + ' --> ');
  352.                     Inc(j);
  353.                 end;
  354.                 Writeln(OutputFile,StringGrid1.Cells[j,i]);
  355.             end;
  356.             CloseFile(OutputFile);
  357.             Application.MessageBox('Данные успешно записаны в файл!', 'Сохранение', MB_ICONINFORMATION);
  358.         except
  359.             Application.MessageBox('Отказано в доступе! Измените параметры файла! ', 'Ошибка!', MB_ICONERROR);
  360.         end;
  361.     end
  362.     else
  363.         Application.MessageBox('Введено некорректное имя файла', 'Ошибка!', MB_ICONERROR);
  364. end;
  365.  
  366. procedure TFormMain.SpinEditChange(Sender: TObject);
  367. begin
  368.     if (SpinEdit.Value >= StringGrid.ColCount) then
  369.     begin
  370.  
  371.         repeat
  372.             ClearStringGrid;
  373.             ClearGrid;
  374.             StringGrid.ColCount := StringGrid.ColCount + 1;
  375.             StringGrid.RowCount := StringGrid.RowCount + 1;
  376.             StringGrid1.RowCount := StringGrid1.RowCount + 1;
  377.             StringGrid1.ColCount := StringGrid1.ColCount + 1;
  378.             StringGrid1.Cells[0, StringGrid1.RowCount - 1] := IntToStr(StringGrid1.RowCount - 1);
  379.             StringGrid.Cells[StringGrid.RowCount - 1, 0] := IntToStr(StringGrid.RowCount - 1);
  380.             StringGrid.Cells[0, StringGrid.RowCount - 1] := IntToStr(StringGrid.RowCount - 1);
  381.         until(SpinEdit.Value = StringGrid.ColCount - 1);
  382.     end
  383.     else
  384.     begin
  385.         repeat
  386.             ClearStringGrid;
  387.             ClearGrid;
  388.             StringGrid.Cells[StringGrid.RowCount, 0] := '';
  389.             StringGrid.Cells[0, StringGrid.RowCount] := '';
  390.             StringGrid1.Cells[0, StringGrid1.RowCount] := '';
  391.             StringGrid1.ColCount := StringGrid1.ColCount - 1;
  392.             StringGrid1.RowCount := StringGrid1.RowCount - 1;
  393.             StringGrid.ColCount := StringGrid.ColCount - 1;
  394.             StringGrid.RowCount := StringGrid.RowCount - 1;
  395.         until(SpinEdit.Value = StringGrid.ColCount - 1);
  396.     end;
  397. end;
  398.  
  399.  
  400.  
  401. procedure TFormMain.StringGridKeyPress(Sender: TObject; var Key: Char);
  402. begin
  403.     if Key = '1' then
  404.     begin
  405.         StringGrid.Cells[StringGrid.Col, StringGrid.Row] := '1';
  406.         StringGrid.Cells[StringGrid.Row, StringGrid.Col] := '1';
  407.     end
  408.     else
  409.         if Key = '0' then
  410.         begin
  411.             StringGrid.Cells[StringGrid.Col, StringGrid.Row] := '0';
  412.             StringGrid.Cells[StringGrid.Row, StringGrid.Col] := '0';
  413.         end;
  414.     Key := #0;
  415. end;
  416.  
  417. procedure TFormMain.StringGridSelectCell(Sender: TObject; ACol, ARow: Integer;
  418.   var CanSelect: Boolean);
  419. begin
  420.     if (ACol < 1) or (ARow < 1) then
  421.         CanSelect := False;
  422. end;
  423.  
  424. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement