Advertisement
madopew

Untitled

May 6th, 2020
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.48 KB | None | 0 0
  1. unit Unit12;
  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, Vcl.Menus;
  8.  
  9. type
  10.   TForm12 = class(TForm)
  11.     sizeEdit: TEdit;
  12.     sizeButton: TButton;
  13.     matrixGrid: TStringGrid;
  14.     convertButton: TButton;
  15.     answerEdit: TMemo;
  16.     MainMenu1: TMainMenu;
  17.     File1: TMenuItem;
  18.     Open1: TMenuItem;
  19.     Save1: TMenuItem;
  20.     About1: TMenuItem;
  21.     procedure UpdateGrid();
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure sizeButtonClick(Sender: TObject);
  24.     procedure convertButtonClick(Sender: TObject);
  25.     procedure ConvertMatrix();
  26.     procedure About1Click(Sender: TObject);
  27.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  28.     procedure Open1Click(Sender: TObject);
  29.     procedure Save1Click(Sender: TObject);
  30.   private
  31.     { Private declarations }
  32.   public
  33.     { Public declarations }
  34.   end;
  35.  
  36. var
  37.   Form12: TForm12;
  38.  
  39. implementation
  40.  
  41. {$R *.dfm}
  42.  
  43. function checkValue(Min, Max: Integer; Value, Name: String; var NumericValue: Integer): Boolean;
  44. var
  45.     Number: Integer;
  46.     Output: String;
  47.     IsCorrect: Boolean;
  48. begin
  49.     IsCorrect := true;
  50.     if (Value = '') then
  51.     begin
  52.         MessageDlg('Input value for ' + Name, mtError, [mbOK], 0);
  53.         IsCorrect := false;
  54.     end;
  55.  
  56.     if IsCorrect then
  57.         try
  58.             Number := StrToInt(Value);
  59.         except
  60.             MessageDlg('Input numerical value for ' + Name, mtError, [mbOK], 0);
  61.             IsCorrect := false;
  62.         end;
  63.  
  64.     if IsCorrect then
  65.         if (Number < Min) OR (Number >= Max) then
  66.         begin
  67.             MessageDlg(Name + ' should be between ' + IntToStr(Min) + ' and ' + IntToStr(Max-1), mtError, [mbOK], 0);
  68.             IsCorrect := false;
  69.         end;
  70.  
  71.     if IsCorrect then
  72.         NumericValue := Number;
  73.  
  74.     checkValue := IsCorrect;
  75. end;
  76.  
  77. procedure TForm12.About1Click(Sender: TObject);
  78. begin
  79.     MessageDlg('This program converts adjacent matrix to incident list.', mtInformation, [mbOk], 0);
  80. end;
  81.  
  82. procedure TForm12.convertButtonClick(Sender: TObject);
  83. var
  84.     IsCorrect: Boolean;
  85.     i, j, Temp: Integer;
  86. begin
  87.     IsCorrect := true;
  88.     for i := 1 to (matrixGrid.ColCount - 1) do
  89.         for j := 1 to (matrixGrid.ColCount - 1) do
  90.             if IsCorrect then
  91.                 IsCorrect := checkValue(0,11,matrixGrid.Cells[i,j],'element ' + IntToStr(i) + ':' + IntToStr(j), Temp);
  92.  
  93.     if IsCorrect then
  94.         ConvertMatrix();
  95. end;
  96.  
  97. procedure TForm12.ConvertMatrix;
  98. var
  99.     i, j, k, Amount: Integer;
  100.     Answer: String;
  101. begin
  102.     answerEdit.Clear();
  103.     for i := 1 to (matrixGrid.ColCount - 1) do
  104.     begin
  105.         Answer := Answer + IntToStr(i) + ': ';
  106.         for j := 1 to (matrixGrid.ColCount - 1) do
  107.         begin
  108.             Amount := StrToInt(matrixGrid.Cells[j,i]);
  109.             for k := 0 to (Amount - 1) do
  110.                 Answer := Answer + IntToStr(j) + ' ';
  111.         end;
  112.         Answer := Answer + #13#10;
  113.     end;
  114.     answerEdit.Text := Answer;
  115. end;
  116.  
  117. procedure TForm12.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  118. begin
  119.     if (MessageDlg('Really close this window?', mtConfirmation, [mbOk, mbCancel], 0) = mrCancel) then
  120.         CanClose := false;
  121. end;
  122.  
  123. procedure TForm12.FormCreate(Sender: TObject);
  124. begin
  125.     UpdateGrid();
  126.     answerEdit.Clear();
  127. end;
  128.  
  129. procedure TForm12.Open1Click(Sender: TObject);
  130. var
  131.     FileName, Line: String;
  132.     Dlg: TOpenDialog;
  133.     TFile: TextFile;
  134.     Temp, Amount, i, j: Integer;
  135.     IsCorrect: Boolean;
  136. begin
  137.     UpdateGrid();
  138.     answerEdit.Clear();
  139.     FileName := '';
  140.     Dlg := TOpenDialog.Create(nil);
  141.     try
  142.         Dlg.InitialDir := 'C:\';
  143.         Dlg.Filter := 'Text files (*.txt)|*.txt';
  144.         if Dlg.Execute(Handle) then
  145.             FileName := Dlg.FileName;
  146.     finally
  147.         Dlg.Free;
  148.     end;
  149.  
  150.     if FileName <> '' then
  151.     begin
  152.         AssignFile(TFile, FileName);
  153.         Reset(TFile);
  154.         ReadLn(TFile, Line);
  155.         IsCorrect := checkValue(2, 6, Line, 'size of matrix', Amount);
  156.         if IsCorrect then
  157.         begin
  158.             matrixGrid.ColCount := Amount+1;
  159.             matrixGrid.RowCount := Amount+1;
  160.             UpdateGrid();
  161.             for i := 0 to (Amount - 1) do
  162.             begin
  163.                 ReadLn(TFile, Line);
  164.                 IsCorrect := checkValue(Amount, Amount+1, IntToStr(Line.Length), 'length of row ' + IntToStr(i+1), Temp);
  165.                 if IsCorrect then
  166.                     for j := 0 to (Amount - 1) do
  167.                         matrixGrid.Cells[j+1,i+1] := Line[j+1]
  168.                 else
  169.                 begin
  170.                     UpdateGrid();
  171.                     break;
  172.                 end;
  173.             end;
  174.         end;
  175.         CloseFile(TFile);
  176.     end;
  177. end;
  178.  
  179. procedure TForm12.Save1Click(Sender: TObject);
  180. var
  181.     Dlg: TSaveDialog;
  182.     TFile: TextFile;
  183.     FileName: String;
  184. begin
  185.     if answerEdit.Text = '' then
  186.     begin
  187.         MessageDlg('Nothing to save', mtError, [mbOk], 0);
  188.     end
  189.     else
  190.     begin
  191.         FileName := '';
  192.         Dlg := TSaveDialog.Create(nil);
  193.         try
  194.             Dlg.InitialDir := 'C:\';
  195.             Dlg.Filter := 'Text files (*.txt)|*.txt';
  196.             Dlg.DefaultExt := 'txt';
  197.             if Dlg.Execute then
  198.                 FileName := Dlg.FileName;
  199.         finally
  200.             Dlg.Free;
  201.         end;
  202.  
  203.         if FileName <> '' then
  204.         begin
  205.             AssignFile(TFile, FileName);
  206.             Rewrite(TFile);
  207.             WriteLn(TFile, answerEdit.Text);
  208.             CloseFile(TFile);
  209.             MessageDlg('File is saved', mtInformation, [mbOk], 0);
  210.         end;
  211.     end;
  212. end;
  213.  
  214. procedure TForm12.sizeButtonClick(Sender: TObject);
  215. var
  216.     Size: Integer;
  217.     IsCorrect: Boolean;
  218. begin
  219.     IsCorrect := checkValue(2,6,sizeEdit.Text,'size of matrix',Size);
  220.     if IsCorrect then
  221.     begin
  222.         matrixGrid.ColCount := Size+1;
  223.         matrixGrid.RowCount := Size+1;
  224.         UpdateGrid();
  225.     end;
  226. end;
  227.  
  228. procedure TForm12.UpdateGrid();
  229. var
  230.     i, j: Integer;
  231. begin
  232.     for i := 0 to (matrixGrid.ColCount - 1) do
  233.         for j := 0 to (matrixGrid.ColCount - 1) do
  234.         begin
  235.             if (i = 0) and (j <> 0) then
  236.                 matrixGrid.Cells[i,j] := IntToStr(j)
  237.             else if (i<>0) and (j = 0) then
  238.                 matrixGrid.Cells[i,j] := IntToStr(i)
  239.             else
  240.                 matrixGrid.Cells[i,j] := '';
  241.         end;
  242. end;
  243.  
  244. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement