Advertisement
MadCortez

Untitled

Nov 10th, 2021
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.08 KB | None | 0 0
  1. unit Unit_Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Math,
  8.   Vcl.Samples.Spin;
  9.  
  10. type
  11.     TArrStr = Array of String;
  12.  
  13.     TPClient = ^TClient;
  14.  
  15.     TClient = Record
  16.         TaskLengths: Array of Integer;
  17.         TaskToDo, RemainingTimeToThink: Integer;
  18.     End;
  19.  
  20.     TLineOfSamePriority = Record
  21.         Clients: Array of TPClient;
  22.         ClientToWorkWith: Integer;
  23.     End;
  24.  
  25.     TClientBase = Array of TLineOfSamePriority;
  26.  
  27.  
  28.   TForm1 = class(TForm)
  29.     LabelTickLength: TLabel;
  30.     LabelThinkingTime: TLabel;
  31.     Memo1: TMemo;
  32.     ButtonRun: TButton;
  33.     ListBox1: TListBox;
  34.     LabelAnswer: TLabel;
  35.     SpinEdit1: TSpinEdit;
  36.     SpinEdit2: TSpinEdit;
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure ButtonRunClick(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     procedure DrawTick(Pri, CliNumInPri, PointsMade: Integer);
  43.     function GetRealNumFromPriorityAndClientNum(Pri, CliNumInPri: Integer) : Integer;
  44.     procedure DoTick();
  45.     function CheckIfClientCanWork(Prior, ClientNum: Integer): Boolean;
  46.     procedure SubstractWaitingTime(Pri, CliNumInPri, PointsMade: Integer);
  47.   end;
  48.  
  49. procedure MyMessageBoxInfo(Form: TForm; CaptionWindow, TextMessage: String; IsWarning: Boolean = False); external 'Dll_MyMessageBox.dll';
  50. function FindRegEx(SInput, StrRegEx: String; StrIfNothingFound: String = '')
  51.   : TArrStr; external 'FindRegExes.dll';
  52.  
  53. var
  54.   Form1: TForm1;
  55.   AllClients: TClientBase;
  56.   ThinkingTime, TickLen, QuantityOfClients, QuantityOfTicks, PointsDoneByProcessor: Integer;
  57.  
  58. implementation
  59.  
  60. {$R *.dfm}
  61.  
  62. function TForm1.CheckIfClientCanWork(Prior, ClientNum: Integer): Boolean;
  63. var
  64.     Client: TPClient;
  65.  
  66. begin
  67.     Client := AllClients[Prior].Clients[ClientNum];
  68.     Result := (Client.TaskToDo <> -1)
  69.         and (Client.RemainingTimeToThink = 0);
  70. end;
  71.  
  72. function TForm1.GetRealNumFromPriorityAndClientNum(Pri, CliNumInPri: Integer) : Integer;
  73. var
  74.     RealNumOfClient, i: Integer;
  75.  
  76. begin
  77.     RealNumOfClient := 0;
  78.     for i := 0 to Pri - 1 do
  79.         RealNumOfClient := RealNumOfClient + Length(AllClients[i].Clients);
  80.     Result := RealNumOfClient + CliNumInPri;
  81. end;
  82.  
  83. procedure TForm1.DrawTick(Pri, CliNumInPri, PointsMade: Integer);
  84. var
  85.     RealNumOfWorking, RealNumTemp, i, j, k, TimeInThoughts: Integer;
  86.     Cl: TPClient;
  87.  
  88. begin
  89.     if (Pri > -1) and (CliNumInPri > -1) and (PointsMade > -1) then
  90.     begin
  91.         RealNumOfWorking := GetRealNumFromPriorityAndClientNum(Pri, CliNumInPri);
  92.         for I := 1 to PointsMade do
  93.             ListBox1.Items[RealNumOfWorking] := ListBox1.Items[RealNumOfWorking] + IntToStr(AllClients[Pri].Clients[CliNumInPri].TaskToDo + 1);
  94.         TimeInThoughts := Min(TickLen - PointsMade, ThinkingTime);
  95.         for I := 1 to TimeInThoughts do
  96.             ListBox1.Items[RealNumOfWorking] := ListBox1.Items[RealNumOfWorking] + '*';
  97.         for I := PointsMade + TimeInThoughts + 1 to TickLen do
  98.             ListBox1.Items[RealNumOfWorking] := ListBox1.Items[RealNumOfWorking] + '_';
  99.         ListBox1.Items[RealNumOfWorking] := ListBox1.Items[RealNumOfWorking] + '|';
  100.     end
  101.     else
  102.         RealNumOfWorking := -1;
  103.  
  104.     for I := Low(AllClients) to High(AllClients) do
  105.         for j := Low(AllClients[i].Clients) to High(AllClients[i].Clients) do
  106.         begin
  107.             Cl := AllClients[i].Clients[j];
  108.             RealNumTemp := GetRealNumFromPriorityAndClientNum(i, j);
  109.             if RealNumTemp <> RealNumOfWorking then
  110.             begin
  111.                 TimeInThoughts := Min(Min(cl.RemainingTimeToThink, ThinkingTime), TickLen);
  112.                 for k := 1 to TimeInThoughts do
  113.                     ListBox1.Items[RealNumTemp] := ListBox1.Items[RealNumTemp] + '*';
  114.                 for k := TimeInThoughts + 1 to TickLen do
  115.                     ListBox1.Items[RealNumTemp] := ListBox1.Items[RealNumTemp] + '_';
  116.                 ListBox1.Items[RealNumTemp] := ListBox1.Items[RealNumTemp] + '|';
  117.             end;
  118.         end;
  119. end;
  120.  
  121. procedure TForm1.SubstractWaitingTime(Pri, CliNumInPri, PointsMade: Integer);
  122. var
  123.     i, j: Integer;
  124.     Cl: TPClient;
  125.  
  126. begin
  127.     for I := Low(AllClients) to High(AllClients) do
  128.         for j := Low(AllClients[i].Clients) to High(AllClients[i].Clients) do
  129.         begin
  130.             Cl := AllClients[i].Clients[j];
  131.             if (Pri = i) and (CliNumInPri = j) then
  132.             begin
  133.                 if cl.TaskLengths[cl.TaskToDo] = 0 then
  134.                     Cl.RemainingTimeToThink := Max(ThinkingTime - TickLen + PointsMade, 0)
  135.             end
  136.             else
  137.             begin
  138.                 if Cl.RemainingTimeToThink > 0 then
  139.                     Cl.RemainingTimeToThink := Max(0, Cl.RemainingTimeToThink - TickLen);
  140.             end;
  141.         end;
  142. end;
  143.  
  144. procedure TForm1.DoTick();
  145. var
  146.     Pri, Cli_In_Pri, PointsDoneWithCli, j: Integer;
  147.     ClientToWorkWithIsNotFound, NoneToWorkInThisPri, NotGivenOrderToNextInThisPri: Boolean;
  148.     Cli: TPClient;
  149.  
  150. begin
  151.     Inc(QuantityOfTicks);
  152.     ClientToWorkWithIsNotFound := True;
  153.     Pri := Low(AllClients);
  154.  
  155.     while (Pri <= High(AllClients)) and ClientToWorkWithIsNotFound do
  156.     begin
  157.         if AllClients[Pri].ClientToWorkWith <> -1 then
  158.         begin
  159.             Cli_In_Pri := AllClients[Pri].ClientToWorkWith;
  160.             while (Cli_In_Pri <= High(AllClients[Pri].Clients))
  161.                     and ClientToWorkWithIsNotFound do
  162.             begin
  163.                 ClientToWorkWithIsNotFound := not CheckIfClientCanWork(Pri, Cli_In_Pri);
  164.                 Inc(Cli_In_Pri);
  165.             end;
  166.  
  167.             if ClientToWorkWithIsNotFound then
  168.                 Cli_In_Pri := 0;
  169.  
  170.             while (Cli_In_Pri < AllClients[Pri].ClientToWorkWith)
  171.                     and ClientToWorkWithIsNotFound do
  172.             begin
  173.                 ClientToWorkWithIsNotFound := not CheckIfClientCanWork(Pri, Cli_In_Pri);
  174.                 Inc(Cli_In_Pri);
  175.             end;
  176.         end;
  177.  
  178.         Inc(Pri);
  179.     end;
  180.  
  181.     if not ClientToWorkWithIsNotFound then
  182.     begin
  183.         Dec(Pri);
  184.         Dec(Cli_In_Pri);
  185.         Cli := AllClients[Pri].Clients[Cli_In_Pri];
  186.         PointsDoneWithCli := Min(Cli.TaskLengths[Cli.TaskToDo], TickLen);
  187.         Cli.TaskLengths[Cli.TaskToDo] := Cli.TaskLengths[Cli.TaskToDo] - PointsDoneWithCli;
  188.  
  189.         // Draw and Wait
  190.         Form1.DrawTick(Pri, Cli_In_Pri, PointsDoneWithCli);
  191.         SubstractWaitingTime(Pri, Cli_In_Pri, PointsDoneWithCli);
  192.         PointsDoneByProcessor := PointsDoneByProcessor + PointsDoneWithCli;
  193.  
  194.         NoneToWorkInThisPri := False;
  195.         if Cli.TaskLengths[Cli.TaskToDo] = 0 then
  196.         begin
  197.             Inc(Cli.TaskToDo);
  198.             if Cli.TaskToDo > High(Cli.TaskLengths) then
  199.             begin
  200.                 Cli.TaskToDo := -1;
  201.                 NoneToWorkInThisPri := True;
  202.                 j := Low(AllClients[Pri].Clients);
  203.                 while (j <= High(AllClients[Pri].Clients))
  204.                         and NoneToWorkInThisPri do
  205.                 begin
  206.                     NoneToWorkInThisPri := AllClients[Pri].Clients[j].TaskToDo = -1;
  207.                     Inc(j);
  208.                 end;
  209.                 if NoneToWorkInThisPri then
  210.                     AllClients[Pri].ClientToWorkWith := -1;
  211.             end;
  212.         end;
  213.  
  214.         if not NoneToWorkInThisPri then
  215.         begin
  216.             AllClients[Pri].ClientToWorkWith := Cli_In_Pri + 1;
  217.             if AllClients[Pri].ClientToWorkWith > High(AllClients[Pri].Clients) then
  218.                 AllClients[Pri].ClientToWorkWith := 0;
  219.             NotGivenOrderToNextInThisPri := False;
  220.         end;
  221.     end
  222.     else
  223.     begin
  224.         Form1.DrawTick(-1, -1, -1);
  225.         SubstractWaitingTime(-1, -1, -1);
  226.     end;
  227. end;
  228.  
  229. procedure TForm1.ButtonRunClick(Sender: TObject);
  230. var
  231.     ThereIsNothingToDo: Boolean;
  232.     i: Integer;
  233.     PointsWastes: Integer;
  234.  
  235. begin
  236.     ButtonRun.Enabled := False;
  237.  
  238.     TickLen := SpinEdit1.Value;
  239.     ThinkingTime := SpinEdit2.Value;
  240.  
  241.     repeat
  242.         DoTick();
  243.  
  244.         ThereIsNothingToDo := True;
  245.         i := 0;
  246.         while ThereIsNothingToDo and (i < Length(AllClients)) do
  247.         begin
  248.             ThereIsNothingToDo := AllClients[i].ClientToWorkWith = -1;
  249.             Inc(i);
  250.         end;
  251.     until ThereIsNothingToDo;
  252.  
  253.     PointsWastes := TickLen * QuantityOfTicks;
  254.     LabelAnswer.Caption := LabelAnswer.Caption + IntToStr(PointsDoneByProcessor) + ' / ' + IntToStr(PointsWastes);
  255.     if PointsDoneByProcessor/PointsWastes < 1 then
  256.         LabelAnswer.Caption := LabelAnswer.Caption + ' = 0.' + IntToStr(Trunc(1000 * PointsDoneByProcessor/PointsWastes))
  257.     else
  258.         LabelAnswer.Caption := LabelAnswer.Caption + ' = 1';
  259.  
  260. end;
  261.  
  262. procedure TForm1.FormCreate(Sender: TObject);
  263. var
  264.     i, j, k, realNumOfClient: Integer;
  265.     StrArr1, StrArr2: TArrStr;
  266.  
  267. begin
  268.     SetLength(AllClients, StrToInt(FindRegEx(Memo1.Lines[0], '\d+')[0]));
  269.     StrArr1 := FindRegEx(Memo1.Lines[1], '\d+');
  270.     RealNumOfClient := 0;
  271.     ThinkingTime := 1;
  272.     TickLen := 1;
  273.     QuantityOfTicks := 0;
  274.     PointsDoneByProcessor := 0;
  275.  
  276.     for I := Low(AllClients) to High(AllClients) do
  277.     begin
  278.         SetLength(AllClients[i].Clients, StrToInt(StrArr1[i]));
  279.  
  280.         AllClients[i].ClientToWorkWith := 0;
  281.  
  282.         for J := Low(AllClients[i].Clients) to High(AllClients[i].Clients) do
  283.         begin
  284.             New(AllClients[i].Clients[j]);
  285.  
  286.             AllClients[i].Clients[j].TaskToDo := 0;
  287.             AllClients[i].Clients[j].RemainingTimeToThink := 0;
  288.  
  289.             StrArr2 := FindRegEx(Memo1.Lines[RealNumOfClient + 2], '\d+');
  290.             SetLength(AllClients[i].Clients[j].TaskLengths, Length(StrArr2));
  291.             for k := Low(AllClients[i].Clients[j].TaskLengths)
  292.                to High(AllClients[i].Clients[j].TaskLengths) do
  293.                 AllClients[i].Clients[j].TaskLengths[k] := StrToInt(StrArr2[k]);
  294.  
  295.             ListBox1.Items.Add(' ' + IntToStr(RealNumOfClient + 1) + ': ');
  296.             Inc(RealNumOfClient);
  297.         end;
  298.     end;
  299.  
  300.     QuantityOfClients := realNumOfClient;
  301. end;
  302.  
  303. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement