unit UntItemCarInfo;interfacetype TItemCar = record //车队的一条记录信息
nIndex: integer; //车号
outDoor: Integer; //出口,初始化为0,可以进入出口1和出口2
enterTime: TDateTime; //进入队列时间
leaveTime: TDateTime; //离开队列的时间
end;
//PItemCarArray= array[0..0] of TItemCar; //队列中的一台车的记录
PItemCar = ^TItemCar; PSignalLamp = ^TSignalLamp; //对事件对象的引用 TSignalLamp = record //事件对象
win_event_obj: THandle; //windows Event对象句柄
lamp_state: Integer; //存储当前信号灯颜色信号。1-红 2-绿 3-黄
End; implementationend.
///
unit UntSafeQueue;interface
uses
Classes, UntItemCarInfo;type TSafeQueue = class(TObject)
private
ListCar: array of PItemCar; //队列,采用动态数组来表示
lenListCar: Integer; //队列的长度
HeadList: Integer; //队列头
TailList: Integer; //队列尾
protected
//
public
constructor Create(AlenListCar: Integer); overload; //初始化队列
destructor Destroy; override; //释放队列
function Pop(): PItemCar; //弹出队列
function Push(AListCar: PItemCar): PItemCar; //车压入队列
function GetCapacity(): Integer; //设置队列的长度
function Count():Integer; //得到当前队列中的记录的条数 ,对应于题目中所要求的Count()
function AtLeast(ACount: Integer): Boolean; //True返回当前队列中的车辆数目大于ACount
function ListCarEmpty(): Boolean; //队列是否为空
function ListCarFull(): Boolean; //队列是否已经满了
end;implementation{ TSafeQueue }//////////////////////////////////////////////////////////////
//功能描述:交通流的车的队列中车的数量是否大于等于ACount
//输入参数:ACount->输入的车流的数量
//输出参数:无
//返回值 :Result->布尔类型,True时当ACount小于当前队列的长度
//说明 :无
//////////////////////////////////////////////////////////////
function TSafeQueue.AtLeast(ACount: Integer): Boolean;
var
AmountCar: Integer;
begin
if ACount > lenListCar then //判断是否已经超出了队列的最大长度
begin
Result := False;
Exit;
end;
AmountCar := Count;
if AmountCar < ACount then
Result := False
else
Result := True;
end;//////////////////////////////////////////////////////////////
//功能描述:类TSafeQueue的构造函数
//输入参数:AlenListCar->输入的车流队列的最大长度
//输出参数:无
//返回值 :无
//说明 :此处通过SetLength设置了队列的最大长度。
//////////////////////////////////////////////////////////////
constructor TSafeQueue.Create(AlenListCar: Integer);
begin
lenListCar := AlenListCar;
//GetMem(ListCar, Sizeof(TItemCar)*AlenListCar);
SetLength(ListCar,AlenListCar); //设置队列的最大长度
HeadList := 0; //队列头
TailList := 0;
end;//////////////////////////////////////////////////////////////
//功能描述:类TSafeQueue的析构函数
//输入参数:无
//输出参数:无
//返回值 :无
//说明 :继承类TObject,释放空间。
//////////////////////////////////////////////////////////////
destructor TSafeQueue.Destroy;
begin
//FreeMem(ListCar, Sizeof(TItemCar)*lenListCar);
SetLength(ListCar,0);
inherited;
end;//////////////////////////////////////////////////////////////
//功能描述:返回队列的最大容量
//输入参数:无
//输出参数:无
//返回值 :得到当前的车的队列的长度
//说明 :无
//////////////////////////////////////////////////////////////
function TSafeQueue.GetCapacity: Integer;
begin
Result := lenListCar;
end;//////////////////////////////////////////////////////////////
//功能描述:取得当前队列中车的数量
//输入参数:无
//输出参数:无
//返回值 :队列中车的数量
//说明 :队列中车的数量小于或者等于队列中可容纳的车的数量。
//////////////////////////////////////////////////////////////
function TSafeQueue.Count: Integer;
var
nIndex: Integer;
begin
if HeadList > TailList then
nIndex := TailList - HeadList + 1
else if HeadList < TailList then
nIndex := lenListCar - (TailList - HeadList) + 1
else
nIndex := 0; Result := nIndex;
end;//////////////////////////////////////////////////////////////
//功能描述:判断队列是否为空,True->空
//输入参数:无
//输出参数:无
//返回值 :True->空,False->不空
//说明 :
//////////////////////////////////////////////////////////////
function TSafeQueue.ListCarEmpty: Boolean;
begin
if HeadList = TailList then
Result := True
else
Result := False;
end;//////////////////////////////////////////////////////////////
//功能描述:判断队列是否已满,True->满
//输入参数:无
//输出参数:无
//返回值 :True->满,False->不满
//说明 :
//////////////////////////////////////////////////////////////
function TSafeQueue.ListCarFull: Boolean;
begin
if ((TailList+1) mod lenListCar) = HeadList then
Result := True
else
Result := False;
end;//////////////////////////////////////////////////////////////
//功能描述:弹出队首的内容
//输入参数:无
//输出参数:无
//返回值 :指向队首内容的指针
//说明 :需要判断队列是否为空的情况
//////////////////////////////////////////////////////////////
function TSafeQueue.Pop: PItemCar;
begin
if HeadList = TailList then
begin
Result := nil;
Exit;
end; HeadList := (HeadList+1) mod lenListCar;
Result := ListCar[HeadList];
end;//////////////////////////////////////////////////////////////
//功能描述:把车流的记录压入队中去,
//输入参数:AListCar 需要压入队列中的车的记录
//输出参数:无
//返回值 :返回车的队尾的记录的内容
//说明 :需要判断队列是否已经满了的情况
//////////////////////////////////////////////////////////////
function TSafeQueue.Push(AListCar: PItemCar): PItemCar;
var
nIndex: integer;
begin
nIndex := (TailList+1) mod lenListCar;
if nIndex = HeadList then //是否队列已经满了
begin
Result := ListCar[TailList];
Exit;
end;
TailList := nIndex;
ListCar[nIndex] := AListCar;
Result := ListCar[nIndex];
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货