Помогите пожалуйста сделать лабу на классы. Исходный код есть, нужно описать граф классом. (Находит минимальный путь между 2мя точками)
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Label3: TLabel;
Edit2: TEdit;
Button1: TButton;
ListBox1: TListBox;
Edit3: TEdit;
Label4: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure FillMatrix;
var
I, J : integer;
begin
randomize;
for I:=1 to Form1.StringGrid1.ColCount - 1 do
begin
Form1.StringGrid1.Cells[I,0] := IntToStr(I); //Заполняем номера точек
Form1.StringGrid1.Cells[0,I] := IntToStr(I);
for J := 1 to Form1.StringGrid1.ColCount - 1 do
begin
Form1.StringGrid1.Cells[I,J] := IntToStr(random(10));
Form1.StringGrid1.Cells[J,I] := Form1.StringGrid1.Cells[I,J];
end;
Form1.StringGrid1.Cells[I,I] := IntToStr(0);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const colMax = 100; //количество вершин в графе
var
a:array[1..colMax,1..colMax] of Integer;//матрица смежности
b:array[1..colMax]of boolean;//список просмотренных вершин
d:array[1..colMax] of Integer;//кратчайшие расстояния
col, tochka, tochka2, i, j, dist, temp: integer;
begin
col := StrToInt(Edit3.text);
//Ввод данных
tochka := StrToIntDef(Edit1.Text, 1); //начальная вершина
tochka2 := StrToIntDef(Edit2.Text, 1); //конечная вершина
if (tochka < 1) or (tochka > col) then tochka := 1;
if (tochka2 < 1) or (tochka2 > col) then tochka2 := 1;
for i := 1 to col do
for j := 1 to col do
a[j, i] := StrToInt(StringGrid1.Cells[i, j]);
//Расчет
fillchar(b,sizeof(b),0);
fillchar(d,sizeof(d), 10000);
d[tochka] := 0;//расстояние до начальной вершины
for i:=1 to col do
begin
dist := 1000;
for j := 1 to col do
if ( (d[j] <= dist) and (not b[j]) ) then
begin
dist := d[j];
temp := j;
end;
b[temp] := true;
for j := 1 to col do
if (not b[j]) and (d[temp]+a[temp,j]<d[j]) then
d[j] := d[temp] + a[temp,j];
end;
//Вывод результата
ListBox1.Clear;
ListBox1.Items.Append(IntToStr(tochka) + ' -> ' + IntToStr(tochka2) + ': '
+ IntToStr(d[tochka2]));
end;
procedure TForm1.Button2Click(Sender: TObject);
var i, j, col : Integer;
begin
if Edit3.Text = '' then
begin
StringGrid1.Visible := False;
Button1.Enabled := False;
end
else
begin
col := StrToIntDef(Edit3.Text,2);
StringGrid1.ColCount := col + 1;
StringGrid1.RowCount := col + 1;
For i := 0 to col do
StringGrid1.Cells[i,0] := IntToStr(i);
For j := 0 to col do
StringGrid1.Cells[0,j] := IntToStr(j);
StringGrid1.Visible := True;
Button1.Enabled := True;
for i := 0 to col do
for j := 0 to col do
StringGrid1.Cells[i,j] := '';
FillMatrix;
end;
end;
end.