Advertisement
Guest User

Untitled

a guest
May 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.29 KB | None | 0 0
  1. with Ada.Text_IO, Ada.Numerics.Float_Random;
  2. use Ada.Text_IO, Ada.Numerics.Float_Random;
  3.  
  4. procedure Traffic is
  5.     protected Multi_printer is
  6.         function Generate_random_number return Float;
  7.         procedure Write(S: in String);
  8.     end Multi_printer;
  9.    
  10.     protected body Multi_printer is
  11.         function Generate_random_number return Float is
  12.         gen : Generator;
  13.         begin
  14.             Reset(gen);
  15.             return Random(gen);
  16.         end generate_random_number;
  17.    
  18.         procedure Write(S: in String) is
  19.         begin
  20.             Put_Line(S);
  21.         end;
  22.     end Multi_printer;
  23.    
  24.     task Crossroads is
  25.         entry Cross(T: in Duration);
  26.         entry Wake_Up;
  27.     end Crossroads;
  28.     type Lamp_color is (Piros,PirosSarga,Zold,Sarga);
  29.     task Lamp is
  30.         entry Switch;
  31.         entry GetColor(Lampc: out Lamp_color);
  32.     end Lamp;
  33.     task body Lamp is
  34.     LC: Lamp_color := Piros;
  35.         task Agens is
  36.             entry Color_Changed;
  37.         end Agens;
  38.         task body Agens is
  39.         begin
  40.             accept Color_Changed
  41.             do
  42.                 Crossroads.Wake_Up;
  43.             end Color_Changed;
  44.         end Agens;
  45.     begin
  46.         loop
  47.             select
  48.                 accept Switch
  49.                 do
  50.                     case(LC) is
  51.                         when Piros          => LC := PirosSarga;
  52.                         when PirosSarga     => LC := Zold;         
  53.                         when Zold           => LC := Sarga;
  54.                         when Sarga          => LC := Piros;
  55.                     end case;
  56.                     Multi_printer.Write(LC'Image);
  57.                     Agens.Color_Changed;
  58.                 end Switch;
  59.             or
  60.                 accept GetColor(Lampc: out Lamp_color)
  61.                 do
  62.                     Lampc := LC;
  63.                 end GetColor;
  64.             or
  65.                 terminate;
  66.             end select;
  67.         end loop;
  68.     end Lamp;
  69.     task Controller;
  70.     task body Controller is
  71.     begin
  72.         loop
  73.             delay 3.0;
  74.             Lamp.Switch;
  75.             delay 1.0;
  76.             Lamp.Switch;
  77.             delay 3.0;
  78.             Lamp.Switch;
  79.             delay 2.0;
  80.             Lamp.Switch;
  81.         end loop;
  82.     end Controller;
  83.     task body Crossroads is
  84.     LC: Lamp_color;
  85.     begin  
  86.         loop
  87.         Lamp.GetColor(LC);
  88.             select
  89.                 when LC = Zold => accept Cross(T: in Duration)
  90.                 do
  91.                     delay(T);
  92.                 end Cross;
  93.             or
  94.                 accept Wake_Up
  95.                 do
  96.                     null;
  97.                 end Wake_Up;
  98.             end select;
  99.         end loop;
  100.     end;
  101.     type PString is access String;
  102.     task type Vehicle(RegNum : PString);
  103.     type Vehicle_Access is access Vehicle;
  104.     P : Vehicle_Access;
  105.     task body Vehicle is
  106.     LC : Lamp_color;
  107.     Waiting : Boolean := True;
  108.     Time: Duration := Duration(Float'Remainder(Multi_printer.Generate_random_number,1.0)) + 0.5;
  109.     begin
  110.         Put_Line(RegNum.all);
  111.         while Waiting loop
  112.             Lamp.GetColor(LC);
  113.             if  LC = Zold then
  114.                 Crossroads.Cross(Time);
  115.                 Put_Line(RegNum.all);
  116.                 Waiting := False;
  117.             else
  118.                 Time := Duration(Float'Remainder(Multi_printer.Generate_random_number,1.0)) + 2.5;
  119.                 delay 0.2;
  120.             end if;
  121.         end loop;
  122.     end Vehicle;
  123.     Auto : PString;
  124.  
  125. begin
  126.     Auto := new String'("EZR231");
  127.     P := new Vehicle(Auto);
  128.     delay 0.5;
  129.     Auto := new String'("HGK831");
  130.     P := new Vehicle(Auto);
  131.     delay 0.5;
  132.     Auto := new String'("SLE391");
  133.     P := new Vehicle(Auto);
  134.     delay 0.5;
  135.     Auto := new String'("REW222");
  136.     P := new Vehicle(Auto);
  137.     delay 0.5;
  138.     Auto := new String'("UYR494");
  139.     P := new Vehicle(Auto);
  140.     delay 0.5;
  141.     Auto := new String'("WQQ121");
  142.     P := new Vehicle(Auto);
  143.     delay 0.5;
  144.     Auto := new String'("BNM367");
  145.     P := new Vehicle(Auto);
  146.     delay 0.5;
  147.     Auto := new String'("LKJ122");
  148.     P := new Vehicle(Auto);
  149.     delay 0.5;
  150.     Auto := new String'("PYO345");
  151.     P := new Vehicle(Auto);
  152.     delay 0.5;
  153.     Auto := new String'("DFG112");
  154.     P := new Vehicle(Auto);
  155. end Traffic;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement