Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit12;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, Vcl.Menus;
- type
- TForm12 = class(TForm)
- sizeEdit: TEdit;
- sizeButton: TButton;
- matrixGrid: TStringGrid;
- convertButton: TButton;
- answerEdit: TMemo;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- Open1: TMenuItem;
- Save1: TMenuItem;
- About1: TMenuItem;
- procedure UpdateGrid();
- procedure FormCreate(Sender: TObject);
- procedure sizeButtonClick(Sender: TObject);
- procedure convertButtonClick(Sender: TObject);
- procedure ConvertMatrix();
- procedure About1Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure Open1Click(Sender: TObject);
- procedure Save1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form12: TForm12;
- implementation
- {$R *.dfm}
- function checkValue(Min, Max: Integer; Value, Name: String; var NumericValue: Integer): Boolean;
- var
- Number: Integer;
- Output: String;
- IsCorrect: Boolean;
- begin
- IsCorrect := true;
- if (Value = '') then
- begin
- MessageDlg('Input value for ' + Name, mtError, [mbOK], 0);
- IsCorrect := false;
- end;
- if IsCorrect then
- try
- Number := StrToInt(Value);
- except
- MessageDlg('Input numerical value for ' + Name, mtError, [mbOK], 0);
- IsCorrect := false;
- end;
- if IsCorrect then
- if (Number < Min) OR (Number >= Max) then
- begin
- MessageDlg(Name + ' should be between ' + IntToStr(Min) + ' and ' + IntToStr(Max-1), mtError, [mbOK], 0);
- IsCorrect := false;
- end;
- if IsCorrect then
- NumericValue := Number;
- checkValue := IsCorrect;
- end;
- procedure TForm12.About1Click(Sender: TObject);
- begin
- MessageDlg('This program converts adjacent matrix to incident list.', mtInformation, [mbOk], 0);
- end;
- procedure TForm12.convertButtonClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- i, j, Temp: Integer;
- begin
- IsCorrect := true;
- for i := 1 to (matrixGrid.ColCount - 1) do
- for j := 1 to (matrixGrid.ColCount - 1) do
- if IsCorrect then
- IsCorrect := checkValue(0,11,matrixGrid.Cells[i,j],'element ' + IntToStr(i) + ':' + IntToStr(j), Temp);
- if IsCorrect then
- ConvertMatrix();
- end;
- procedure TForm12.ConvertMatrix;
- var
- i, j, k, Amount: Integer;
- Answer: String;
- begin
- answerEdit.Clear();
- for i := 1 to (matrixGrid.ColCount - 1) do
- begin
- Answer := Answer + IntToStr(i) + ': ';
- for j := 1 to (matrixGrid.ColCount - 1) do
- begin
- Amount := StrToInt(matrixGrid.Cells[j,i]);
- for k := 0 to (Amount - 1) do
- Answer := Answer + IntToStr(j) + ' ';
- end;
- Answer := Answer + #13#10;
- end;
- answerEdit.Text := Answer;
- end;
- procedure TForm12.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- if (MessageDlg('Really close this window?', mtConfirmation, [mbOk, mbCancel], 0) = mrCancel) then
- CanClose := false;
- end;
- procedure TForm12.FormCreate(Sender: TObject);
- begin
- UpdateGrid();
- answerEdit.Clear();
- end;
- procedure TForm12.Open1Click(Sender: TObject);
- var
- FileName, Line: String;
- Dlg: TOpenDialog;
- TFile: TextFile;
- Temp, Amount, i, j: Integer;
- IsCorrect: Boolean;
- begin
- UpdateGrid();
- answerEdit.Clear();
- FileName := '';
- Dlg := TOpenDialog.Create(nil);
- try
- Dlg.InitialDir := 'C:\';
- Dlg.Filter := 'Text files (*.txt)|*.txt';
- if Dlg.Execute(Handle) then
- FileName := Dlg.FileName;
- finally
- Dlg.Free;
- end;
- if FileName <> '' then
- begin
- AssignFile(TFile, FileName);
- Reset(TFile);
- ReadLn(TFile, Line);
- IsCorrect := checkValue(2, 6, Line, 'size of matrix', Amount);
- if IsCorrect then
- begin
- matrixGrid.ColCount := Amount+1;
- matrixGrid.RowCount := Amount+1;
- UpdateGrid();
- for i := 0 to (Amount - 1) do
- begin
- ReadLn(TFile, Line);
- IsCorrect := checkValue(Amount, Amount+1, IntToStr(Line.Length), 'length of row ' + IntToStr(i+1), Temp);
- if IsCorrect then
- for j := 0 to (Amount - 1) do
- matrixGrid.Cells[j+1,i+1] := Line[j+1]
- else
- begin
- UpdateGrid();
- break;
- end;
- end;
- end;
- CloseFile(TFile);
- end;
- end;
- procedure TForm12.Save1Click(Sender: TObject);
- var
- Dlg: TSaveDialog;
- TFile: TextFile;
- FileName: String;
- begin
- if answerEdit.Text = '' then
- begin
- MessageDlg('Nothing to save', mtError, [mbOk], 0);
- end
- else
- begin
- FileName := '';
- Dlg := TSaveDialog.Create(nil);
- try
- Dlg.InitialDir := 'C:\';
- Dlg.Filter := 'Text files (*.txt)|*.txt';
- Dlg.DefaultExt := 'txt';
- if Dlg.Execute then
- FileName := Dlg.FileName;
- finally
- Dlg.Free;
- end;
- if FileName <> '' then
- begin
- AssignFile(TFile, FileName);
- Rewrite(TFile);
- WriteLn(TFile, answerEdit.Text);
- CloseFile(TFile);
- MessageDlg('File is saved', mtInformation, [mbOk], 0);
- end;
- end;
- end;
- procedure TForm12.sizeButtonClick(Sender: TObject);
- var
- Size: Integer;
- IsCorrect: Boolean;
- begin
- IsCorrect := checkValue(2,6,sizeEdit.Text,'size of matrix',Size);
- if IsCorrect then
- begin
- matrixGrid.ColCount := Size+1;
- matrixGrid.RowCount := Size+1;
- UpdateGrid();
- end;
- end;
- procedure TForm12.UpdateGrid();
- var
- i, j: Integer;
- begin
- for i := 0 to (matrixGrid.ColCount - 1) do
- for j := 0 to (matrixGrid.ColCount - 1) do
- begin
- if (i = 0) and (j <> 0) then
- matrixGrid.Cells[i,j] := IntToStr(j)
- else if (i<>0) and (j = 0) then
- matrixGrid.Cells[i,j] := IntToStr(i)
- else
- matrixGrid.Cells[i,j] := '';
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement