Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 1.96 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.     Passed_Over : Boolean := False;
  86. begin
  87.         Put_Line(N.all & " Car Is Waiting At The Lamp");
  88.     select
  89.         Crossroad.Cross(1.0);
  90.     else
  91.         Crossroad.Cross(3.0);
  92.     end select;
  93.         Put_Line(N.all & " Car Passed Over");
  94. end Vehicle;
  95.  
  96.     type PVehicle is access Vehicle;
  97.     type VehicleAr is array (1..30) of PVehicle;
  98.  
  99.     Vehicles : VehicleAr;
  100.     P : Pstring;
  101. begin
  102.     for I in 1..30 loop
  103.         P := new String'(Integer'Image(I));
  104.         delay 0.5;
  105.         Vehicles(I) := new Vehicle(P);
  106.     end loop;
  107.     Skip_Line;
  108.     Controller.Stop;
  109. end Traffic;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement