Advertisement
dxvmxnd

Untitled

Nov 18th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. program Project6;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. System.SysUtils;
  7.  
  8. type
  9. TTimeArr = array of Integer;
  10.  
  11. TClient = class
  12. public
  13. constructor Create(Arr: TTimeArr; Priority: Integer);
  14. function IsInEnterState: Boolean;
  15. private
  16. Priority, EnterTime, CurrInd: Integer;
  17. TaskArr: TTimeArr;
  18. end;
  19.  
  20. TClientArr = array[0..5] of TClient;
  21.  
  22. PQueueElem = ^TQueueElem;
  23.  
  24. TQueueElem = record
  25. Data: TClient;
  26. Next, Prev: PQueueElem;
  27. end;
  28.  
  29. TQueue = class
  30. public
  31. constructor Create;
  32. procedure Add(var Elem: TClient);
  33. function Get: TClient;
  34. function IsEmpty: Boolean;
  35. private
  36. Head, Tail: PQueueElem;
  37. end;
  38.  
  39. constructor TClient.Create(Arr: TTimeArr; Priority: Integer);
  40. begin
  41. TaskArr := Arr;
  42. Self.Priority := Priority;
  43. EnterTime := 0;
  44. CurrInd := 0;
  45. end;
  46.  
  47. function TClient.IsInEnterState: Boolean;
  48. begin
  49. Result := EnterTime > 0;
  50. end;
  51.  
  52. constructor TQueue.Create;
  53. begin
  54. Head := nil;
  55. Tail := nil;
  56. end;
  57.  
  58. procedure TQueue.Add(var Elem: TClient);
  59. var
  60. PElem, Buff: PQueueElem;
  61. begin
  62. New(PElem);
  63. PElem.Data := Elem;
  64. PElem.Next := nil;
  65. PElem.Prev := nil;
  66. if (Head = nil) then
  67. begin
  68. Head := PElem;
  69. Tail := Head;
  70. end
  71. else
  72. begin
  73. Buff := Tail;
  74. while ((Buff <> nil) and (Buff.Data.Priority > Elem.Priority)) do
  75. Buff := Buff.Next;
  76.  
  77. if (Buff = nil) then
  78. begin
  79. PElem.Prev := Head;
  80. Head.Next := PElem;
  81. Head := PElem;
  82. end
  83. else
  84. begin
  85. PElem.Prev := Buff.Prev;
  86. PElem.Next := Buff;
  87. Buff.Prev := PElem;
  88. if (PElem.Prev <> nil) then
  89. PElem.Prev.Next := PElem;
  90. while (Tail.Prev <> nil) do
  91. Tail := Tail.Prev;
  92. end;
  93. end;
  94. end;
  95.  
  96. function TQueue.Get: TClient;
  97. var
  98. Buff: PQueueElem;
  99. begin
  100. if ((Head <> nil) and (Head.Data.IsInEnterState)) then
  101. begin
  102. Buff := Head.Prev;
  103. while ((Buff <> nil) and (Buff.Data.IsInEnterState)) do
  104. Buff := Buff.Prev;
  105. if (Buff <> nil) then
  106. begin
  107. Buff.Next.Prev := Buff.Prev;
  108. if (Buff.Prev <> nil) then
  109. Buff.Prev.Next := Buff.Next
  110. else
  111. Tail := Buff.Next;
  112. end;
  113. end
  114. else
  115. begin
  116. Buff := Head;
  117. if (Head <> nil) then
  118. begin
  119. Head := Head.Prev;
  120. if (Head <> nil) then
  121. Head.Next := nil;
  122. end;
  123. end;
  124. if Buff <> nil then
  125. Result := Buff.Data
  126. else
  127. Result := nil;
  128. end;
  129.  
  130. function TQueue.IsEmpty: Boolean;
  131. begin
  132. Result := Head = nil;
  133. end;
  134.  
  135. function Tick(var Arr: TClientArr; var Que: TQueue;
  136. CurrTickTime, CurrEnterTime: Integer): Integer;
  137. var
  138. CurrClient: TClient;
  139. UselesTick, I: Integer;
  140. begin
  141. UselesTick := 0;
  142. CurrClient := Que.Get();
  143. if CurrClient <> nil then
  144. begin
  145. with CurrClient do
  146. begin
  147. TaskArr[CurrInd] := TaskArr[CurrInd] - CurrTickTime;
  148. if (TaskArr[CurrInd] <= 0) then
  149. begin
  150. UselesTick := (-1) * TaskArr[CurrInd];
  151. EnterTime := CurrEnterTime;
  152. EnterTime := EnterTime - UselesTick;
  153. Inc(CurrInd);
  154. end;
  155. if (CurrInd < Length(TaskArr)) then
  156. begin
  157. Que.Add(CurrClient);
  158. end;
  159. end;
  160. end
  161. else
  162. UselesTick := CurrTickTime;
  163.  
  164. for I := Low(Arr) to High(Arr) do
  165. begin
  166. if (Arr[I] <> CurrClient) and (Arr[I].IsInEnterState) then
  167. Arr[I].EnterTime := Arr[I].EnterTime - CurrTickTime;
  168. end;
  169.  
  170. Result := UselesTick;
  171. end;
  172.  
  173. function Emulate(TickCount, EnterTime: Integer;
  174. var TotalUselesTick: Integer): Integer;
  175. var
  176. Que: TQueue;
  177. Tacts, I: Integer;
  178. ClientArr: TClientArr;
  179. const
  180. Arr1: TTimeArr = [3, 2, 1, 5, 4, 3, 5, 6, 8];
  181. Arr2: TTimeArr = [9, 2, 9, 4, 6, 8, 5, 7, 4];
  182. Arr3: TTimeArr = [5, 6, 9, 7, 7, 4, 3, 2, 1];
  183. Arr4: TTimeArr = [1, 2, 3, 3, 4, 6, 8, 9, 7];
  184. Arr5: TTimeArr = [5, 6, 4, 3, 5, 4, 3, 7, 9];
  185. Arr6: TTimeArr = [9, 6, 8, 2, 7, 1, 9, 7, 4];
  186. begin
  187. TotalUselesTick := 0;
  188. Tacts := 0;
  189. ClientArr[0] := TClient.Create(Arr1, 1);
  190. ClientArr[1] := TClient.Create(Arr2, 1);
  191. ClientArr[2] := TClient.Create(Arr3, 1);
  192. ClientArr[3] := TClient.Create(Arr4, 2);
  193. ClientArr[4] := TClient.Create(Arr5, 3);
  194. ClientArr[5] := TClient.Create(Arr5, 3);
  195. Que := TQueue.Create();
  196. for I := Low(ClientArr) to High(ClientArr) do
  197. Que.Add(ClientArr[I]);
  198.  
  199. while (not Que.IsEmpty) do
  200. begin
  201. Inc(Tacts);
  202. TotalUselesTick := TotalUselesTick + Tick(ClientArr, Que, TickCount, EnterTime);
  203. end;
  204. Result := Tacts;
  205. end;
  206.  
  207. var
  208. TickCount, EnterTime, CurrTacts, CurrUseles, I: Integer;
  209.  
  210. begin
  211. WriteLn('|':5);
  212. for EnterTime := 0 to 10 do
  213. Write(EnterTime:2, '|':7);
  214. WriteLn;
  215.  
  216. for TickCount := 1 to 10 do
  217. begin
  218. Write(TickCount:2, '|':3);
  219. for EnterTime := 0 to 10 do
  220. begin
  221. CurrTacts := Emulate(TickCount, EnterTime, CurrUseles);
  222. Write(CurrUseles:3, '/':1, ((CurrTacts * TickCount - CurrUseles) / (CurrTacts * TickCount)):1:2, '|':1);
  223. end;
  224. WriteLn;
  225. for I := 0 to 103 do
  226. Write('-');
  227. WriteLn;
  228. end;
  229. ReadLn;
  230. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement