Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit SimpleFormUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, System.UITypes;
- type
- TMatrix = Array of array of Integer;
- TSimpleForm = class(TForm)
- Menu: TMainMenu;
- Instruction: TMenuItem;
- Developer: TMenuItem;
- procedure InstructionClick(Sender: TObject);
- procedure DeveloperClick(Sender: TObject);
- Procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure SetInstructions(); virtual;
- procedure FormCreate(Sender: TObject);
- procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); virtual;
- Procedure FormCloseQueryNope(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- protected
- Instructions: String;
- public
- { Public declarations }
- end;
- var
- SimpleForm: TSimpleForm;
- implementation
- {$R *.dfm}
- procedure TSimpleForm.DeveloperClick(Sender: TObject);
- begin
- ShowMessage('Ravodin Alexander, group 151002');
- end;
- procedure TSimpleForm.InstructionClick(Sender: TObject);
- begin
- ShowMessage(Instructions);
- end;
- Procedure TSimpleForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := False;
- if MessageDlg('Are you sure you want to quit?',mtConfirmation, mbOKCancel, 0) = mrOk then
- begin
- CanClose := True;
- end;
- end;
- Procedure TSimpleForm.FormCloseQueryNope(Sender: TObject; var CanClose: Boolean);
- Begin
- CanClose := True;
- End;
- procedure TSimpleForm.SetInstructions();
- begin
- Instructions := 'F1 - instruction window.' + #13#10 + 'F3 - developer window.'
- + #13#10 + 'ESCAPE - close window.'+#13#10;
- end;
- procedure TSimpleForm.FormCreate(Sender: TObject);
- begin
- SetInstructions;
- end;
- procedure TSimpleForm.FormKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if (Key = VK_ESCAPE) then
- Self.Close;
- end;
- end.
- unit MainForm;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SimpleFormUnit, Vcl.Menus, Vcl.Grids,
- Vcl.StdCtrls, Vcl.Buttons, Vcl.Samples.Spin, Vcl.ValEdit, MatrixForm, System.UITypes;
- type
- TForm7_2 = class(TSimpleForm)
- VertexCountEdit: TSpinEdit;
- ResultButton: TBitBtn;
- IncedenceStringGrid: TStringGrid;
- FileMenu: TMenuItem;
- SaveFile: TMenuItem;
- OpenFile: TMenuItem;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- InputLabel: TLabel;
- procedure ResultButtonClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure VertexCountEditChange(Sender: TObject);
- procedure IncedenceStringGridSetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- procedure AnalyseStr(const Value: String; Row: Integer);
- function IsValidStr(const Value: string): Boolean;
- procedure FormKeyPress(Sender: TObject; var Key: Char);
- procedure SaveFileClick(Sender: TObject);
- procedure OpenFileClick(Sender: TObject);
- procedure SetInstructions(); override;
- procedure AnalyseList();
- end;
- Const
- ENTER_CODE = 13;
- var
- Form7_2: TForm7_2;
- IncidenceList: PIncidenceList;
- implementation
- {$R *.dfm}
- function TForm7_2.IsValidStr(const Value: string): Boolean;
- Const
- VALID_ARRAY: array [1..11] of String[1] = ('0','1', '2', '3', '4', '5', '6', '7', '8', '9', ' ');
- MIN_V = 1;
- function IsNumbCorrect(const Buff: String): Boolean;
- Var
- N, MaxV: Integer;
- Begin
- MaxV := Self.IncedenceStringGrid.RowCount-1;
- Result := True;
- N := StrToInt(Buff);
- if (N > MaxV) Or (N < MIN_V) then
- Result := False;
- End;
- Var
- I, J: Integer;
- Find: Boolean;
- Buffer: String[2];
- Begin
- Result := True;
- I := 1;
- Buffer := '';
- while (I <= Length(Value)) and (Result) do
- begin
- Find := False;
- J := 1;
- While(J <= Length(VALID_ARRAY)) And (not Find) Do
- begin
- if Value[I] = VALID_ARRAY[J] then
- Find := True;
- Inc(J);
- end;
- if Value[I] <> ' ' then
- Buffer := Value[I]+Buffer
- else
- begin
- Find := IsNumbCorrect(Buffer);
- Buffer := '';
- end;
- if not Find then
- Result := Find;
- Inc(I);
- end;
- if buffer <> '' then
- Result := IsNumbCorrect(Buffer);
- End;
- procedure TForm7_2.OpenFileClick(Sender: TObject);
- Var
- InputFile: TextFile;
- begin
- if OpenDialog.Execute then
- Begin
- If FileExists(OpenDialog.FileName) then
- Begin
- if TrySetInputFile(InputFile, OpenDialog.FileName) then
- Begin
- OpenFromFile(InputFile, Self.IncedenceStringGrid, VertexCountEdit);
- CloseFile(InputFile);
- End
- Else
- MessageDlg('File can not be openned.', mtError, [mbOk], 0);
- End;
- End;
- end;
- procedure TForm7_2.SaveFileClick(Sender: TObject);
- Var
- OutputFile: TextFile;
- begin
- if SaveDialog.Execute then
- Begin
- If FileExists(SaveDialog.FileName) then
- Begin
- if TrySetOutputFile(OutputFile, SaveDialog.FileName) then
- Begin
- SaveInFile(OutputFile, Self.IncedenceStringGrid);
- CloseFile(OutputFile);
- MessageDlg('Successfully saved', mtInformation, [mbOk], 0);
- End
- Else
- MessageDlg('Something was wrong', mtError, [mbOk], 0);
- End;
- End;
- end;
- procedure TForm7_2.SetInstructions;
- Const
- NEW_LINE = #13#10;
- begin
- Inherited;
- Instructions := 'This program transfers incidence list into incident matrix.' + NEW_LINE
- + 'Input number of vertices. Then input number of incedent vertices in the 2nd col.' + NEW_LINE
- +'CTRL+S - save matrix in file.' + NEW_LINE + 'CTRL+O - take lists from file.' + NEW_LINE
- +'To end input and show the result press ENTER and "Show result".';
- end;
- procedure TForm7_2.AnalyseList;
- Var
- I: Integer;
- begin
- if IncidenceList <> Nil then
- ClearList(IncidenceList);
- for I := 1 to IncedenceStringGrid.RowCount - 1 do
- Begin
- if isValidStr(IncedenceStringGrid.Cells[1, I]) then
- AnalyseStr(IncedenceStringGrid.Cells[1, I], I);
- End;
- end;
- procedure TForm7_2.AnalyseStr(const Value: String; Row: INteger);
- Var
- Reader: String[2];
- I, N, ErrorCode: Integer;
- procedure AddNumberToList();
- Begin
- Val(Reader, N, ErrorCode);
- if ErrorCode = 0 then
- AddToList(IncidenceList, Row, N);
- End;
- begin
- Reader := '';
- For I := 1 to Length(Value) do
- Begin
- If Value[I] <> ' ' then
- Reader := Reader+Value[I]
- Else
- Begin
- AddNumberToList;
- Reader := '';
- End;
- End;
- AddNumberToList;
- end;
- procedure TForm7_2.ResultButtonClick(Sender: TObject);
- begin
- AnalyseList;
- if not Assigned(ResultForm) then
- ResultForm := TResultForm.Create(Self);
- ResultForm.TakeSize(IncedenceStringGrid.RowCount - 1);
- ResultForm.TakeIncedenceList(IncidenceList);
- ResultForm.Show;
- end;
- procedure TForm7_2.VertexCountEditChange(Sender: TObject);
- Const
- WINDOW_DEFAULT_HEIGHT = 150;
- begin
- SetGridSize(1, StrToint(VertexCountEdit.Text), IncedenceStringGrid, 48);
- if IncedenceStringGrid.height > ClientHeight-WINDOW_DEFAULT_HEIGHT then
- ClientHeight := IncedenceStringGrid.height+WINDOW_DEFAULT_HEIGHT;
- end;
- procedure TForm7_2.FormCreate(Sender: TObject);
- begin
- inherited;
- IncedenceStringGrid.Cells[0,0] := 'V';
- IncedenceStringGrid.Cells[1,0] := '1';
- IncedenceStringGrid.Cells[0,1] := '1';
- end;
- procedure TForm7_2.FormKeyPress(Sender: TObject; var Key: Char);
- begin
- inherited;
- if Key = Chr(ENTER_CODE) then
- Begin
- ResultButtonClick(Sender);
- End;
- end;
- procedure TForm7_2.IncedenceStringGridSetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- begin
- if not isValidStr(Value) then
- Begin
- MessageDlg('Wrong data.', mtError, [mbOk], 0);
- (Sender as TStringGrid).Cells[ACol, ARow] := '';
- End;
- end;
- end.
- unit MatrixForm;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SimpleFormUnit, Vcl.Menus, Vcl.Grids, Vcl.Samples.Spin, System.UITypes,
- Vcl.StdCtrls;
- type
- PVertex = ^TVertex;
- TVertex = record
- Number: Integer;
- Next: PVertex;
- end;
- PIncidenceList = ^TIncidenceList;
- TIncidenceList = Record
- Vertex: Integer;
- Incedent: PVertex;
- Next: PIncidenceList;
- End;
- TMatrix = array of array of integer;
- TResultForm = class(TSimpleForm)
- AdjacencyGrid: TStringGrid;
- Save: TMenuItem;
- SaveDialog: TSaveDialog;
- ResultLabel: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure TakeSize(N: Integer);
- procedure TakeIncedenceList(IncidenceListPointer: PIncidenceList);
- procedure FullFillGrid(Var Grid: TStringGrid; IncidenceList: PIncidenceList);
- procedure FormShow(Sender: TObject);
- procedure SaveClick(Sender: TObject);
- function receiveIncidentMatrix(var AdjacencyMatrix: TMatrix): TMatrix;
- function receiveAdjacencyMatrix(var IncidenceList: PIncidenceList): TMatrix;
- Private
- GridSize: Integer;
- IncedenceList: PIncidenceList;
- end;
- procedure SetGridSize(const Width, Height: Integer; var StringGrid: TStringGrid; const Offset: integer);
- procedure AddtoList(var List: PIncidenceList; V: Integer; N: Integer);
- Function TrySetOutputFile(var OutputFile: TextFile; const Name: String): Boolean;
- Procedure SaveInFile(var SaveFile: TextFile; const Grid: TStringGrid);
- Function TrySetInputFile(var InputFile: TextFile; const Name: String): Boolean;
- Procedure OpenFromFile(var InputFile: TextFile; var Grid: TStringGrid; var VertexCountEdit: TSpinEdit);
- Procedure ClearList(var IncidenceList: PIncidenceList);
- var
- ResultForm: TResultForm;
- implementation
- {$R *.dfm}
- Procedure ClearList(var IncidenceList: PIncidenceList);
- Var
- VertexDeleter: PVertex;
- ListDeleter : PIncidenceList;
- Begin
- While(IncidenceList <> Nil) do
- Begin
- while IncidenceList^.Incedent <> Nil do
- Begin
- VertexDeleter := IncidenceList^.Incedent;
- IncidenceList^.Incedent := IncidenceList^.Incedent^.Next;
- Dispose(VertexDeleter);
- End;
- ListDeleter := IncidenceList;
- IncidenceList := IncidenceList^.Next;
- Dispose(ListDeleter);
- End;
- End;
- function TrySetOutputFile(var OutputFile: TextFile; const Name: String): Boolean;
- Begin
- Result := True;
- AssignFile(OutputFile, Name);
- Try
- Rewrite(OutputFile);
- Except
- Result := False;
- End;
- End;
- Function TrySetInputFile(var InputFile: TextFile; const Name: String): Boolean;
- Begin
- Result := True;
- AssignFile(InputFile, Name);
- Try
- Reset(InputFile);
- Except
- Result := False;
- End;
- End;
- procedure OpenFromFile(var InputFile: TextFile; var Grid: TStringGrid; var VertexCountEdit: TSpinEdit);
- Var
- i, j, Width: Integer;
- Str: String;
- Buffer: Char;
- Begin
- Readln(InputFile, Width);
- VertexCountEdit.Text := IntToStr(Width);
- for i := 1 to Grid.RowCount - 1 do
- Begin
- for j := 0 to 1 do
- Begin
- Buffer := 'f';
- Str := '';
- While (Buffer <> ' ') And (j = 0) do
- Begin
- Read(InputFile, Buffer);
- Str := Str + buffer;
- End;
- If (j = 1) Then
- Begin
- Read(InputFile, Str);
- End;
- Grid.Cells[j, i] := Str;
- End;
- Readln(InputFile);
- End;
- End;
- procedure SaveInFile(var SaveFile: TextFile; const Grid: TStringGrid);
- const
- LETTER = 97;
- Var
- i, j: Integer;
- Begin
- write(SaveFile, ' ');
- for i := 0 to Grid.RowCount do
- write(SaveFile, char(LETTER + i):2, ' ');
- writeln(SaveFile);
- for i := 1 to Grid.RowCount - 1 do
- Begin
- for j := 0 to Grid.ColCount - 1 do
- Begin
- Write(SaveFile, Grid.Cells[j, i]:2);
- Write(SaveFile, ' ');
- End;
- Writeln(SaveFile);
- End;
- End;
- procedure TResultForm.FormCreate(Sender: TObject);
- begin
- inherited;
- AdjacencyGrid.Cells[0,0] := 'V';
- AdjacencyGrid.Cells[1,0] := '1';
- AdjacencyGrid.Cells[0,1] := '1';
- end;
- procedure TResultForm.TakeSize(N: Integer);
- Begin
- GridSize := N;
- End;
- procedure TResultForm.TakeIncedenceList(IncidenceListPointer: PIncidenceList);
- Begin
- IncedenceList := IncidenceListPointer;
- End;
- procedure SetGridSize(const Width, Height: Integer; var StringGrid: TStringGrid; const OFFSET: integer);
- Var
- I: Integer;
- begin
- StringGrid.ColCount := Width + 1;
- StringGrid.RowCount := Height + 1;
- for I := 1 to Height do
- StringGrid.Cells[0, I] := IntToStr(I);
- for I := 1 to Width do
- StringGrid.Cells[I, 0] := char(i + OFFSET);
- StringGrid.Width := (Width + 1) * StringGrid.DefaultColWidth + 15;
- StringGrid.Height := (Height + 1) * StringGrid.DefaultRowHeight + 15;
- end;
- procedure AddtoList(var List: PIncidenceList; V: Integer; N: Integer);
- Var
- isFind: Boolean;
- Save, Temp: PIncidenceList;
- InTemp: PVertex;
- Begin
- isFind := False;
- if List = Nil then
- Begin
- New(List);
- List^.Vertex := V;
- New(List^.Incedent);
- List^.Incedent^.Number := N;
- List^.Next := Nil;
- List^.Incedent^.Next := Nil;
- isFind := True;
- End;
- Temp := List;
- while (not isFind) And (Temp <> Nil) do
- Begin
- if Temp^.Vertex = V then
- Begin
- InTemp := Temp^.Incedent;
- While(InTemp^.Next <> Nil) do
- InTemp := InTemp^.Next;
- isFind := True;
- New(InTemp^.Next);
- InTemp := InTemp^.Next;
- InTemp^.Number := N;
- InTemp^.Next := Nil;
- End
- Else
- Begin
- if Temp^.Next = Nil then
- Save := Temp;
- Temp := Temp^.Next;
- End;
- End;
- if not isFind then
- Begin
- New(Save^.Next);
- Save := Save^.Next;
- Save^.Vertex := V;
- New(Save^.Incedent);
- Save^.Incedent^.Number := N;
- Save.Next := Nil;
- Save^.Incedent^.Next := Nil;
- End;
- End;
- procedure resizeIncidentMatrix(var IncidentMatrix: TMatrix; Col: integer);
- var
- i: integer;
- begin
- for i := 0 to Col do
- setlength(IncidentMatrix[i], Col);
- end;
- procedure TResultForm.FormShow(Sender: TObject);
- begin
- inherited;
- FullFillGrid(AdjacencyGrid, IncedenceList);
- end;
- procedure TResultForm.FullFillGrid(var Grid: TStringGrid; IncidenceList: PIncidenceList);
- procedure fillGridWithZero();
- var
- I, J: Integer;
- Begin
- for I := 1 to Grid.RowCount - 1 do
- for J := 1 to Grid.ColCount - 1 do
- Grid.Cells[J, I] := '0';
- End;
- var
- IncidentMatrix, AdjacencyMatrix: TMatrix;
- i, j, col, row: integer;
- begin
- fillGridWithZero;
- AdjacencyMatrix := receiveAdjacencyMatrix(IncidenceList);
- IncidentMatrix := receiveIncidentMatrix(AdjacencyMatrix);
- SetGridSize(length(IncidentMatrix[0]), length(IncidentMatrix), Grid, 96);
- fillGridWithZero();
- if AdjacencyGrid.height > ClientHeight then
- ClientHeight := AdjacencyGrid.height + 30;
- if AdjacencyGrid.Width > ClientWidth then
- ClientWidth := AdjacencyGrid.Width + 30;
- col := length(IncidentMatrix[0]);
- row := length(IncidentMatrix);
- for i := 1 to length(IncidentMatrix) do
- for j := 1 to length(IncidentMatrix[0]) do
- Grid.cells[j,i] := intToStr(IncidentMatrix[i-1][j-1]);
- end;
- function TResultForm.receiveIncidentMatrix(var AdjacencyMatrix: TMatrix): TMatrix;
- const
- MAX_EDGE_COUNT = 25;
- var
- Col, Row, i, j: integer;
- IncidentMatrix: TMatrix;
- begin
- Col := 0;
- Row := 0;
- setlength(IncidentMatrix, GridSize, MAX_EDGE_COUNT);
- for i := 0 to high(AdjacencyMatrix) do
- begin
- for j := 0 to high(AdjacencyMatrix) do
- begin;
- if AdjacencyMatrix[i][j] = 1 then
- begin
- if i = j then
- IncidentMatrix[j][Col] := 2
- else
- begin
- IncidentMatrix[j][Col] := 1;
- if (AdjacencyMatrix[j][i] = 1) then
- begin
- IncidentMatrix[i][Col] := 1;
- AdjacencyMatrix[j][i] := 0;
- end
- else
- begin
- IncidentMatrix[i][Col] := -1;
- AdjacencyMatrix[j][i] := 0;
- end;
- end;
- inc(Col);
- end;
- end;
- end;
- resizeIncidentMatrix(IncidentMatrix, Col);
- Result := IncidentMatrix;
- end;
- function TResultForm.receiveAdjacencyMatrix(var IncidenceList: PIncidenceList): TMatrix;
- var
- Temp: PIncidenceList;
- AdjacencyMatrix: TMatrix;
- begin
- setlength(AdjacencyMatrix, GridSize, GridSize);
- Temp := IncidenceList;
- while Temp <> Nil do
- Begin
- while Temp^.Incedent <> Nil do
- Begin
- AdjacencyMatrix[Temp^.Vertex - 1][Temp^.Incedent^.Number - 1] := 1;
- Temp^.Incedent := Temp^.Incedent^.Next;
- End;
- Temp := Temp^.Next;
- End;
- Result := AdjacencyMatrix;
- end;
- procedure TResultForm.SaveClick(Sender: TObject);
- Var
- OutputFile: TextFile;
- begin
- if SaveDialog.Execute then
- Begin
- If FileExists(SaveDialog.FileName) then
- Begin
- if TrySetOutputFile(OutputFile, SaveDialog.FileName) then
- Begin
- SaveInFile(OutputFile, Self.AdjacencyGrid);
- CloseFile(OutputFile);
- MessageDlg('Data is saved.', mtInformation, [mbOk], 0);
- End
- Else
- MessageDlg('Error. Data is not saved.', mtError, [mbOk], 0);
- End;
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement