program StackHierarchy;
uses
SysUtils;
type
TStack = class
protected
FData: array of Pointer;
FCount: Integer;
FCapacity: Integer;
procedure Resize;
function GetTop: Pointer;
procedure SetTop(Value: Pointer);
public
constructor Create;
destructor Destroy; override;
function IsEmpty: Boolean;
procedure Push(Item: Pointer);
function Pop: Pointer;
property Top: Pointer read GetTop write SetTop;
end;
TIntegerStack = class(TStack)
public
function IsPositive: Boolean;
end;
TStringStack = class(TStack)
public
function IsLongString: Boolean;
end;
{ TStack }
constructor TStack.Create;
begin
FCapacity := 10;
SetLength(FData, FCapacity);
FCount := 0;
end;
destructor TStack.Destroy;
begin
SetLength(FData, 0);
inherited Destroy;
end;
function TStack.IsEmpty: Boolean;
begin
Result := FCount = 0;
end;
procedure TStack.Push(Item: Pointer);
begin
if FCount = FCapacity then
Resize;
FData[FCount] := Item;
Inc(FCount);
end;
function TStack.Pop: Pointer;
begin
if IsEmpty then
raise Exception.Create(‘Stack is empty’);
Dec(FCount);
Result := FData[FCount];
end;
procedure TStack.Resize;
begin
FCapacity := FCapacity * 2;
SetLength(FData, FCapacity);
end;
function TStack.GetTop: Pointer;
begin
if IsEmpty then
raise Exception.Create(‘Stack is empty’);
Result := FData[FCount - 1];
end;
procedure TStack.SetTop(Value: Pointer);
begin
if IsEmpty then
raise Exception.Create(‘Stack is empty’);
FData[FCount - 1] := Value;
end;
{ TIntegerStack }
function TIntegerStack.IsPositive: Boolean;
begin
Result := False;
if not IsEmpty then
Result := Integer(Top) > 0;
end;
{ TStringStack }
function TStringStack.IsLongString: Boolean;
begin
Result := False;
if not IsEmpty then
Result := Length(PString(Top)^) > 5;
end;
var
IntegerStack: TIntegerStack;
StringStack: TStringStack;
i: Integer;
S: string;
Value: Pointer;
begin
IntegerStack := TIntegerStack.Create;
StringStack := TStringStack.Create;
Writeln(‘Enter integer elements, 0 to finish:’);
repeat
Readln(i);
if i <> 0 then
IntegerStack.Push(Pointer(i));
until i = 0;
Writeln(‘Enter string elements, empty string to finish:’);
repeat
Readln(S);
if S <> ‘’ then
begin
Value := AllocMem(Length(S) + 1);
PString(Value)^ := S;
StringStack.Push(Value);
end;
until S = ‘’;
Writeln;
Writeln(‘Elements satisfying the criteria:’);
while not IntegerStack.IsEmpty do
begin
Value := IntegerStack.Pop;
if IntegerStack.IsPositive then
Writeln(Integer(Value));
end;
Writeln;
while not StringStack.IsEmpty do
begin
Value := StringStack.Pop;
if StringStack.IsLongString then
Writeln(string(PString(Value)^));
FreeMem(Value);
end;
IntegerStack.Free;
StringStack.Free;
end.
Объясни этот код построчно