📄 xubin.pas
字号:
unit xubin;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, DBCtrls, Grids, jpeg, OleCtrls,
ShockwaveFlashObjects_TLB, Buttons;
type
TForm1 = class(TForm)
Timer1: TTimer;
GroupBox1: TGroupBox;
num: TEdit;
state: TEdit;
time: TEdit;
hunger: TEdit;
p0: TEdit;
s0: TEdit;
t0: TEdit;
h0: TEdit;
p1: TEdit;
s1: TEdit;
t1: TEdit;
h1: TEdit;
p2: TEdit;
s2: TEdit;
t2: TEdit;
h2: TEdit;
p3: TEdit;
s3: TEdit;
t3: TEdit;
h3: TEdit;
p4: TEdit;
s4: TEdit;
t4: TEdit;
h4: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Shape1: TShape;
kz0: TImage;
kz1: TImage;
kz2: TImage;
kz3: TImage;
kz4: TImage;
fla0: TShockwaveFlash;
fla1: TShockwaveFlash;
fla2: TShockwaveFlash;
fla3: TShockwaveFlash;
fla4: TShockwaveFlash;
a0: TEdit;
a1: TEdit;
a2: TEdit;
a3: TEdit;
a4: TEdit;
Start: TBitBtn;
stop: TBitBtn;
speedup: TBitBtn;
speeddown: TBitBtn;
piece: TEdit;
Label6: TLabel;
Label7: TLabel;
kz00: TImage;
kz11: TImage;
kz22: TImage;
kz33: TImage;
kz44: TImage;
help: TBitBtn;
GroupBox2: TGroupBox;
imfor: TLabel;
Image1: TImage;
pic: TBitBtn;
GroupBox3: TGroupBox;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure StartClick(Sender: TObject);
procedure stopClick(Sender: TObject);
procedure speedupClick(Sender: TObject);
procedure speeddownClick(Sender: TObject);
procedure helpClick(Sender: TObject);
procedure picClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type Philosopher=record
t,ht:integer;{t-吃饭时间或思考时间,ht-饥饿时间即优先权}
st:ShortString;{哲学家状态-1思考,2吃饭,3等待}
end;
var
Form1: TForm1;
chop:array[0..4] of integer; {"筷子"数组,chop[i]的值为1则筷子i空闲}
p:array[0..4] of Philosopher; {哲学家结构体数组}
edits:array[0..4,1..4] of ^Tedit;
Q: array[0..4] of ^TShockwaveFlash;
image:array[0..4,0..1] of ^Timage;
implementation
{$R *.dfm}
procedure TForm1.StartClick(Sender: TObject);
var
i:integer;
begin
for i:=0 to 4 do
Q[i].Visible:=true;
if(Start.Caption='&Start 开始') then
begin
Timer1.Interval:=1000;
piece.Text:=inttostr(Timer1.Interval);
Start.Caption:='暂停';
end
else
begin
Timer1.Interval:=0;
piece.Text:=inttostr(Timer1.Interval);
Start.Caption:='&Start 开始';
end
end;
procedure TForm1.stopClick(Sender: TObject);
var
i:integer;
begin
if(Start.Caption='暂停') then
Start.Caption:='&Start 开始';
edits[0,1]:=@s0;
edits[0,2]:=@t0;
edits[0,3]:=@h0;
edits[0,4]:=@a0;
edits[1,1]:=@s1;
edits[1,2]:=@t1;
edits[1,3]:=@h1;
edits[1,4]:=@a1;
edits[2,1]:=@s2;
edits[2,2]:=@t2;
edits[2,3]:=@h2;
edits[2,4]:=@a2;
edits[3,1]:=@s3;
edits[3,2]:=@t3;
edits[3,3]:=@h3;
edits[3,4]:=@a3;
edits[4,1]:=@s4;
edits[4,2]:=@t4;
edits[4,3]:=@h4;
edits[4,4]:=@a4;
Q[0]:=@fla0;
Q[1]:=@fla1;
Q[2]:=@fla2;
Q[3]:=@fla3;
Q[4]:=@fla4;
image[0,0]:=@kz0;
image[1,0]:=@kz1;
image[2,0]:=@kz2;
image[3,0]:=@kz3;
image[4,0]:=@kz4;
image[0,1]:=@kz00;
image[1,1]:=@kz11;
image[2,1]:=@kz22;
image[3,1]:=@kz33;
image[4,1]:=@kz44;
Timer1.Interval:=0;
piece.Text:=inttostr(Timer1.Interval);
for i:=0 to 4 do begin {初始化}
chop[i]:=1;
image[i,0].Visible:=true;
image[i,1].Visible:=false;
Q[i].LoadMovie(0,ExpandFileName('flash\thinking.swf'));
with p[i] do
begin
t:=0;
t:=round(random*5);
ht:=0;
st:=' 思考';
end;
edits[i,1].Text:=p[i].st;
edits[i,4].Text:=p[i].st;
edits[i,2].Text:='0';
edits[i,3].Text:=inttostr(p[i].ht);
end;
imfor.Caption:='编号: 编号 i 表示哲学家 i '+#13+
'状态: 包括思考、吃饭、饥饿'+#13+
'时间: 思考状态时表示思考时间;'+#13+
' 吃饭状态时表示吃饭时间;'+#13+
' 等待时表示申请吃饭的时间;'+#13+
'饥饿: 表示饥饿程度,数值越高'+#13+
' 优先权越大';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i,j,tem:integer;
f:array[0..4] of integer; {用于存放哲学家的处理顺序,即对哲学家的优先权进行排序}
begin
for i:=0 to 4 do {对处理哲学家的顺序进行排序}
f[i]:=i; {饥饿--优先权高的哲学家先处理}
for i:=0 to 4 do
for j:=0 to 3-i do
begin
if (p[f[i]].ht<p[f[i+1]].ht) then
begin
tem:=f[i];
f[i]:=f[i+1];
f[i+1]:=tem;
end;
end;
for i:=0 to 4 do {处理哲学家f[i]}
begin
if p[f[i]].st=' 思考' then {若哲学家处于思考状态}
begin
p[f[i]].t:=p[f[i]].t-1;
if p[f[i]].t=-1 then {若哲学家思考完毕}
begin
p[f[i]].st:=' 等待'; {变成等待状态}
Q[f[i]].LoadMovie(0,ExpandFileName('flash\waiting.swf'));
randomize;
p[f[i]].t:=round(random*10); {随机生成吃饭时间}
end;
edits[f[i],1].Text:=p[f[i]].st;
edits[f[i],4].Text:=p[f[i]].st;
edits[f[i],2].Text:=inttostr(p[f[i]].t);
edits[f[i],3].Text:=inttostr(p[f[i]].ht);
end
else if p[f[i]].st=' 吃饭' then {若哲学家处于吃饭状态}
begin
p[f[i]].t:=p[f[i]].t-1;
if p[f[i]].t=-1 then {若哲学家吃饭完毕}
begin
p[f[i]].st:=' 思考'; {变成思考状态}
Q[f[i]].LoadMovie(0,ExpandFileName('flash\thinking.swf'));
randomize;
p[f[i]].t:=round(random*10); {随机生成思考时间}
chop[f[i]]:=1; {放下筷子}
chop[(f[i]+1)mod 5]:=1;
image[f[i],0].Visible:=true;
image[f[i],1].Visible:=false;
image[(f[i]+1)mod 5,0].Visible:=true;
image[(f[i]+1)mod 5,1].Visible:=false;
end;
edits[f[i],1].Text:=p[f[i]].st;
edits[f[i],4].Text:=p[f[i]].st;
edits[f[i],2].Text:=inttostr(p[f[i]].t);
edits[f[i],3].Text:=inttostr(p[f[i]].ht);
end
else {若哲学家处于等待状态}
begin
if(chop[f[i]]=1)and (chop[(f[i]+1)mod 5]=1) then {若哲学家两双筷子都能用}
begin
p[f[i]].st:=' 吃饭'; {变成吃饭状态}
p[f[i]].ht:=0;
Q[f[i]].LoadMovie(0,ExpandFileName('flash\eating.swf'));
chop[f[i]]:=0; {拿起筷子}
chop[(f[i]+1)mod 5]:=0;
image[f[i],0].Visible:=false;
image[f[i],1].Visible:=true;
image[(f[i]+1)mod 5,0].Visible:=false;
image[(f[i]+1)mod 5,1].Visible:=true;
end
else {若哲学家有一双筷子不能用}
p[f[i]].ht:=p[f[i]].ht+1; {则该哲学家的饥饿--优先权加1}
edits[f[i],1].Text:=p[f[i]].st;
edits[f[i],4].Text:=p[f[i]].st;
edits[f[i],2].Text:=inttostr(p[f[i]].t);
edits[f[i],3].Text:=inttostr(p[f[i]].ht);
end
end;
end;
procedure TForm1.speedupClick(Sender: TObject);
begin
if Timer1.Interval>=1500 then
Timer1.Interval:=Timer1.Interval-500;
piece.Text:=inttostr(Timer1.Interval);
end;
procedure TForm1.speeddownClick(Sender: TObject);
begin
Timer1.Interval:=Timer1.Interval+500;
piece.Text:=inttostr(Timer1.Interval);
end;
procedure TForm1.helpClick(Sender: TObject);
begin
messagedlg(' 算法说明'+#13+#13+
' 随机生成每个哲学家的思考、吃饭时间,只有哲学家旁边'+#13+
'两只筷子都能用时才能吃饭,若不能吃饭则饥饿值加1,优先权'+#13+
'增加。这个算法不存在死锁,长时间测试也未出现死锁,所以'+#13+
'没有死锁检测和处理。'+#13+#13+
' 03网络工程1班 2003372128 湛栩斌',mtInformation,[mbOK],0);
end;
procedure TForm1.picClick(Sender: TObject);
var
i:integer;
begin
for i:=1 to 4 do
Q[i].Visible:=false;
if(pic.Caption='图标说明') then
begin
Q[0].LoadMovie(0,ExpandFileName('flash\thinking.swf'));
pic.Caption:='思考';
end
else if(pic.Caption='思考') then
begin
Q[0].LoadMovie(0,ExpandFileName('flash\waiting.swf'));
pic.Caption:='等待';
end
else if(pic.Caption='等待') then
begin
Q[0].LoadMovie(0,ExpandFileName('flash\eating.swf'));
pic.Caption:='吃饭';
end
else if(pic.Caption='吃饭') then
begin
Q[0].LoadMovie(0,ExpandFileName('flash\thinking.swf'));
pic.Caption:='图标说明';
for i:=1 to 4 do
Q[i].Visible:=true;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -