Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MainUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.Grids,
- System.StrUtils;
- type
- TPVertex = ^TVertex;
- TVertex = Record
- VertexNum: Integer;
- Next: TPVertex;
- End;
- TMatrix = Array Of Array Of Integer;
- TMainForm = class(TForm)
- InfoLabel: TLabel;
- EnterVertexAmountLabel: TLabel;
- EnterVertexAmountEdit: TEdit;
- MainMenu1: TMainMenu;
- N1: TMenuItem;
- OpenFile: TMenuItem;
- SaveMenu: TMenuItem;
- Instruction: TMenuItem;
- DevInfo: TMenuItem;
- ConvertButton: TButton;
- InputList: TStringGrid;
- OutputMemo: TMemo;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- procedure EnterVertexAmountEditChange(Sender: TObject);
- procedure EnterVertexAmountEditKeyPress(Sender: TObject; var Key: Char);
- procedure EnterVertexAmountEditKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure ConvertButtonClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure DevInfoClick(Sender: TObject);
- procedure InstructionClick(Sender: TObject);
- procedure OpenFileClick(Sender: TObject);
- procedure SaveMenuClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- {$R *.dfm}
- Procedure PrintMatrix(AdjacencyMatrix: TMatrix);
- Var
- I, J: Integer;
- Begin
- For I := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
- Begin
- For J := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
- MainForm.OutputMemo.Lines.Text := MainForm.OutputMemo.Lines.Text +
- IntToStr(AdjacencyMatrix[I, J]) + ' ';
- MainForm.OutputMemo.Lines.Text :=
- MainForm.OutputMemo.Lines.Text + #10#13;
- End;
- MainForm.SaveMenu.Enabled := True;
- End;
- Procedure SetMatrix(VertexArray: Array Of TPVertex);
- Var
- AdjacencyMatrix: TMatrix;
- I, J: Integer;
- Curr: TPVertex;
- Begin
- SetLength(AdjacencyMatrix, StrToInt(MainForm.EnterVertexAmountEdit.Text),
- StrToInt(MainForm.EnterVertexAmountEdit.Text));
- For I := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
- For J := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
- AdjacencyMatrix[I, J] := 0;
- For I := Low(AdjacencyMatrix) To High(AdjacencyMatrix) Do
- Begin
- Curr := VertexArray[I].Next;
- While Curr <> Nil Do
- Begin
- J := Curr.VertexNum - 1;
- AdjacencyMatrix[I, J] := 1;
- AdjacencyMatrix[J, I] := 1;
- Curr := Curr.Next;
- End;
- End;
- PrintMatrix(AdjacencyMatrix);
- End;
- Function CheckInput: Boolean;
- Var
- I, J: Integer;
- Begin
- For I := 0 To MainForm.InputList.RowCount - 1 Do
- For J := 1 To MainForm.InputList.ColCount - 1 Do
- If (MainForm.InputList.Cells[J, I] <> '') And
- (StrToInt(MainForm.InputList.Cells[J, I]) >
- MainForm.InputList.RowCount) And
- (StrToInt(MainForm.InputList.Cells[J, I]) = 0) Then
- Begin
- CheckInput := False;
- Exit;
- End;
- For I := 0 To MainForm.InputList.RowCount - 1 Do
- Begin
- If MainForm.InputList.Cells[1, I] = '' Then
- Begin
- CheckInput := False;
- Exit;
- End
- Else If MainForm.InputList.ColCount > 4 Then
- For J := 2 To MainForm.InputList.ColCount - 2 Do
- Begin
- If (MainForm.InputList.Cells[J - 1, I] = '') And
- (MainForm.InputList.Cells[J + 1, I] = '') And
- (MainForm.InputList.Cells[J, I] <> '') Then
- Begin
- CheckInput := False;
- Exit;
- End;
- End;
- End;
- CheckInput := True;
- End;
- procedure TMainForm.ConvertButtonClick(Sender: TObject);
- Var
- VertexArray: Array Of TPVertex;
- NewNode, Curr: TPVertex;
- I, J: Integer;
- Begin
- OutputMemo.Clear;
- If CheckInput Then
- Begin
- OutputMemo.Visible := True;
- SetLength(VertexArray, StrToInt(EnterVertexAmountEdit.Text));
- For I := 0 To InputList.RowCount - 1 Do
- Begin
- J := 1;
- New(VertexArray[I]);
- VertexArray[I].VertexNum := StrToInt(InputList.Cells[0, I]);
- VertexArray[I].Next := Nil;
- While InputList.Cells[J, I] <> '' Do
- Begin
- New(NewNode);
- NewNode.VertexNum := StrToInt(InputList.Cells[J, I]);
- NewNode.Next := Nil;
- If VertexArray[I].Next = Nil Then
- VertexArray[I].Next := NewNode
- Else
- Begin
- Curr := VertexArray[I];
- While Curr.Next <> Nil Do
- Curr := Curr.Next;
- Curr.Next := NewNode;
- End;
- Inc(J);
- End;
- End;
- SetMatrix(VertexArray);
- End
- Else
- Begin
- Application.MessageBox('Проверьте введенные данные!', 'Ошибка',
- MB_OK + MB_ICONERROR);
- End;
- End;
- procedure TMainForm.DevInfoClick(Sender: TObject);
- begin
- Application.MessageBox('Сымоник Вадим, группа 251004', 'Разработчик');
- end;
- procedure TMainForm.EnterVertexAmountEditChange(Sender: TObject);
- Var
- I, J: Integer;
- Begin
- OutputMemo.Visible := False;
- If ((Length(EnterVertexAmountEdit.Text) <> 0) and
- (Length(EnterVertexAmountEdit.Text) < 2)) Then
- Begin
- SaveMenu.Enabled := False;
- For I := 0 To InputList.RowCount - 1 Do
- For J := 0 To InputList.ColCount - 1 Do
- InputList.Cells[J, I] := '';
- OutputMemo.Clear;
- InputList.ColCount := StrToInt(EnterVertexAmountEdit.Text) + 1;
- InputList.RowCount := StrToInt(EnterVertexAmountEdit.Text);
- InputList.FixedCols := 1;
- For I := 0 To InputList.RowCount - 1 Do
- InputList.Cells[0, I] := IntToStr(I + 1);
- InputList.Visible := True;
- ConvertButton.Visible := True;
- End
- Else
- Begin
- InputList.Visible := False;
- ConvertButton.Visible := False;
- End;
- End;
- procedure TMainForm.EnterVertexAmountEditKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- If (SsShift In Shift) And (Key = VK_Insert) Then
- Abort;
- end;
- procedure TMainForm.EnterVertexAmountEditKeyPress(Sender: TObject;
- var Key: Char);
- begin
- If Key <> #08 Then
- Begin
- If Not(Key In ['1' .. '9', #9]) Then
- Begin
- MessageBox(Application.Handle,
- 'К вводу доступны только цифры от 1 до 9.', 'Внимание!', MB_OK);
- Key := #0;
- End
- Else
- Begin
- If Length(EnterVertexAmountEdit.Text) = 1 Then
- Begin
- MessageBox(Application.Handle,
- 'Нельзя ввести больше 1-го символа.', 'Внимание!', MB_OK);
- Key := #0;
- End;
- End;
- End;
- end;
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- InputList.Visible := False;
- OutputMemo.Visible := False;
- SaveMenu.Enabled := False;
- end;
- procedure TMainForm.InstructionClick(Sender: TObject);
- const
- Info1 = 'Ввод с клавиатуры:'#13#10;
- Info2 = 'Можно ввести количество вершин от 1 до 9'#13#10;
- Info3 = 'первая клетка должна быть заполнена обязательно, между клетками с цифрами не должно быть пустых клеток'#13#10;
- Info4 = 'значение в клетке должно быть не больше количества вершин'#13#10;
- Info5 = 'Ввод с файла: Первое число - количество вершин'#13#10;
- Info6 = 'Далее в каждой строке идут вершины, которые будут связаны с вершиной, соответствующей номеру строки'#13#10;
- Info7 = 'т.е первая строка вершин после количества вершин соответсвует первой вершине графа и т.д, при том номер самой строки указывать не надо'#13#10;
- begin
- Application.MessageBox(Info1 + Info2 + Info3 + Info4 + Info5 + Info6 +
- Info7, 'Справка', MB_OK + MB_ICONINFORMATION);
- end;
- procedure TMainForm.OpenFileClick(Sender: TObject);
- Var
- InputFile: TextFile;
- I, J, VertexAmount, Temp: Integer;
- TempArr: TArray<String>;
- Input: String;
- FileWasOpened: Boolean;
- Begin
- If OpenDialog1.Execute Then
- Begin
- AssignFile(InputFile, OpenDialog1.FileName);
- Try
- FileWasOpened := True;
- Try
- Reset(InputFile);
- Except
- On E: Exception Do
- Begin
- FileWasOpened := False;
- Application.MessageBox
- (PChar('Ошибка работы с файлом: ' + E.Message), 'Ошибка',
- MB_OK + MB_ICONERROR);
- End;
- End;
- If FileWasOpened Then
- Begin
- Try
- Readln(InputFile, VertexAmount);
- EnterVertexAmountEdit.Text := IntToStr(VertexAmount);
- Except
- On E: Exception Do
- Begin
- Application.MessageBox
- (PChar('Ошибка работы с файлом: ' + E.Message),
- 'Ошибка', MB_OK + MB_ICONERROR);
- End;
- End;
- For I := 0 To (VertexAmount - 1) Do
- Begin
- Readln(InputFile, Input);
- TempArr := SplitString(Input, ' ');
- For J := Low(TempArr) To High(TempArr) Do
- If TryStrToInt(TempArr[J], Temp) Then
- InputList.Cells[J + 1, I] := TempArr[J];
- End;
- Application.MessageBox('Чтение успешно произведено.',
- 'Чтение из файла', MB_OK + MB_ICONINFORMATION);
- End;
- Finally
- If FileWasOpened Then
- CloseFile(InputFile);
- End;
- End
- Else
- Application.MessageBox('Чтение из файла отменено!', 'Чтение из файла',
- MB_OK + MB_ICONINFORMATION);
- End;
- procedure TMainForm.SaveMenuClick(Sender: TObject);
- Begin
- If SaveDialog1.Execute Then
- Try
- OutputMemo.Lines.SaveToFile(SaveDialog1.FileName);
- Application.MessageBox('Результат успешно сохранен в файл!',
- 'Сохранение в файл', MB_OK + MB_ICONINFORMATION);
- Except
- On E: Exception Do
- Begin
- Application.MessageBox
- (PChar('Ошибка работы с файлом: ' + E.Message), 'Ошибка',
- MB_OK + MB_ICONERROR);
- End;
- End
- Else
- Application.MessageBox('Сохранение в файл отменено!', 'Внимание',
- MB_OK + MB_ICONINFORMATION);
- End;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement