unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids, XPMan;
type
TForm1 =
class
(TForm)
Button1: TButton;
tmr1: TTimer;
strngrd1: TStringGrid;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
lbl4: TLabel;
edt1: TEdit;
lbl5: TLabel;
lbl6: TLabel;
lbl7: TLabel;
lbl8: TLabel;
procedure
Button1Click(Sender: TObject);
procedure
FormCreate(Sender: TObject);
procedure
tmr1Timer(Sender: TObject);
procedure
WriteGrid;
procedure
strngrd1DrawCell(Sender: TObject; ACol, ARow:
Integer
;
Rect: TRect; State: TGridDrawState);
procedure
WaitingTime;
private
public
end
;
var
Form1: TForm1;
proc:
array
[
1..5
]
of
array
[
0..2
]
of
Integer
;
number_proc:
Integer
;
kvant:
Integer
=
0
;
i:
Integer
=
1
;
implementation
{$R *.dfm}
procedure
SortProc;
var
i,j:
Integer
;
x0,x1,x2:
Integer
;
begin
for
I :=
1
to
4
do
for
j :=
1
to
4
do
if
proc[j,
1
] > proc[j+
1
,
1
]
then
begin
x0:= proc[j+
1
,
0
];
x1:= proc[j+
1
,
1
];
x2:= proc[j+
1
,
2
];
proc[j+
1
,
0
] := proc[j,
0
];
proc[j+
1
,
1
] := proc[j,
1
];
proc[j+
1
,
2
] := proc[j,
2
];
proc[j,
0
] := x0;
proc[j,
1
] := x1;
proc[j,
2
] := x2;
end
;
end
;
procedure
TForm1
.
Button1Click(Sender: TObject);
begin
if
tmr1
.
Enabled
then
begin
tmr1
.
Enabled:=
false
;
Button1
.
Caption:=
'Запуск'
;
end
else
begin
FormCreate(Sender);
strngrd1
.
ColCount:=
1
;
number_proc:=
1
;
kvant:=
0
;
i:=
1
;
SortProc;
tmr1
.
Interval:=StrToInt(edt1
.
Text);
tmr1
.
Enabled:=
True
;
Button1
.
Caption:=
'Стоп'
;
end
;
end
;
procedure
TForm1
.
FormCreate(Sender: TObject);
var
i:
integer
;
begin
for
i:=
1
to
5
do
begin
strngrd1
.
Cells[
0
, i]:=IntToStr(i);
proc[i,
0
]:= i;
end
;
proc[
1
,
1
]:=
3
;
proc[
1
,
2
]:=
10
;
proc[
2
,
1
]:=
6
;
proc[
2
,
2
]:=
4
;
proc[
3
,
1
]:=
0
;
proc[
3
,
2
]:=
5
;
proc[
4
,
1
]:=
2
;
proc[
4
,
2
]:=
1
;
proc[
5
,
1
]:=
4
;
proc[
5
,
2
]:=
3
;
end
;
procedure
TForm1
.
WaitingTime;
var
waiting_time, turnaroud_time:
Real
;
i,j:
Integer
;
begin
waiting_time:=
0
;
turnaroud_time:=
0
;
for
i:=
1
to
strngrd1
.
RowCount-
1
do
for
j:=
1
to
strngrd1
.
ColCount-
1
do
begin
if
strngrd1
.
Cells[j,i]=
'Г'
then
waiting_time:=waiting_time+
1
;
if
strngrd1
.
Cells[j,i]<>
'-'
then
turnaroud_time:=turnaroud_time+
1
;
end
;
lbl3
.
Caption:=
'Среднее время ожидания: '
+ FloatToStr(waiting_time/
5
);
lbl4
.
Caption:=
'Среднее полное время выполнени: '
+ FloatToStr(turnaroud_time/
5
);
end
;
procedure
TForm1
.
WriteGrid;
var
i:
Integer
;
begin
lbl1
.
Caption:=
'Текущий квант времени: '
+inttostr(kvant);
lbl2
.
Caption:=
'Исполняемый процесс: '
+inttostr(proc[number_proc,
0
]);
strngrd1
.
Cells[kvant,
0
]:=IntToStr(kvant);
strngrd1
.
ColCount:=kvant+
1
;
strngrd1
.
Cells[kvant,proc[number_proc,
0
]]:=
'В'
;
for
i:=
1
to
5
do
begin
if
i<>number_proc
then
begin
if
((proc[i,
2
]=
0
)
or
(proc[i,
1
]>kvant))
then
strngrd1
.
Cells[kvant,proc[i,
0
]]:=
'-'
else
strngrd1
.
Cells[kvant,proc[i,
0
]]:=
'Г'
;
end
;
end
;
end
;
procedure
TForm1
.
tmr1Timer(Sender: TObject);
var
j,k:
integer
;
flag:
Boolean
;
begin
k:=
0
;
Inc(kvant);
repeat
if
((proc[number_proc,
1
]<=kvant)
and
(proc[number_proc,
2
]<>
0
)
and
(i<=
4
))
then
begin
Dec(proc[number_proc,
2
]);
Inc(i);
flag:=
True
;
WriteGrid;
WaitingTime;
end
else
begin
Inc(number_proc);
if
number_proc>
5
then
number_proc:=
1
;
flag:=
False
;
i:=
1
;
end
;
for
j:=
1
to
5
do
if
proc[j,
2
]<>
0
then
Inc(k);
if
k=
0
then
begin
tmr1
.
Enabled:=
False
;
Button1
.
Caption:=
'Запуск'
;
Exit;
end
;
until
(flag);
end
;
procedure
TForm1
.
strngrd1DrawCell(Sender: TObject; ACol, ARow:
Integer
;
Rect: TRect; State: TGridDrawState);
begin
if
strngrd1
.
Cells[ACol, ARow] =
'В'
then
strngrd1
.
Canvas
.
Brush
.
Color:=
255
;
if
strngrd1
.
Cells[ACol, ARow] =
'Г'
then
strngrd1
.
Canvas
.
Brush
.
Color:=clMenuHighlight;
strngrd1
.
Canvas
.
Font
.
Color := clBlack;
strngrd1
.
Canvas
.
FillRect(Rect);
strngrd1
.
Canvas
.
TextOut(Rect
.
Left, Rect
.
Top, strngrd1
.
Cells[ACol, ARow]);
end
;
end
.