Advertisement
Guest User

Untitled

a guest
Apr 1st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 7.08 KB | None | 0 0
  1. with Ada.Text_IO;
  2. with Ada.Numerics.Discrete_Random;
  3. with Const;
  4.  
  5. use Ada.Text_IO;
  6.  
  7. procedure Main is
  8.    type Sign is ('+', '-', '*');
  9.    
  10.    type Job is
  11.       record
  12.          first: Integer;
  13.          operation: Sign;
  14.          second: Integer;
  15.    end record;
  16.  
  17.    task type CompanyPresident;
  18.  
  19.    task type Employee is
  20.  
  21.    task type Customer is
  22.  
  23.    task Scheduler is
  24.       entry newJob (AJob: in Job);
  25.       entry GetJob (AJob: out Job);
  26.       entry GetState (mode: in Boolean);
  27.    end Scheduler;
  28.  
  29.    task Store is
  30.       entry newProduct (AnItem: in Integer);
  31.       entry buyProduct (AnItem: out Integer);
  32.       entry GetState (mode: in Boolean);
  33.    end Store;
  34.  
  35.    task body CompanyPresident is
  36.       Operations: array (0..2) of Sign := ('+', '-', '*');
  37.       AJob: Job;
  38.       subtype RangeNumbers is Integer range 1..1000;
  39.       subtype RangeOperations is Integer range 0..2;
  40.       package RNum is new Ada.Numerics.Discrete_Random(RangeNumbers);
  41.       package ROp is new Ada.Numerics.Discrete_Random(RangeOperations);
  42.       GNum: RNum.Generator;
  43.       GOp: ROp.Generator;
  44.    begin
  45.       presidentLoop:
  46.       loop
  47.          AJob := (RNum.Random(GNum), Operations(ROp.Random(GOp)), RNum.Random(GNum));
  48.          Scheduler.NewJob(AJob);
  49.          if Const.Mode then
  50.             Put_Line("Boss adds" & Integer'Image(AJob.first) & " " & Sign'Image(Ajob.operation) & Integer'Image(AJob.second) & " to tasks list.");
  51.          end if;
  52.          delay Const.CompanyPresidentSpeed;
  53.       end loop presidentLoop;
  54.    end CompanyPresident;
  55.  
  56.    task body Employee is
  57.       Identifier : Integer;
  58.       CurrentJob: Job;
  59.       Result: Integer;
  60.    begin
  61.       accept Id (employeeId: in Integer) do
  62.          Identifier := employeeId;
  63.       end Id;
  64.       endlessLoop:
  65.       loop
  66.          Scheduler.GetJob(CurrentJob);
  67.          case CurrentJob.operation is
  68.             when '+' =>
  69.                Result := CurrentJob.first + CurrentJob.second;
  70.             when '-' =>
  71.                Result := CurrentJob.first - CurrentJob.second;
  72.             when '*' =>
  73.                Result := CurrentJob.first * CurrentJob.second;
  74.          end case;
  75.          if Const.Mode then
  76.             Put_Line("worker" & Integer'Image(Identifier) & " produces " & Integer'Image(Result));
  77.          end if;
  78.          Store.newProduct(Result);
  79.          delay Const.EmployeeSpeed;
  80.       end loop endlessLoop;
  81.    end Employee;
  82.    
  83.    task body Customer is
  84.       identifier : Integer;
  85.       Bought: Integer;
  86.    begin
  87.       accept Id (customerId: in Integer) do
  88.          identifier := customerId;
  89.       end Id;
  90.       endlessLoop:
  91.       loop
  92.          Store.buyProduct(Bought);
  93.          if Const.Mode then
  94.             Put_Line("client" & Integer'image(identifier) & " bought " & Integer'image(bought));
  95.          end if;
  96.          delay Const.CustomerSpeed;
  97.       end loop endlessLoop;
  98.    end Customer;
  99.  
  100.    task body Scheduler is
  101.       Jobs : array(0..Const.ToDoSize) of Job;
  102.       JobsCount: Integer := 0;
  103.       Head: Integer := 0;
  104.       Tail: Integer := 0;
  105.    begin
  106.       endlessLoop:
  107.       loop
  108.          select
  109.             when JobsCount < Const.ToDoSize =>
  110.                accept NewJob(AJob: in Job) do
  111.                   Jobs(Tail) := AJob;
  112.                end NewJob;
  113.                Tail := (Tail+1) mod Const.StoreSize;
  114.                JobsCount := JobsCount + 1;
  115.          or when JobsCount > 0 =>
  116.                accept GetJob(AJob: out Job) do
  117.                   AJob := Jobs(Head);
  118.                end GetJob;
  119.                Head := (Head + 1) mod Const.StoreSize;
  120.                JobsCount := JobsCount - 1;
  121.          or accept GetState(mode: in Boolean) do
  122.                Put_Line("Bot> Lista zada:");
  123.                Put("{ ");
  124.                for I in Jobs'Range loop
  125.                   if Jobs(I).first = 0 then
  126.                      Put("()");
  127.                   else
  128.                      Put("(" & Integer'Image(Jobs(I).first) & Sign'Image(Jobs(I).operation) & Integer'Image(Jobs(I).second) & " )");
  129.                   end if;
  130.                end loop;
  131.                Put(" }");
  132.                New_Line;
  133.             end GetState;
  134.          end select;
  135.       end loop endlessLoop;
  136.    end Scheduler;
  137.  
  138.    task body Store is
  139.       Items: array (0..Const.StoreSize) of Integer;
  140.       ItemsCount: Integer := 0;
  141.       Head: Integer := 0;
  142.       Tail: Integer := 0;
  143.    begin
  144.       endlessLoop:
  145.       loop
  146.          select
  147.             when ItemsCount < Const.StoreSize =>
  148.                accept newProduct(AnItem: in Integer) do
  149.                   Items(Tail) := AnItem;
  150.                end newProduct;
  151.                Tail := (Tail+1) mod Const.StoreSize;
  152.                ItemsCount := ItemsCount+1;
  153.          or when ItemsCount > 0 =>
  154.                accept buyProduct (AnItem: out Integer) do
  155.                   AnItem := Items(Head);
  156.                end buyProduct;
  157.                Head := (Head + 1) mod Const.StoreSize;
  158.                ItemsCount := ItemsCount-1;
  159.          or accept GetState(mode: in Boolean) do
  160.                Put_Line("Bot> Stan magazynu:");
  161.                Put("{ ");
  162.                for I in Items'Range loop
  163.                   Put("(" & Integer'Image(Items(I)) & " )");
  164.                end loop;
  165.                Put(" }");
  166.                New_Line;
  167.             end GetState;
  168.          end select;
  169.       end loop endlessLoop;
  170.    end Store;
  171.  
  172.    p : CompanyPresident;
  173.    w : array (0 .. Const.EmployeeNumber) of Employee;
  174.    c : array (0 .. Const.CustomerNumber) of Customer;
  175.    
  176.    input: String(1..80);
  177.    last: Natural;
  178. begin
  179.    for I in w'Range loop
  180.       w(I).Id(I);
  181.    end loop;
  182.    
  183.    for I in c'Range loop
  184.       c(I).Id(I);
  185.    end loop;
  186.    
  187.    if not Const.Mode then
  188.       Put_Line("Witaj w naszej firmie, co chcialbys wiedziec?");
  189.       Put_Line("1 - Liczba pracownikow");
  190.       Put_Line("2 - Ilosc rzeczy do zrobienia");
  191.       Put_Line("3 - Lista zadan");
  192.       Put_Line("4 - Liczba klientow");
  193.       Put_Line("5 - Rozmiar magazynu");
  194.       Put_Line("6 - Stan magazynu");
  195.       loop
  196.          Get_Line(input, last);
  197.          case input(1) is
  198.             when '1' =>
  199.                Put_Line("Bot> Liczba pracownikow:" & Integer'Image(Const.EmployeeNumber));
  200.             when '2' =>
  201.                Put_Line("Bot> Ilosc rzeczy do zrobienia:" & Integer'Image(Const.ToDoSize));
  202.             when '3' =>
  203.                Scheduler.GetState(True);
  204.             when '4' =>
  205.                Put_Line("Bot> Liczba klientow:" & Integer'Image(Const.CustomerNumber));
  206.             when '5' =>
  207.                Put_Line("Bot> Rozmiar magazynu:" & Integer'Image(Const.StoreSize));
  208.             when '6' =>
  209.                Store.GetState(True);
  210.             when others =>
  211.                Put_Line("Witaj w naszej firmie, co chcialbys wiedziec?");
  212.                Put_Line("1 - Liczba pracownikow");
  213.                Put_Line("2 - Ilosc rzeczy do zrobienia");
  214.                Put_Line("3 - Lista zadan");
  215.                Put_Line("4 - Liczba klientow");
  216.                Put_Line("5 - Rozmiar magazynu");
  217.                Put_Line("6 - Stan magazynu");
  218.          end case;
  219.       end loop;
  220.    end if;
  221. end Main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement