Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project6;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- type
- TTimeArr = array of Integer;
- TClient = class
- public
- constructor Create(Arr: TTimeArr; Priority: Integer);
- function IsInEnterState: Boolean;
- private
- Priority, EnterTime, CurrInd: Integer;
- TaskArr: TTimeArr;
- end;
- TClientArr = array[0..5] of TClient;
- PQueueElem = ^TQueueElem;
- TQueueElem = record
- Data: TClient;
- Next, Prev: PQueueElem;
- end;
- TQueue = class
- public
- constructor Create;
- procedure Add(var Elem: TClient);
- function Get: TClient;
- function IsEmpty: Boolean;
- private
- Head, Tail: PQueueElem;
- end;
- constructor TClient.Create(Arr: TTimeArr; Priority: Integer);
- begin
- TaskArr := Arr;
- Self.Priority := Priority;
- EnterTime := 0;
- CurrInd := 0;
- end;
- function TClient.IsInEnterState: Boolean;
- begin
- Result := EnterTime > 0;
- end;
- constructor TQueue.Create;
- begin
- Head := nil;
- Tail := nil;
- end;
- procedure TQueue.Add(var Elem: TClient);
- var
- PElem, Buff: PQueueElem;
- begin
- New(PElem);
- PElem.Data := Elem;
- PElem.Next := nil;
- PElem.Prev := nil;
- if (Head = nil) then
- begin
- Head := PElem;
- Tail := Head;
- end
- else
- begin
- Buff := Tail;
- while ((Buff <> nil) and (Buff.Data.Priority > Elem.Priority)) do
- Buff := Buff.Next;
- if (Buff = nil) then
- begin
- PElem.Prev := Head;
- Head.Next := PElem;
- Head := PElem;
- end
- else
- begin
- PElem.Prev := Buff.Prev;
- PElem.Next := Buff;
- Buff.Prev := PElem;
- if (PElem.Prev <> nil) then
- PElem.Prev.Next := PElem;
- while (Tail.Prev <> nil) do
- Tail := Tail.Prev;
- end;
- end;
- end;
- function TQueue.Get: TClient;
- var
- Buff: PQueueElem;
- begin
- if ((Head <> nil) and (Head.Data.IsInEnterState)) then
- begin
- Buff := Head.Prev;
- while ((Buff <> nil) and (Buff.Data.IsInEnterState)) do
- Buff := Buff.Prev;
- if (Buff <> nil) then
- begin
- Buff.Next.Prev := Buff.Prev;
- if (Buff.Prev <> nil) then
- Buff.Prev.Next := Buff.Next
- else
- Tail := Buff.Next;
- end;
- end
- else
- begin
- Buff := Head;
- if (Head <> nil) then
- begin
- Head := Head.Prev;
- if (Head <> nil) then
- Head.Next := nil;
- end;
- end;
- if Buff <> nil then
- Result := Buff.Data
- else
- Result := nil;
- end;
- function TQueue.IsEmpty: Boolean;
- begin
- Result := Head = nil;
- end;
- function Tick(var Arr: TClientArr; var Que: TQueue;
- CurrTickTime, CurrEnterTime: Integer): Integer;
- var
- CurrClient: TClient;
- UselesTick, I: Integer;
- begin
- UselesTick := 0;
- CurrClient := Que.Get();
- if CurrClient <> nil then
- begin
- with CurrClient do
- begin
- TaskArr[CurrInd] := TaskArr[CurrInd] - CurrTickTime;
- if (TaskArr[CurrInd] <= 0) then
- begin
- UselesTick := (-1) * TaskArr[CurrInd];
- EnterTime := CurrEnterTime;
- EnterTime := EnterTime - UselesTick;
- Inc(CurrInd);
- end;
- if (CurrInd < Length(TaskArr)) then
- begin
- Que.Add(CurrClient);
- end;
- end;
- end
- else
- UselesTick := CurrTickTime;
- for I := Low(Arr) to High(Arr) do
- begin
- if (Arr[I] <> CurrClient) and (Arr[I].IsInEnterState) then
- Arr[I].EnterTime := Arr[I].EnterTime - CurrTickTime;
- end;
- Result := UselesTick;
- end;
- function Emulate(TickCount, EnterTime: Integer;
- var TotalUselesTick: Integer): Integer;
- var
- Que: TQueue;
- Tacts, I: Integer;
- ClientArr: TClientArr;
- const
- Arr1: TTimeArr = [3, 2, 1, 5, 4, 3, 5, 6, 8];
- Arr2: TTimeArr = [9, 2, 9, 4, 6, 8, 5, 7, 4];
- Arr3: TTimeArr = [5, 6, 9, 7, 7, 4, 3, 2, 1];
- Arr4: TTimeArr = [1, 2, 3, 3, 4, 6, 8, 9, 7];
- Arr5: TTimeArr = [5, 6, 4, 3, 5, 4, 3, 7, 9];
- Arr6: TTimeArr = [9, 6, 8, 2, 7, 1, 9, 7, 4];
- begin
- TotalUselesTick := 0;
- Tacts := 0;
- ClientArr[0] := TClient.Create(Arr1, 1);
- ClientArr[1] := TClient.Create(Arr2, 1);
- ClientArr[2] := TClient.Create(Arr3, 1);
- ClientArr[3] := TClient.Create(Arr4, 2);
- ClientArr[4] := TClient.Create(Arr5, 3);
- ClientArr[5] := TClient.Create(Arr5, 3);
- Que := TQueue.Create();
- for I := Low(ClientArr) to High(ClientArr) do
- Que.Add(ClientArr[I]);
- while (not Que.IsEmpty) do
- begin
- Inc(Tacts);
- TotalUselesTick := TotalUselesTick + Tick(ClientArr, Que, TickCount, EnterTime);
- end;
- Result := Tacts;
- end;
- var
- TickCount, EnterTime, CurrTacts, CurrUseles, I: Integer;
- begin
- WriteLn('|':5);
- for EnterTime := 0 to 10 do
- Write(EnterTime:2, '|':7);
- WriteLn;
- for TickCount := 1 to 10 do
- begin
- Write(TickCount:2, '|':3);
- for EnterTime := 0 to 10 do
- begin
- CurrTacts := Emulate(TickCount, EnterTime, CurrUseles);
- Write(CurrUseles:3, '/':1, ((CurrTacts * TickCount - CurrUseles) / (CurrTacts * TickCount)):1:2, '|':1);
- end;
- WriteLn;
- for I := 0 to 103 do
- Write('-');
- WriteLn;
- end;
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement