Разработка алгоритмов прямого пошагового выполнения визуализации и выполнения отката

//Запись очереди в таблицу типа TStringGrid.

procedure QueueToSg(const aQueue: TQueue; aSg: TStringGrid);

var

i,j: Integer;

begin

aSg.Rows[0].Clear;

aSg.Rowcount:=2;

if aQueue.Cnt = 0 then begin

aSg.ColCount:=1;

aSg.Cells[0, 0]:= '';

end else begin

aSg.ColCount:= aQueue.cnt;

for i:= 0 to aQueue.cnt-1 do

aSg.Cells[i, 0]:=aQueue.Arr[i];

end;

for j:= 0 to aQueue.Cnt do asg.Cells[j,1]:='...';

if aQueue.start<>-1 then

begin

asg.Cells[aQueue.start,1]:='start';

asg.Cells[aQueue.ent,1]:='end';

if aQueue.start=aQueue.ent then asg.Cells[aQueue.ent,1]:='start end';

end;

if (aQueue.free<>-1) and (aQueue.cnt=0) then

asg.Cells[aQueue.free,1]:='free';

end;

procedure enq(var aQueue: TQueue; const aData: TData);

var

j:integer;

begin

with aQueue do begin

if cnt<length(arr) then begin

inc(cnt);

arr[cnt-1]:=adata;

inc(ent);

if start=-1 then inc(start);

if cnt=length(arr) then

free:=-1

ELSE inc(free);

end else

begin

form1.edit3.text:=arr[start];

arr[start]:=adata;

inc(start);

ent:=start-1;

if start>length(arr)-1 then start:=0;

end;

end;

end;

//Изъятие элемента из начала очереди.

function popr(var aQueue: TQueue):string;

var

i,j: Integer;

begin

if aQueue.cnt=0 then exit;

popr:=aQueue.arr[aQueue.start];

aQueue.arr[aQueue.start]:='';

aQueue.free:=aQueue.start;

if aQueue.cnt<>0 then

with aQueue do begin

if start=ent then

begin

start:=-1;

ent:=-1;

cnt:=0;

free:=0;

end;

if start=0 then begin

for i:= 0 to cnt-2 do

arr[i]:=arr[i+1];

aQueue.arr[aQueue.Cnt-1]:='';

dec(ent);

dec(cnt);

end;

if start>ent then begin

for i:= start to cnt-2 do

arr[i]:=arr[i+1];

aQueue.arr[aQueue.Cnt-1]:='';

dec(cnt);

end;

if start=cnt then begin

start:=0;

end; end;

Блок-схема алгоритма процедуры представлена в Приложении 1 и 2

Особенности программной реализации

Для удобства реализации программы был выбран элемент Delphi10 stringrid – таблица. В нее выводится очередь для того чтобы показать какие элементы находятся сейчас в очереди.

Методика и результаты тестирования

Для начала необходимо ввести рандом чисел. По умолчанию он равен 5. Затем, при нажатии на кнопку рандом чисел числа меняются. Затем мы можем извлечь число из начала очереди. Для этого нам надо нажать кнопку извлечь элемент. Для вставки элемента в очередь необходимо ввести число и нажать кнопку вставить элемент. Так же мы можем добавить или уменьшить количество элементов в очереди.


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: