Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 1.93 KB | None | 0 0
  1. with Ada.Text_IO;
  2. use Ada.Text_IO;
  3.  
  4. procedure Traffic is
  5.     type Lamp is (Piros,PirosSarga,Zold,Sarga);
  6.     type Pstring is access String;
  7.  
  8. protected Crossroad is
  9.     entry Cross(D : Duration);
  10.     procedure Wake_Up;
  11. end Crossroad;
  12. task type Signal;
  13.  
  14. task body Signal is
  15. begin
  16.     Crossroad.Wake_Up;
  17. end Signal;
  18.    
  19.     type PSignal is access Signal;
  20.    
  21. protected Lamps is
  22.     function Color return Lamp;
  23.     procedure Switch;
  24. private
  25.     ActColor : Lamp := Piros;
  26.     LSignal : PSignal;
  27. end Lamps;
  28.  
  29. protected body Lamps is
  30.     function Color return Lamp is
  31.     begin
  32.         return ActColor;
  33.     end Color;
  34.     procedure Switch is
  35.     begin
  36.         LSignal := new Signal;
  37.         if ActColor = Sarga then
  38.             ActColor := Piros;
  39.             Put_Line(Lamp'Image(ActColor));
  40.         else
  41.             ActColor := Lamp'Succ(ActColor);
  42.             Put_Line(Lamp'Image(ActColor));
  43.         end if;
  44.     end Switch;
  45. end Lamps;
  46.  
  47. task Controller is
  48.     entry Stop;
  49. end Controller;
  50.  
  51. task body Controller is
  52.     Stopped : Boolean := False;
  53. begin
  54.     while not Stopped loop
  55.         select
  56.             accept Stop;
  57.             Stopped := True;
  58.         else
  59.             Lamps.Switch;
  60.             delay 3.0;
  61.             Lamps.Switch;
  62.             delay 1.0;
  63.             Lamps.Switch;
  64.             delay 3.0;
  65.             Lamps.Switch;
  66.             delay 2.0;
  67.         end select;
  68.     end loop;
  69. end Controller;
  70.  
  71. protected body Crossroad is
  72.     entry Cross(D : Duration) when Lamps.Color = Zold is
  73.     begin
  74.         delay D;
  75.     end Cross;
  76.     procedure Wake_Up is
  77.     begin
  78.         null;
  79.     end Wake_Up;
  80. end Crossroad;
  81.  
  82. task type Vehicle(N : Pstring);
  83.  
  84. task body Vehicle is
  85. begin
  86.         Put_Line(N.all & " Car Is Waiting At The Lamp");
  87.     select
  88.         Crossroad.Cross(1.0);
  89.     else
  90.         Crossroad.Cross(3.0);
  91.     end select;
  92.         Put_Line(N.all & " Car Passed Over");
  93. end Vehicle;
  94.  
  95.     type PVehicle is access Vehicle;
  96.     type VehicleAr is array (1..30) of PVehicle;
  97.  
  98.     Vehicles : VehicleAr;
  99.     P : Pstring;
  100. begin
  101.     for I in 1..30 loop
  102.         P := new String'(Integer'Image(I));
  103.         delay 0.5;
  104.         Vehicles(I) := new Vehicle(P);
  105.     end loop;
  106.     Skip_Line;
  107.     Controller.Stop;
  108. end Traffic;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement