Advertisement
believe_me

Untitled

May 22nd, 2022
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 14.98 KB | None | 0 0
  1. unit MainForm;
  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, SimpleFormUnit, Vcl.Menus, Vcl.Grids,
  8.   Vcl.StdCtrls, Vcl.Buttons, Vcl.Samples.Spin, Vcl.ValEdit, ResForm, System.UITypes;
  9.  
  10. type
  11.    
  12.   TForm7_2 = class(TSimpleForm)
  13.     edtVCount: TSpinEdit;
  14.     btnDone: TBitBtn;
  15.     grdIncedence: TStringGrid;
  16.     FileMenu: TMenuItem;
  17.     SaveFile: TMenuItem;
  18.     OpenFile: TMenuItem;
  19.     OpenDialog: TOpenDialog;
  20.     SaveDialog: TSaveDialog;
  21.     procedure btnDoneClick(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure edtVCountChange(Sender: TObject);
  24.     procedure grdIncedenceSetEditText(Sender: TObject; ACol, ARow: Integer;
  25.       const Value: string);
  26.     procedure AnalyseStr(const Value: String; Row: Integer);
  27.     function IsItValidStr(const Value:String): Boolean;
  28.     procedure FormKeyPress(Sender: TObject; var Key: Char);
  29.     procedure SaveFileClick(Sender: TObject);
  30.     procedure OpenFileClick(Sender: TObject);
  31.     procedure SetInstructions(); override;
  32.     procedure AnalyseList();
  33.   end;
  34.  
  35. Const
  36.     ENTER_CODE = 13;
  37.    
  38. var
  39.   Form7_2: TForm7_2;
  40.   IncList: PList;
  41.  
  42.  
  43. implementation
  44.  
  45. {$R *.dfm}
  46.  
  47. function TForm7_2.IsItValidStr(const Value:String): Boolean;
  48. Const
  49.     VALID_ARRAY: array [1..11] of String[1] = ('0','1', '2', '3', '4', '5', '6', '7', '8', '9', ' ');
  50.     MIN_V = 1;
  51. function IsNumbCorrect(const Buff: String): Boolean;
  52. Var
  53.     N, MaxV: Integer;
  54. Begin
  55.     MaxV := Self.grdIncedence.RowCount-1;
  56.     Result := True;
  57.     N := StrToInt(Buff);
  58.     if (N > MaxV) Or (N < MIN_V) then
  59.         Result := False;
  60. End;
  61. Var
  62.     I, J: Integer;
  63.     Find: Boolean;
  64.     Buffer: String[2];
  65. Begin
  66.     Result := True;
  67.     I := 1;
  68.     Buffer := '';
  69.     While (I <= Length(Value)) And (Result) do
  70.     Begin
  71.         Find := False;
  72.         J := 1;
  73.         While(J <= Length(VALID_ARRAY)) And (not Find) Do
  74.         Begin
  75.             if Value[I] = VALID_ARRAY[J] then
  76.                 Find := True;
  77.             Inc(J);
  78.         End;
  79.         if Value[I] <> ' ' then
  80.             Buffer := Value[I]+Buffer
  81.         Else
  82.         Begin
  83.             Find := IsNumbCorrect(Buffer);
  84.             Buffer := '';
  85.         End;
  86.         if not Find then
  87.             Result := Find;
  88.         Inc(I);
  89.     End;
  90.     if buffer <> '' then
  91.         Result := IsNumbCorrect(Buffer);
  92. End;
  93.  
  94. procedure TForm7_2.OpenFileClick(Sender: TObject);
  95. Var
  96.     OFile: TextFile;
  97. begin
  98.     if OpenDialog.Execute then
  99.     Begin
  100.         If FileExists(OpenDialog.FileName) then
  101.         Begin
  102.             if TrySetInputFile(OFile, OpenDialog.FileName) then
  103.             Begin
  104.                 OpenFromFile(OFile, Self.grdIncedence, edtVCount);
  105.                 CloseFile(OFile);
  106.             End
  107.             Else
  108.                 MessageDlg('Something was wrong', mtError, [mbOk], 0);
  109.         End;
  110.     End;
  111. end;
  112.  
  113. procedure TForm7_2.SaveFileClick(Sender: TObject);
  114. Var
  115.     SFile: TextFile;
  116. begin
  117.     if SaveDialog.Execute then
  118.     Begin
  119.         If FileExists(SaveDialog.FileName) then
  120.         Begin
  121.             if TrySetOutputFile(SFile, SaveDialog.FileName) then
  122.             Begin
  123.                 SaveInFile(SFile, Self.grdIncedence);
  124.                 CloseFile(SFile);
  125.                 MessageDlg('Successfully saved', mtInformation, [mbOk], 0);
  126.             End
  127.             Else
  128.                 MessageDlg('Something was wrong', mtError, [mbOk], 0);
  129.         End;
  130.     End;
  131. end;
  132.  
  133. procedure TForm7_2.SetInstructions;
  134. Const
  135.     NEW_LINE = #13#10;
  136. begin
  137.     Inherited;
  138.     Instructions := 'This program transfer incedence list into adjacncy matrix.'+NEW_LINE
  139.         + 'In first column of grid is number of veryex. In second - incedent vertex.'+NEW_LINE
  140.             +'Locate incedence vertex after spaces. To save list press CTRL+S. To open - CTRL+O.'+NEW_LINE
  141.                 +'To end input in list press ENTER, than press Show result.';
  142. end;
  143.  
  144. procedure TForm7_2.AnalyseList;
  145. Var
  146.     I: Integer;
  147. begin
  148.     if IncList <> Nil then
  149.       ClearList(IncList);
  150.     for I := 1 to grdIncedence.RowCount - 1 do
  151.     Begin
  152.         if IsItValidStr(grdIncedence.Cells[1, I]) then
  153.             AnalyseStr(grdIncedence.Cells[1, I], I);
  154.     End;
  155. end;
  156.  
  157. procedure TForm7_2.AnalyseStr(const Value: String; Row: INteger);
  158. Var
  159.     Reader: String[2];
  160.     I, N, ErrorCode: Integer;
  161. procedure AddNumberToList();
  162. Begin
  163.     Val(Reader, N, ErrorCode);
  164.     if ErrorCode = 0 then
  165.         AddToList(IncList, Row, N);
  166. End;
  167. begin
  168.     Reader := '';
  169.     For I := 1 to Length(Value) do
  170.     Begin
  171.         If Value[I] <> ' ' then
  172.             Reader := Reader+Value[I]
  173.         Else
  174.         Begin
  175.             AddNumberToList;
  176.             Reader := '';
  177.         End;
  178.     End;
  179.     AddNumberToList;
  180. end;
  181.  
  182. procedure TForm7_2.btnDoneClick(Sender: TObject);
  183. begin
  184.      AnalyseList;
  185.      if not Assigned(ResultForm) then
  186.         ResultForm := TResultForm.Create(Self);
  187.      ResultForm.TakeSize(grdIncedence.RowCount-1);
  188.      ResultForm.TakeIncedenceList(IncList);
  189.      ResultForm.Show;
  190.  
  191. end;
  192.  
  193. procedure TForm7_2.edtVCountChange(Sender: TObject);
  194. Const
  195.     WINDOW_DEFAULT_HEIGHT = 150;
  196. begin
  197.     SetGridSize(1, StrToint(edtVCount.Text), grdIncedence);
  198.     if grdIncedence.height > ClientHeight-WINDOW_DEFAULT_HEIGHT then
  199.         ClientHeight := grdIncedence.height+WINDOW_DEFAULT_HEIGHT;
  200. end;
  201.  
  202. procedure TForm7_2.FormCreate(Sender: TObject);
  203. begin
  204.   inherited;
  205.     grdIncedence.Cells[0,0] := 'V';
  206.     grdIncedence.Cells[1,0] := '1';
  207.     grdIncedence.Cells[0,1] := '1';
  208. end;
  209.  
  210.  
  211.  
  212. procedure TForm7_2.FormKeyPress(Sender: TObject; var Key: Char);
  213. begin
  214.   inherited;
  215.    if Key =  Chr(ENTER_CODE) then
  216.    Begin
  217.         btnDoneClick(Sender);
  218.    End;
  219. end;
  220.  
  221. procedure TForm7_2.grdIncedenceSetEditText(Sender: TObject; ACol, ARow: Integer;
  222.   const Value: string);
  223. begin
  224.     if not IsItValidStr(Value) then
  225.     Begin
  226.         MessageDlg('Wrong content!', mtError, [mbOk], 0);
  227.         (Sender as TStringGrid).Cells[ACol, ARow] := '';
  228.     End;
  229. end;
  230.  
  231. end.
  232.  
  233. unit ResForm;
  234.  
  235. interface
  236.  
  237. uses
  238.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  239.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SimpleFormUnit, Vcl.Menus, Vcl.Grids, Vcl.Samples.Spin, System.UITypes;
  240.  
  241. type
  242.     PVirtex = ^TVirtex;
  243.     TVirtex = record
  244.         El: Integer;
  245.         Next: PVirtex;
  246.     end;
  247.     PList = ^TList;
  248.     TList = Record
  249.         Virtex: Integer;
  250.         Incedent: PVirtex;
  251.         Next: PList;
  252.     End;
  253.  
  254.   TResultForm = class(TSimpleForm)
  255.     AdjacencyGrid: TStringGrid;
  256.     Save: TMenuItem;
  257.     SaveDialog: TSaveDialog;
  258.     procedure FormCreate(Sender: TObject);
  259.     procedure TakeSize(N: Integer);
  260.     procedure TakeIncedenceList(P: PList);
  261.     procedure FullFillGrid(Var Grid: TStringGrid; IncList: PList);
  262.     procedure FormShow(Sender: TObject);
  263.     procedure SaveClick(Sender: TObject);
  264.     Private
  265.         GridSize: Integer;
  266.         IncedenceList: PList;
  267.   end;
  268.   procedure SetGridSize(const Width, Height: Integer; var Grid: TStringGrid);
  269.   procedure AddtoList(var List: PList; V: Integer; N: Integer);
  270.   Function TrySetOutputFile(var SF: TextFile;const Name: String): Boolean;
  271.   Procedure SaveInFile(var SF: TextFile;const Grid: TStringGrid);
  272.   Function TrySetInputFile(var IFile: TextFile; const Name: String): Boolean;
  273.   Procedure OpenFromFile(var IFile: TextFile; var Grid: TStringGrid; var Edt: TSpinEdit);
  274.   Procedure ClearList(Var List: PList);
  275. var
  276.   ResultForm: TResultForm;
  277.  
  278. implementation
  279.  
  280. {$R *.dfm}
  281.  
  282. Procedure ClearList(var List: PList);
  283. Var
  284.     DelV: PVirtex;
  285.     Del : PList;
  286. Begin
  287.     While(List <> Nil) do
  288.     Begin
  289.         while List^.Incedent <> Nil do
  290.         Begin
  291.             DelV := List^.Incedent;
  292.             List^.Incedent := List^.Incedent^.Next;
  293.             Dispose(DelV);
  294.         End;
  295.         Del := List;
  296.         List := List^.Next;
  297.         Dispose(Del);
  298.     End;
  299. End;
  300.  
  301. function TrySetOutputFile(var SF: TextFile; const Name: String): Boolean;
  302. Begin
  303.     Result := True;
  304.     AssignFile(SF, Name);
  305.     Try
  306.         Rewrite(SF);
  307.     Except
  308.         Result := False;
  309.     End;
  310. End;
  311.  
  312. Function TrySetInputFile(var IFile: TextFile; const Name: String): Boolean;
  313. Begin
  314.     Result := True;
  315.     AssignFile(IFile, Name);
  316.     Try
  317.         Reset(IFile);
  318.     Except
  319.         Result := False;
  320.     End;
  321. End;
  322.  
  323. procedure OpenFromFile(var IFile: TextFile; var Grid: TStringGrid; var Edt: TSpinEdit);
  324. Var
  325.     I, J, Width: Integer;
  326.     Str: String;
  327.     Buffer: Char;
  328. Begin
  329.     Readln(IFile, Width);
  330.     Edt.Text := IntToStr(Width);
  331.     for I := 1 to Grid.RowCount-1 do
  332.     Begin
  333.         for J := 0 to 1 do
  334.         Begin
  335.             Buffer := 'f';
  336.             Str := '';
  337.             While (Buffer <> ' ') And (J = 0) do
  338.             Begin
  339.                 Read(IFile, Buffer);
  340.                 Str := Str + buffer;
  341.             End;
  342.             If (J = 1) Then
  343.             Begin
  344.                 Read(IFile, Str);
  345.             End;
  346.             Grid.Cells[J, I] := Str;
  347.         End;
  348.         Readln(IFile);
  349.     End;
  350. End;
  351.  
  352. procedure SaveInFile(var SF: TextFile; const Grid: TStringGrid);
  353. Var
  354.     I, J: Integer;
  355. Begin
  356.     Writeln(SF, Grid.RowCount-1);
  357.     for I := 1 to Grid.RowCount-1 do
  358.     Begin
  359.         for J := 0 to Grid.ColCount-1 do
  360.         Begin
  361.             Write(SF, Grid.Cells[J, I]);
  362.             Write(SF, ' ');
  363.         End;
  364.         Writeln(SF);
  365.     End;
  366. End;
  367.  
  368. procedure TResultForm.FormCreate(Sender: TObject);
  369. begin
  370.   inherited;
  371.     AdjacencyGrid.Cells[0,0] := 'V';
  372.     AdjacencyGrid.Cells[1,0] := '1';
  373.     AdjacencyGrid.Cells[0,1] := '1';
  374. end;
  375. procedure TResultForm.TakeSize(N: Integer);
  376. Begin
  377.     GridSize := N;
  378. End;
  379.  
  380. procedure TResultForm.TakeIncedenceList(P: PList);
  381. Begin
  382.     IncedenceList := P;
  383. End;
  384.  
  385. procedure SetGridSize(const Width, Height: Integer; var Grid: TStringGrid);
  386. Var
  387.     I: Integer;
  388. begin
  389.     Grid.ColCount := Width+1;
  390.     Grid.RowCount := Height+1;
  391.     for I := 1 to Height do
  392.     Begin
  393.         Grid.Cells[I, 0] := IntToStr(I);
  394.         Grid.Cells[0, I] := IntToStr(I);
  395.     End;
  396.     Grid.Width := (width+1)*Grid.DefaultColWidth + 15;
  397.     Grid.Height := (height+1)*Grid.DefaultRowHeight + 15;
  398. end;
  399.  
  400. procedure AddtoList(var List: PList; V: Integer; N: Integer);
  401. Var
  402.     Find: Boolean;
  403.     Save, Temp: PList;
  404.     InTemp: PVirtex;
  405. Begin
  406.     Find := False;
  407.     if List = Nil then
  408.     Begin
  409.         New(List);
  410.         List^.Virtex := V;
  411.         New(List^.Incedent);
  412.         List^.Incedent^.El := N;
  413.         List^.Next := Nil;
  414.         List^.Incedent^.Next := Nil;
  415.         Find := True;
  416.     End;
  417.     Temp := List;
  418.     while (not Find) And (Temp <> Nil) do
  419.     Begin
  420.         if Temp^.Virtex = V then
  421.         Begin
  422.             InTemp := Temp^.Incedent;
  423.             While(InTemp^.Next  <> Nil) do
  424.                 InTemp := InTemp^.Next;
  425.             Find := True;
  426.             New(InTemp^.Next);
  427.             InTemp := InTemp^.Next;
  428.             InTemp^.El := N;
  429.             InTemp^.Next := Nil;
  430.         End
  431.         Else
  432.         Begin
  433.             if Temp^.Next = Nil then
  434.                 Save := Temp;
  435.             Temp := Temp^.Next;
  436.         End;
  437.     End;
  438.     if not Find then
  439.     Begin
  440.         New(Save^.Next);
  441.         Save := Save^.Next;
  442.         Save^.Virtex := V;
  443.         New(Save^.Incedent);
  444.         Save^.Incedent^.El := N;
  445.         Save.Next := Nil;
  446.         Save^.Incedent^.Next := Nil;
  447.     End;
  448. End;
  449.  
  450. procedure TResultForm.FormShow(Sender: TObject);
  451. begin
  452.   inherited;
  453.     SetGridSize(GridSize, GridSize, AdjacencyGrid);
  454.     if AdjacencyGrid.height > ClientHeight then
  455.         ClientHeight := AdjacencyGrid.height + 30;
  456.     if AdjacencyGrid.Width > ClientWidth then
  457.         ClientWidth := AdjacencyGrid.Width + 30;
  458.     FullFillGrid(AdjacencyGrid, IncedenceList);
  459. end;
  460.  
  461. procedure TResultForm.FullFillGrid(var Grid: TStringGrid; IncList: PList);
  462. procedure InitGridWithZero();
  463. var
  464.     I, J: Integer;
  465. Begin
  466.     for I := 1 to Grid.RowCount-1 do
  467.         for J := 1 to Grid.ColCount-1 do
  468.             Grid.Cells[J, I] := '0';
  469. End;
  470. var
  471.   Temp :PList;
  472. begin
  473.     Temp := IncList;
  474.     InitGridWithZero;
  475.     while Temp <> Nil do
  476.     Begin
  477.         while Temp^.Incedent <> Nil do
  478.         Begin
  479.             Grid.Cells[Temp^.Virtex, Temp^.Incedent^.El] := '1';
  480.             Temp^.Incedent := Temp^.Incedent^.Next;
  481.         End;
  482.         Temp := Temp^.Next;
  483.     End;
  484.  
  485. end;
  486.  
  487. procedure TResultForm.SaveClick(Sender: TObject);
  488. Var
  489.     SFile: TextFile;
  490. begin
  491.     if SaveDialog.Execute then
  492.     Begin
  493.         If FileExists(SaveDialog.FileName) then
  494.         Begin
  495.             if TrySetOutputFile(SFile, SaveDialog.FileName) then
  496.             Begin
  497.                 SaveInFile(SFile, Self.AdjacencyGrid);
  498.                 CloseFile(SFile);
  499.                 MessageDlg('Successfully saved', mtInformation, [mbOk], 0);
  500.             End
  501.             Else
  502.                 MessageDlg('Something was wrong', mtError, [mbOk], 0);
  503.         End;
  504.     End;
  505. end;
  506.  
  507. end.
  508.  
  509. Unit SimpleFormUnit;
  510.  
  511. interface
  512.  
  513. uses
  514.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  515.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, System.UITypes;
  516.  
  517. type
  518.   TMatrix = Array of array of Integer;
  519.   TSimpleForm = class(TForm)
  520.     Menu: TMainMenu;
  521.     Instruction: TMenuItem;
  522.     Developer: TMenuItem;
  523.     procedure InstructionClick(Sender: TObject);
  524.     procedure DeveloperClick(Sender: TObject);
  525.     Procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  526.     procedure SetInstructions(); virtual;
  527.     procedure FormCreate(Sender: TObject);
  528.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); virtual;
  529.     Procedure FormCloseQueryNope(Sender: TObject; var CanClose: Boolean);
  530.   private
  531.     { Private declarations }
  532.   protected
  533.     Instructions: String;
  534.   public
  535.     { Public declarations }
  536.   end;
  537.  
  538. var
  539.   SimpleForm: TSimpleForm;
  540.  
  541. implementation
  542.  
  543. {$R *.dfm}
  544.  
  545. procedure TSimpleForm.DeveloperClick(Sender: TObject);
  546. begin
  547.     ShowMessage('Yegor Rusakovich, 151002');
  548. end;
  549.  
  550. procedure TSimpleForm.InstructionClick(Sender: TObject);
  551. begin
  552.     ShowMessage(Instructions);
  553. end;
  554.  
  555. Procedure TSimpleForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  556. begin
  557.     CanClose := False;
  558.     if MessageDlg('Are you sure you want to quit?',mtConfirmation, mbOKCancel, 0) = mrOk then
  559.     begin
  560.         CanClose := True;
  561.     end;
  562. end;
  563.  
  564. Procedure TSimpleForm.FormCloseQueryNope(Sender: TObject; var CanClose: Boolean);
  565. Begin
  566.     CanClose := True;
  567. End;
  568.  
  569. procedure TSimpleForm.SetInstructions();
  570. begin
  571.     Instructions := 'Press F1 to see this message.' + #13#10 + 'Press F3 to estimate the authors name.' + #13#10 + 'Press escape to close window.'+#13#10;
  572. end;
  573.  
  574. procedure TSimpleForm.FormCreate(Sender: TObject);
  575. begin
  576.     SetInstructions;
  577. end;
  578.  
  579. procedure TSimpleForm.FormKeyDown(Sender: TObject; var Key: Word;
  580.   Shift: TShiftState);
  581. begin
  582.     if (Key = VK_ESCAPE) then
  583.         Self.Close;
  584. end;
  585.  
  586.  
  587. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement