-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufloatstack.pas
78 lines (64 loc) · 1.32 KB
/
ufloatstack.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
unit ufloatstack;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TFloatStack = class
strict private
FItems: array of double;
FTop:integer;
function GetTop:double; //inspect top item without popping
function GetSize:integer;
public
constructor Create;
destructor Destroy; override;
procedure Push(s:double);
function Pop:double;
function IsEmpty:boolean;
property Top: double read GetTop;
property Size: integer read GetSize;
end;
implementation
{ TStack }
constructor TFloatStack.Create;
begin
SetLength(FItems,100);
FTop:=-1;
end;
destructor TFloatStack.Destroy;
begin
FItems:=nil;
inherited;
end;
function TFloatStack.GetSize: integer;
begin
result:=FTop+1;
end;
function TFloatStack.GetTop: double;
begin
if not IsEmpty then
result:=FItems[FTop];
end;
function TFloatStack.IsEmpty: boolean;
begin
result:=Size=0;
end;
function TFloatStack.Pop: double;
begin
if not IsEmpty then
begin
result:=GetTop;
dec (FTop);
if FTop+105<length(FItems) then
SetLength(FItems,length(FItems)-100);
end;
end;
procedure TFloatStack.Push(s: double);
begin
inc(FTop);
FItems[FTop]:=s;
if FTop> length(FItems)-5 then
SetLength(FItems,length(FItems)+100);
end;
end.