Консультация № 188441
16.12.2015, 21:29
0.00 руб.
0 0 0
Здравствуйте, уважаемые эксперты! Прошу вас ответить на следующий вопрос:
Начал изучать сложную динамическую структуру данных.
Вот программа на паскале:
uses
GraphABC;

const
n = 10;

type
PnodeS = ^nodeS;
PnodeM = ^nodeM;
PnodeR = ^nodeR;

//Главный нод (2 связи)
nodeM = record
data: string;
pointM: PnodeM;
pointS: pnodeS;
end;
//Нод-саттелит (1 связь)
nodeS = record
data: char;
point: PnodeR;
end;
//Возвращающий нод (1 связь)
nodeR = record
data: char;
point: pnodeM;
end;

var
stackhead: pnodeM;
a: array[1..26] of char;

procedure NewNode(stack: pnodeM);
var
st: pnodeM;
st2: pnodeS;
st3: pnodeR;
begin
New(st);
st^.data := a[random(27) + 1] + a[random(27) + 1];
st^.pointM := stackhead;
New(st2);
st^.pointS := st2;
st^.pointS^.data := a[random(27) + 1];
New(st3);
st^.pointS^.point := st3;
st^.pointS^.point^.data := a[random(27) + 1];
st^.pointS^.point^.point := st;
stackhead := st;
end;

function finderS(stack: pnodeM; temp: char): pnodeS;
begin
while (stack <> nil) do
begin
if stack^.pointS^.data = temp then
begin
result := stack^.pointS;
exit;
end;
stack := stack^.pointM;
end;
result := nil;
end;

function finderR(stack: pnodeM; temp: char): pnodeR;
begin
while (stack <> nil) do
begin
if stack^.pointS^.point^.data = temp then
begin
result := stack^.pointS^.point;
exit;
end;
stack := stack^.pointM;
end;
result := nil;
end;

function finderM(stack: pnodeM; temp: string): pnodeM;
begin
while (stack <> nil) do
begin
if stack^.data = temp then
begin
result := stack;
exit;
end;
stack := stack^.pointM;
end;
result := nil;
end;

procedure drawstructure(struct: pnodeM; x: integer);
begin
DrawRectangle(x * 90, 10 + 20, x * 90 + 50, 40 + 20);
DrawRectangle(x * 90 + 50, 10 + 20, x * 90 + 55, 40 + 20);
DrawRectangle(x * 90 + 55, 10 + 20, x * 90 + 60, 40 + 20);
TextOut(x * 90 + 20, 15 + 20, struct^.data);
line(x * 90 + 50, 40 + 20, x * 90, 60 + 20);
line(x * 90 + 60, 20 + 20, x * 90 + 90, 40);
end;

procedure drawstructure(struct: pnodeS; x: integer);
begin
DrawRectangle(x * 90, 60 + 20, x * 90 + 50, 90 + 20);
DrawRectangle(x * 90 + 50, 60 + 20, x * 90 + 55, 90 + 20);
TextOut(x * 90 + 20, 65 + 20, struct^.data);
line(x * 90 + 50, 90 + 20, x * 90, 110 + 20);
end;

procedure drawstructure(struct: pnodeR; x: integer);
begin
DrawRectangle(x * 90, 110 + 20, x * 90 + 50, 140 + 20);
DrawRectangle(x * 90 + 50, 110 + 20, x * 90 + 55, 140 + 20);
TextOut(x * 90 + 20, 115 + 20, struct^.data);
MoveTo(x * 90 + 50, 160);
Lineto(x * 90 + 50, 170);
LineTo(x * 90 - 10, 170);
lineTo(x * 90, 40 + 20);
end;


var

c: char;
str: pnodeM;
i: integer;
flag: boolean;
s: string;

begin
LockDrawing;
window.SetSize(1100, 200);
i := 1;
for c := 'a' to 'z' do
begin
a[i] := c;
i := i + 1;
end;


repeat
i := i + 1;
flag := false;
stackhead := nil;
for i := 1 to n do
begin
try
NewNode(stackhead);
except
flag := true;
end;
end;
until not(flag);

str := stackHead;
for i := 1 to n do
begin
drawstructure(str, i);
drawstructure(str^.pointS, i);
drawstructure(str^.pointS^.point, i);
str := str^.pointM;
end;
redraw;
flag := false;

while true do
begin
readln(s);
Window.clear;
if length(s) = 1 then
flag := true
else
flag := false;
str := stackhead;
for i := 1 to n do
begin
if (str = finderM(str, s))and(not flag) then
SetPenColor(clRed)
else
SetPenColor(clBlack);
drawstructure(str, i);
if (str^.pointS = finderS(str, s[1]))and flag then
SetPenColor(clRed)
else
SetPenColor(clBlack);
drawstructure(str^.pointS, i);
if (str^.pointS^.point = finderR(str, s[1]))and flag then
SetPenColor(clRed)
else
SetPenColor(clBlack);
drawstructure(str^.pointS^.point, i);
str := str^.pointM;

end;
Redraw;
end;

end.




можете объяснить, что тут происходит? желательно коммент возже каждой процедуры....

Обсуждение

Форма ответа