📄 dijkstra.pas
字号:
unit Dijkstra;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
CheckBox1: TCheckBox;
Edit1: TEdit;
CheckBox2: TCheckBox;
Panel2: TPanel;
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn3: TBitBtn;
Label2: TLabel;
BitBtn2: TBitBtn;
Label3: TLabel;
Label4: TLabel;
procedure CheckBox1Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var XLmatrix,XLmatrix2,edit,edit2,xledit,xledit2:array of array of TEdit;
var T,k,N,hangshu,hangshu1,hangshu2,hangshu3,hangshu11,hangshu12,hangshu13:InTEger;
var C,D:array[1..99,1..99] of real; //声明的数组类型数据:二维数组
implementation
{$R *.dfm}
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
CheckBox2.Checked:=CheckBox1.Checked=False;
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
CheckBox1.Checked:=CheckBox2.Checked=False;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
N:=strtoint(Edit1.Text);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var i,j:inTEger;
begin
Label4.Caption:='行点号';
Label3.Caption:='列点号';
//创建节点名称输入编辑框
if edit<>nil then
for i:=0 to hangshu1-1 do
for j:=0 to 0 do
edit[i,j].Free;
hangshu1:=StrToInt(Edit1.TExt);
SETLength(edit,hangshu1);
for i:=low(edit) to high(edit) do
SETLength(edit[i],1);
for i:=low(edit) to high(edit) do
for j:=0 to 0 do
begin
edit[i,j]:=TEdit.CreaTE(SElf);
with edit[i,j] do
begin
Parent:=Panel2;
Width:=40;
Height:=25;
left:=40+j*35;
top:=95+i*30;
end;
end;
//创建相邻节点名称显示编辑框
if XLedit<>nil then
for i:=0 to 0 do
for j:=0 to hangshu2-1 do
XLedit[i,j].Free;
hangshu2:=StrToInt(Edit1.TExt);
SETLength(XLedit,hangshu2);
for i:=low(XLedit) to high(XLedit) do
SETLength(XLedit[i],hangshu2);
for i:=0 to 0 do
for j:=low(XLedit) to high(XLedit) do
begin
XLedit[i,j]:=TEdit.CreaTE(SElf);
with XLedit[i,j] do
begin
Parent:=Panel2;
Width:=40;
Height:=25;
left:=110+j*45;
top:=45+i*40;
end;
end;
//创建相邻相邻矩阵参数输入编辑框
if XLmatrix<>nil then
for i:=0 to hangshu3-1 do
for j:=0 to hangshu3-1 do
XLmatrix[i,j].Free;
hangshu3:=StrToInt(Edit1.TExt);
SETLength(XLmatrix,hangshu3);
for i:=low(XLmatrix) to high(XLmatrix) do
SETLength(XLmatrix[i],hangshu3);
for i:=low(XLmatrix) to high(XLmatrix) do
for j:=low(XLmatrix) to high(XLmatrix) do
begin
XLmatrix[i,j]:=TEdit.CreaTE(SElf);
with XLmatrix[i,j] do
begin
Parent:=Panel2;
Width:=40;
Height:=25;
left:=110+j*45;
top:=95+i*30;
end;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var j1,k,IQ,n,n1,i,j,y,flag:integer;
var x:real;
begin
n:=StrToInt(Edit1.Text);
//创建列节点名称输出编辑框
if edit2<>nil then
for i:=0 to hangshu11-1 do
for j:=0 to 0 do
edit2[i,j].Free;
hangshu11:=StrToInt(Edit1.TExt);
SETLength(edit2,hangshu11);
for i:=low(edit2) to high(edit2) do
SETLength(edit2[i],1);
for i:=low(edit2) to high(edit2) do
for j:=0 to 0 do
begin
edit2[i,j]:=TEdit.CreaTE(SElf);
with edit2[i,j] do
begin
Parent:=Panel2;
Width:=40;
Height:=25;
left:=180+N*40+j*35;
top:=95+i*30;
end;
end;
//创建行节点名称输出编辑框
if XLedit2<>nil then
for i:=0 to 0 do
for j:=0 to hangshu12-1 do
XLedit2[i,j].Free;
hangshu12:=StrToInt(Edit1.TExt);
SETLength(XLedit2,hangshu12);
for i:=low(XLedit2) to high(XLedit2) do
SETLength(XLedit2[i],hangshu12);
for i:=0 to 0 do
for j:=low(XLedit2) to high(XLedit2) do
begin
XLedit2[i,j]:=TEdit.CreaTE(SElf);
with XLedit2[i,j] do
begin
Parent:=Panel2;
Width:=40;
Height:=25;
left:=240+N*40+j*45;
top:=45+i*40;
end;
end;
//创建最短路径值输出编辑框
if XLmatrix2<>nil then
for i:=0 to hangshu13-1 do
for j:=0 to hangshu13-1 do
XLmatrix2[i,j].Free;
hangshu13:=StrToInt(Edit1.TExt);
SETLength(XLmatrix2,hangshu13);
for i:=low(XLmatrix2) to high(XLmatrix2) do
SETLength(XLmatrix2[i],hangshu13);
for i:=low(XLmatrix2) to high(XLmatrix2) do
for j:=low(XLmatrix2) to high(XLmatrix2) do
begin
XLmatrix2[i,j]:=TEdit.CreaTE(SElf);
with XLmatrix2[i,j] do
begin
Parent:=Panel2;
Width:=40;
Height:=25;
left:=240+N*40+j*45;
top:=95+i*30;
end;
end;
if checkbox1.Checked then flag:=1
else flag:=0;
for i:=0 to n-1 do
for j:=0 to n-1 do
if XLmatrix[i,j].Text='' then d[i+1,j+1]:=0
else if (XLmatrix[i,j].Text='m')or (XLmatrix[i,j].Text='M')
then d[i+1,j+1]:=1000000
else d[i+1,j+1]:=StrToFloat(XLmatrix[i,j].Text);
y:=0;
if flag=1 then
begin
N1:=n-1;
for i:=1 to N1 do
begin
D[i,i]:=0;
IQ:=I+1;
for j:=IQ to N do
begin
D[i,j]:=D[j,i];
end;
end;
end;
for k:=1 to n do
C[k,k]:=0;
j1:=1;
if flag<>1 then N1:=n;
for k:=1 to n do
begin
for i:=1 to n1 do
begin
if flag=1 then j1:=i+1;
for j:=j1 to N do
begin
X:=D[i,k]+D[k,j];
if D[i,j]<X then C[i,j]:=D[i,j]
else C[i,j]:=X;
if flag=1 then C[j,i]:=C[i,j];
end;
end;
for i:=1 to n do
for j:=1 to n do
D[i,j]:=C[i,j];
end;
//输出内容
for i:=0 to n-1 do
begin
XLedit[0,i].Text:=edit[i,0].Text;
edit2[i,0].Text:=edit[i,0].Text;
XLedit2[0,i].Text:=edit[i,0].Text;
for j:=0 to n-1 do
begin
XLmatrix[i,j].Text:=FloatToStr(D[i+1,j+1]);
if C[i+1,j+1]<>1000000
then XLmatrix2[i,j].Text:=FloatToStr(C[i+1,j+1])
else XLmatrix2[i,j].Text:='不通';
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -