Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Main;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus, Vcl.Grids, Vector;
- type
- TMatrix = Array of Array of Integer;
- TMyArray = class(TArray<Integer>)
- end;
- TVector = class(TArray<TMyArray>)
- end;
- TVectorCopy = Array of TMyArray;
- TMainForm = class(TForm)
- sgMatrix: TStringGrid;
- Task: TLabel;
- MainMenu: TMainMenu;
- About: TMenuItem;
- FileMenu: TMenuItem;
- OpenFromFileMenu: TMenuItem;
- SaveToFileMenu: TMenuItem;
- OpenFromFile: TOpenDialog;
- SaveToFile: TSaveDialog;
- Proces: TButton;
- NumOfNodesLabel: TLabel;
- NumOfColsLabel: TLabel;
- NumOfRowsEdit: TEdit;
- NumOfColsEdit: TEdit;
- IncidentListMemo: TMemo;
- procedure sgMatrixKeyPress(Sender: TObject; var Key: Char);
- procedure NumOfRowsEditKeyPress(Sender: TObject; var Key: Char);
- procedure NumOfColsEditKeyPress(Sender: TObject; var Key: Char);
- procedure NumOfRowsEditChange(Sender: TObject);
- procedure NumOfColsEditChange(Sender: TObject);
- procedure OpenFromFileMenuClick(Sender: TObject);
- procedure SaveToFileMenuClick(Sender: TObject);
- procedure ProcesClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure PrintIncidentList(IncidentList: TVector);
- function CheckMatrixValues(): Boolean;
- function CheckNumberOfLinkedNodes(): Boolean;
- function CheckOrientedEdges(): Boolean;
- function CheckLoopEdges(): Boolean;
- procedure FormCreate(Sender: TObject);
- procedure FileMenuClick(Sender: TObject);
- procedure AboutClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- IncidentMatrix: TMatrix;
- implementation
- {$R *.dfm}
- type
- TFileState = (fsSomething, fsEmpty, fsMissing);
- TArray = Array of Byte;
- procedure TMainForm.FileMenuClick(Sender: TObject);
- begin
- SaveToFileMenu.Enabled := not (IncidentListMemo.Lines.Count = 0);
- end;
- procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- var
- WND: HWND;
- lpCaption, lpText: PChar;
- Tip: Integer;
- begin
- WND := MainForm.Handle;
- lpCaption := 'Выход';
- lpText := 'Вы уверены, что хотите выйти?';
- Tip := MB_YESNO + MB_ICONINFORMATION + MB_DEFBUTTON2;
- case MessageBox(WND, lpText, lpCaption, Tip) of
- IDYES : CanClose := True;
- IDNO : CanClose := False;
- end;
- end;
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- IncidentListMemo.Clear();
- end;
- procedure TMainForm.NumOfColsEditChange(Sender: TObject);
- var
- Length: Integer;
- I: Byte;
- begin
- if (NumOfColsEdit.Text <> '') then
- begin
- if (NumOfRowsEdit.Text <> '') then
- sgMatrix.Enabled := True
- else
- sgMatrix.Enabled := False;
- sgMatrix.ColCount := StrToInt(NumOfColsEdit.Text) + 1;
- sgMatrix.Width := (sgMatrix.DefaultColWidth + 2) * sgMatrix.ColCount;
- Length := sgMatrix.Left + sgMatrix.Width;
- if (Length > MainForm.ClientWidth) then
- MainForm.ClientWidth := Length + 20;
- for I := sgMatrix.FixedCols to sgMatrix.ColCount - 1 do
- sgMatrix.Cells[i, 0] := 'r' + IntToStr(i);
- end;
- end;
- procedure TMainForm.NumOfColsEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if not (Key in ['1'..'9', #08]) then
- Key := #0;
- end;
- procedure TMainForm.NumOfRowsEditChange(Sender: TObject);
- var
- Height: Integer;
- I: Byte;
- begin
- if (NumOfRowsEdit.Text <> '') then
- begin
- if (NumOfColsEdit.Text <> '') then
- sgMatrix.Enabled := True
- else
- sgMatrix.Enabled := False;
- sgMatrix.RowCount := StrToInt(NumOfRowsEdit.Text) + 1;
- sgMatrix.Height := (sgMatrix.DefaultRowHeight + 2) * sgMatrix.RowCount;
- Height := sgMatrix.Top + sgMatrix.Height;
- if (Height > MainForm.ClientHeight) then
- MainForm.ClientHeight := Height + 20;
- for I := sgMatrix.FixedRows to sgMatrix.RowCount - 1 do
- sgMatrix.Cells[0, i] := IntToStr(i);
- end;
- end;
- function GetMatrixFromFile(FileName: string; var IsCorrect: Boolean): TMatrix;
- var
- InputFile: TextFile;
- Matrix: TMatrix;
- NumOfRows, NumOfCols, I, J: Byte;
- begin
- IsCorrect := True;
- try
- if FileExists(FileName) then
- begin
- Assign(InputFile, FileName);
- Reset(InputFile);
- end
- else
- begin
- IsCorrect := False;
- Application.MessageBox('Файл не найден!','Ошибка',MB_ICONERROR);
- end;
- except
- IsCorrect := False;
- Application.MessageBox('Ошибка доступа к файлу!','Ошибка',MB_ICONERROR);
- end;
- if (IsCorrect) then
- begin
- try
- Readln(InputFile, NumOfRows);
- except
- IsCorrect := False;
- end;
- if (NumOfRows < 1) or (NumOfRows > 9) then
- IsCorrect := False;
- end;
- if (IsCorrect) then
- begin
- try
- Readln(InputFile, NumOfCols);
- except
- IsCorrect := False;
- end;
- if (NumOfCols < 1) or (NumOfCols > 9) then
- IsCorrect := False;
- end;
- if (IsCorrect) then
- begin
- SetLength(Matrix, NumOfRows, NumOfCols);
- for I := 0 to High(Matrix) do
- for J := 0 to High(Matrix[0]) do
- begin
- try
- Read(InputFile, Matrix[I][J]);
- except
- IsCorrect := False;
- end;
- end;
- end;
- Close(InputFile);
- if (IsCorrect) then
- GetMatrixFromFile := Matrix;
- end;
- procedure TMainForm.NumOfRowsEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if not (Key in ['1'..'9', #08]) then
- Key := #0;
- end;
- function GetFileState(s : string) : TFileState;
- var
- sr : TSearchRec;
- err : integer;
- begin
- err := FindFirst(s, faAnyFile and not faDirectory, sr);
- if err <> 0
- then Result := fsMissing
- else if sr.Size = 0
- then Result := fsEmpty
- else Result := fsSomething;
- FindClose(sr);
- end;
- function IsFileCorrect(Path: String): Boolean;
- var
- FileToCheck: TextFile;
- Num: Integer;
- IsCorrect: Boolean;
- begin
- AssignFile(FileToCheck, Path);
- Reset(FileToCheck);
- IsCorrect := true;
- try
- Read(FileToCheck, Num);
- except
- IsCorrect := false;
- end;
- CloseFile(FileToCheck);
- IsFileCorrect := IsCorrect;
- end;
- procedure TMainForm.OpenFromFileMenuClick(Sender: TObject);
- var
- Matrix: TMatrix;
- IsCorrect: Boolean;
- NumOfCols, NumOfRows, I, J: Byte;
- begin
- if OpenFromFile.Execute() then
- if IsFileCorrect(OpenFromFile.FileName) then
- begin
- if (GetFileState(OpenFromFile.FileName) = fsEmpty) then
- Application.MessageBox('Файл пустой.', 'Ошибка!', MB_ICONERROR)
- else
- begin
- Matrix := GetMatrixFromFile(OpenFromFile.FileName, IsCorrect);
- if IsCorrect then
- begin
- NumOfRows := Length(Matrix);
- NumOfCols := Length(Matrix[0]);
- NumOfColsEdit.Text := IntToStr(NumOfCols);
- NumOfRowsEdit.Text := IntToStr(NumOfRows);
- for I := 0 to High(Matrix) do
- for J := 0 to High(Matrix[0]) do
- sgMatrix.Cells[J+1, I+1] := IntToStr(Matrix[I][J]);
- end
- else
- Application.MessageBox('Данные в файле некорректны.', 'Ошибка!', MB_ICONERROR);
- end;
- end
- else
- Application.MessageBox('Данные в файле некорректны.', 'Ошибка!', MB_ICONERROR);
- end;
- procedure FindIncidentList(var IncidentList: TVector; IncidentMatrix: TMatrix);
- var
- TempList: TVectorCopy;
- I, J: Byte;
- begin
- SetLength(TempList, length(IncidentMatrix));
- for I := 0 to High(TempList) do
- TempList[I] := TMyArray.Create();
- IncidentList := TVector.Create(length(IncidentMatrix));
- for I := 0 to High(IncidentMatrix) do
- begin
- for J := 0 to High(IncidentMatrix[0]) do
- if(IncidentMatrix[i][j] = 1) or (IncidentMatrix[i][j] = -1) or (IncidentMatrix[i][j] = 2) then
- TempList[i].Push_back(j + 1);
- IncidentList[i] := TempList[i];
- end;
- end;
- procedure TMainForm.PrintIncidentList(IncidentList: TVector);
- var
- I, J: Integer;
- TempString: String;
- begin
- IncidentListMemo.Clear();
- TempString := '';
- for I := 0 to IncidentList.Size() - 1 do
- begin
- TempString := TempString + IntToStr(i+1) + ':';
- for J := 0 to IncidentList[i].Size() - 1 do
- TempString := TempString + '->' + 'r' + IntToStr(IncidentList[i][j]);
- IncidentListMemo.Lines.Add(TempString);
- TempString := '';
- end;
- end;
- function TMainForm.CheckMatrixValues(): Boolean;
- var
- IsCorrect: Boolean;
- I, J: Byte;
- Temp: Integer;
- begin
- IsCorrect := True;
- with sgMatrix do
- begin
- for I := FixedRows to RowCount-1 do
- for J := FixedCols to ColCount-1 do
- begin
- try
- Temp := StrToInt(Cells[j, i]);
- except
- IsCorrect := False;
- end;
- if (Temp < -1) or (Temp > 2) then
- IsCorrect := False;
- end;
- end;
- CheckMatrixValues := IsCorrect;
- end;
- procedure TMainForm.AboutClick(Sender: TObject);
- const
- MESSAGE_ONE = 'Данная программа работает с любым типом графа.' + #13#10;
- MESSAGE_TWO = 'Следовательно, разрешается вводить -1, 0, 1 и 2 в матрице инциденций.' + #13#10;
- MESSAGE_THREE = 'Матрицу можно считать из файла, а список инциденций можно сохранить в файл.' + #13#10;
- begin
- Application.MessageBox(MESSAGE_ONE+MESSAGE_TWO+MESSAGE_THREE, 'Справка', MB_ICONINFORMATION);
- end;
- function TMainForm.CheckLoopEdges(): Boolean;
- var
- I, J, K: Byte;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- I := 1;
- J := 1;
- while((J < sgMatrix.ColCount) and (IsCorrect)) do
- begin
- while((I < sgMatrix.RowCount) and (IsCorrect)) do
- begin
- if (sgMatrix.Cells[j,i] = '2') then
- begin
- K := I + 1;
- while(K < sgMatrix.RowCount) do
- begin
- if (sgMatrix.Cells[j,k] <> '0') then
- IsCorrect := False;
- Inc(K);
- end;
- end;
- inc(I);
- end;
- if (IsCorrect) then
- begin
- inc(J);
- i := 0;
- end;
- end;
- CheckLoopEdges := IsCorrect;
- end;
- function TMainForm.CheckOrientedEdges(): Boolean;
- var
- IsCorrect: Boolean;
- I, J, K, Counter: Byte;
- begin
- IsCorrect := True;
- I := 1;
- J := 1;
- Counter := 0;
- while((J < sgMatrix.ColCount) and (IsCorrect)) do
- begin
- while(I < sgMatrix.RowCount) do
- begin
- if (sgMatrix.Cells[j,i] = '-1') then
- begin
- K := 1;
- while((K < sgMatrix.RowCount) and (IsCorrect)) do
- begin
- if (sgMatrix.Cells[j,k] = '1') then
- Inc(Counter);
- if ((K > I) and (sgMatrix.Cells[j,k] = '-1')) then
- IsCorrect := False;
- inc(K);
- end;
- if(Counter <> 1) then
- IsCorrect := False;
- end;
- Counter := 0;
- inc(I);
- end;
- if(IsCorrect) then
- begin
- inc(J);
- i := 0;
- end;
- end;
- CheckOrientedEdges := IsCorrect;
- end;
- function TMainForm.CheckNumberOfLinkedNodes(): Boolean;
- var
- IsCorrect: Boolean;
- I, J, Temp: Byte;
- begin
- IsCorrect := True;
- I := 1;
- J := 1;
- Temp := 0;
- while((J < sgMatrix.ColCount) and (IsCorrect)) do
- begin
- while(I < sgMatrix.RowCount) do
- begin
- if (sgMatrix.Cells[j,i] = '1') then
- Inc(Temp);
- if ((Temp = 2) and (sgMatrix.Cells[j,i] = '2')) then
- IsCorrect := False;
- inc(I);
- end;
- if (Temp > 2) then
- IsCorrect := False
- else
- begin
- inc(J);
- i := 0;
- Temp := 0;
- end;
- end;
- CheckNumberOfLinkedNodes := IsCorrect;
- end;
- procedure TMainForm.ProcesClick(Sender: TObject);
- var
- Matrix: TMatrix;
- NumOfCols, NumOfRows, I, J: Byte;
- IncidentList: TVector;
- begin
- if ((CheckMatrixValues()) and (CheckNumberOfLinkedNodes()) and (CheckOrientedEdges()) and (CheckLoopEdges())) then
- begin
- NumOfCols := sgMatrix.ColCount - 1;
- NumOfRows := sgMatrix.RowCount - 1;
- SetLength(Matrix, NumOfRows, NumOfCols);
- for I := sgMatrix.FixedRows to sgMatrix.RowCount-1 do
- for J := sgMatrix.FixedCols to sgMatrix.ColCount-1 do
- begin
- Matrix[i-1, j-1] := StrToInt(sgMatrix.Cells[j,i]);
- end;
- Application.MessageBox('Данные введены успешно!', 'Информация', MB_ICONINFORMATION);
- FindIncidentList(IncidentList, Matrix);
- PrintIncidentList(IncidentList);
- end
- else
- Application.MessageBox('Данные введены некорректно! (См. Справку)', 'Ошибка!', MB_ICONERROR);
- end;
- procedure TMainForm.SaveToFileMenuClick(Sender: TObject);
- var
- I, J: Byte;
- OutputFile: TextFile;
- begin
- if SaveToFile.Execute() then
- begin
- AssignFile(OutputFile, SaveToFile.FileName);
- Rewrite(OutputFile);
- Writeln(OutputFile, 'Входные данные:');
- Writeln(OutputFile, 'Количество вершин: ', NumOfRowsEdit.Text);
- Writeln(OutputFile, 'Количество рёбер: ', NumOfColsEdit.Text);
- Writeln(OutputFile, 'Матрица: ');
- with sgMatrix do
- begin
- for I := FixedRows to RowCount - 1 do
- begin
- for J := FixedCols to ColCount - 1 do
- Write(OutputFile, Cells[J, I], ' ');
- Writeln(OutputFile);
- end;
- end;
- Writeln(OutputFile, 'Список инциденций: ');
- Writeln(OutputFile, IncidentListMemo.Lines.Text);
- CloseFile(OutputFile);
- Application.MessageBox('Список инциденций успешно сохранен по указанному пути.', 'Сохранение', MB_ICONINFORMATION);
- end;
- end;
- procedure TMainForm.sgMatrixKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key <> '0') and (Key <> '1') and (Key <> '2') and (Key <> '-') and (Key <> #8) then
- Key := #0;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment