Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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, ResForm, System.UITypes;
- type
- TForm7_2 = class(TSimpleForm)
- edtVCount: TSpinEdit;
- btnDone: TBitBtn;
- grdIncedence: TStringGrid;
- FileMenu: TMenuItem;
- SaveFile: TMenuItem;
- OpenFile: TMenuItem;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- procedure btnDoneClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure edtVCountChange(Sender: TObject);
- procedure grdIncedenceSetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- procedure AnalyseStr(const Value: String; Row: Integer);
- function IsItValidStr(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;
- IncList: PList;
- implementation
- {$R *.dfm}
- function TForm7_2.IsItValidStr(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.grdIncedence.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
- OFile: TextFile;
- begin
- if OpenDialog.Execute then
- Begin
- If FileExists(OpenDialog.FileName) then
- Begin
- if TrySetInputFile(OFile, OpenDialog.FileName) then
- Begin
- OpenFromFile(OFile, Self.grdIncedence, edtVCount);
- CloseFile(OFile);
- End
- Else
- MessageDlg('Something was wrong', mtError, [mbOk], 0);
- End;
- End;
- end;
- procedure TForm7_2.SaveFileClick(Sender: TObject);
- Var
- SFile: TextFile;
- begin
- if SaveDialog.Execute then
- Begin
- If FileExists(SaveDialog.FileName) then
- Begin
- if TrySetOutputFile(SFile, SaveDialog.FileName) then
- Begin
- SaveInFile(SFile, Self.grdIncedence);
- CloseFile(SFile);
- 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 transfer incedence list into adjacncy matrix.'+NEW_LINE
- + 'In first column of grid is number of veryex. In second - incedent vertex.'+NEW_LINE
- +'Locate incedence vertex after spaces. To save list press CTRL+S. To open - CTRL+O.'+NEW_LINE
- +'To end input in list press ENTER, than press Show result.';
- end;
- procedure TForm7_2.AnalyseList;
- Var
- I: Integer;
- begin
- if IncList <> Nil then
- ClearList(IncList);
- for I := 1 to grdIncedence.RowCount - 1 do
- Begin
- if IsItValidStr(grdIncedence.Cells[1, I]) then
- AnalyseStr(grdIncedence.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(IncList, 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.btnDoneClick(Sender: TObject);
- begin
- AnalyseList;
- if not Assigned(ResultForm) then
- ResultForm := TResultForm.Create(Self);
- ResultForm.TakeSize(grdIncedence.RowCount-1);
- ResultForm.TakeIncedenceList(IncList);
- ResultForm.Show;
- end;
- procedure TForm7_2.edtVCountChange(Sender: TObject);
- Const
- WINDOW_DEFAULT_HEIGHT = 150;
- begin
- SetGridSize(1, StrToint(edtVCount.Text), grdIncedence);
- if grdIncedence.height > ClientHeight-WINDOW_DEFAULT_HEIGHT then
- ClientHeight := grdIncedence.height+WINDOW_DEFAULT_HEIGHT;
- end;
- procedure TForm7_2.FormCreate(Sender: TObject);
- begin
- inherited;
- grdIncedence.Cells[0,0] := 'V';
- grdIncedence.Cells[1,0] := '1';
- grdIncedence.Cells[0,1] := '1';
- end;
- procedure TForm7_2.FormKeyPress(Sender: TObject; var Key: Char);
- begin
- inherited;
- if Key = Chr(ENTER_CODE) then
- Begin
- btnDoneClick(Sender);
- End;
- end;
- procedure TForm7_2.grdIncedenceSetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- begin
- if not IsItValidStr(Value) then
- Begin
- MessageDlg('Wrong content!', mtError, [mbOk], 0);
- (Sender as TStringGrid).Cells[ACol, ARow] := '';
- End;
- end;
- end.
- unit ResForm;
- 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;
- type
- PVirtex = ^TVirtex;
- TVirtex = record
- El: Integer;
- Next: PVirtex;
- end;
- PList = ^TList;
- TList = Record
- Virtex: Integer;
- Incedent: PVirtex;
- Next: PList;
- End;
- TResultForm = class(TSimpleForm)
- AdjacencyGrid: TStringGrid;
- Save: TMenuItem;
- SaveDialog: TSaveDialog;
- procedure FormCreate(Sender: TObject);
- procedure TakeSize(N: Integer);
- procedure TakeIncedenceList(P: PList);
- procedure FullFillGrid(Var Grid: TStringGrid; IncList: PList);
- procedure FormShow(Sender: TObject);
- procedure SaveClick(Sender: TObject);
- Private
- GridSize: Integer;
- IncedenceList: PList;
- end;
- procedure SetGridSize(const Width, Height: Integer; var Grid: TStringGrid);
- procedure AddtoList(var List: PList; V: Integer; N: Integer);
- Function TrySetOutputFile(var SF: TextFile;const Name: String): Boolean;
- Procedure SaveInFile(var SF: TextFile;const Grid: TStringGrid);
- Function TrySetInputFile(var IFile: TextFile; const Name: String): Boolean;
- Procedure OpenFromFile(var IFile: TextFile; var Grid: TStringGrid; var Edt: TSpinEdit);
- Procedure ClearList(Var List: PList);
- var
- ResultForm: TResultForm;
- implementation
- {$R *.dfm}
- Procedure ClearList(var List: PList);
- Var
- DelV: PVirtex;
- Del : PList;
- Begin
- While(List <> Nil) do
- Begin
- while List^.Incedent <> Nil do
- Begin
- DelV := List^.Incedent;
- List^.Incedent := List^.Incedent^.Next;
- Dispose(DelV);
- End;
- Del := List;
- List := List^.Next;
- Dispose(Del);
- End;
- End;
- function TrySetOutputFile(var SF: TextFile; const Name: String): Boolean;
- Begin
- Result := True;
- AssignFile(SF, Name);
- Try
- Rewrite(SF);
- Except
- Result := False;
- End;
- End;
- Function TrySetInputFile(var IFile: TextFile; const Name: String): Boolean;
- Begin
- Result := True;
- AssignFile(IFile, Name);
- Try
- Reset(IFile);
- Except
- Result := False;
- End;
- End;
- procedure OpenFromFile(var IFile: TextFile; var Grid: TStringGrid; var Edt: TSpinEdit);
- Var
- I, J, Width: Integer;
- Str: String;
- Buffer: Char;
- Begin
- Readln(IFile, Width);
- Edt.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(IFile, Buffer);
- Str := Str + buffer;
- End;
- If (J = 1) Then
- Begin
- Read(IFile, Str);
- End;
- Grid.Cells[J, I] := Str;
- End;
- Readln(IFile);
- End;
- End;
- procedure SaveInFile(var SF: TextFile; const Grid: TStringGrid);
- Var
- I, J: Integer;
- Begin
- Writeln(SF, Grid.RowCount-1);
- for I := 1 to Grid.RowCount-1 do
- Begin
- for J := 0 to Grid.ColCount-1 do
- Begin
- Write(SF, Grid.Cells[J, I]);
- Write(SF, ' ');
- End;
- Writeln(SF);
- 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(P: PList);
- Begin
- IncedenceList := P;
- End;
- procedure SetGridSize(const Width, Height: Integer; var Grid: TStringGrid);
- Var
- I: Integer;
- begin
- Grid.ColCount := Width+1;
- Grid.RowCount := Height+1;
- for I := 1 to Height do
- Begin
- Grid.Cells[I, 0] := IntToStr(I);
- Grid.Cells[0, I] := IntToStr(I);
- End;
- Grid.Width := (width+1)*Grid.DefaultColWidth + 15;
- Grid.Height := (height+1)*Grid.DefaultRowHeight + 15;
- end;
- procedure AddtoList(var List: PList; V: Integer; N: Integer);
- Var
- Find: Boolean;
- Save, Temp: PList;
- InTemp: PVirtex;
- Begin
- Find := False;
- if List = Nil then
- Begin
- New(List);
- List^.Virtex := V;
- New(List^.Incedent);
- List^.Incedent^.El := N;
- List^.Next := Nil;
- List^.Incedent^.Next := Nil;
- Find := True;
- End;
- Temp := List;
- while (not Find) And (Temp <> Nil) do
- Begin
- if Temp^.Virtex = V then
- Begin
- InTemp := Temp^.Incedent;
- While(InTemp^.Next <> Nil) do
- InTemp := InTemp^.Next;
- Find := True;
- New(InTemp^.Next);
- InTemp := InTemp^.Next;
- InTemp^.El := N;
- InTemp^.Next := Nil;
- End
- Else
- Begin
- if Temp^.Next = Nil then
- Save := Temp;
- Temp := Temp^.Next;
- End;
- End;
- if not Find then
- Begin
- New(Save^.Next);
- Save := Save^.Next;
- Save^.Virtex := V;
- New(Save^.Incedent);
- Save^.Incedent^.El := N;
- Save.Next := Nil;
- Save^.Incedent^.Next := Nil;
- End;
- End;
- procedure TResultForm.FormShow(Sender: TObject);
- begin
- inherited;
- SetGridSize(GridSize, GridSize, AdjacencyGrid);
- if AdjacencyGrid.height > ClientHeight then
- ClientHeight := AdjacencyGrid.height + 30;
- if AdjacencyGrid.Width > ClientWidth then
- ClientWidth := AdjacencyGrid.Width + 30;
- FullFillGrid(AdjacencyGrid, IncedenceList);
- end;
- procedure TResultForm.FullFillGrid(var Grid: TStringGrid; IncList: PList);
- procedure InitGridWithZero();
- 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
- Temp :PList;
- begin
- Temp := IncList;
- InitGridWithZero;
- while Temp <> Nil do
- Begin
- while Temp^.Incedent <> Nil do
- Begin
- Grid.Cells[Temp^.Virtex, Temp^.Incedent^.El] := '1';
- Temp^.Incedent := Temp^.Incedent^.Next;
- End;
- Temp := Temp^.Next;
- End;
- end;
- procedure TResultForm.SaveClick(Sender: TObject);
- Var
- SFile: TextFile;
- begin
- if SaveDialog.Execute then
- Begin
- If FileExists(SaveDialog.FileName) then
- Begin
- if TrySetOutputFile(SFile, SaveDialog.FileName) then
- Begin
- SaveInFile(SFile, Self.AdjacencyGrid);
- CloseFile(SFile);
- MessageDlg('Successfully saved', mtInformation, [mbOk], 0);
- End
- Else
- MessageDlg('Something was wrong', mtError, [mbOk], 0);
- End;
- End;
- end;
- end.
- 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('Yegor Rusakovich, 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 := 'Press F1 to see this message.' + #13#10 + 'Press F3 to estimate the authors name.' + #13#10 + 'Press escape to 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement