Guest User

Ada minta ZH

a guest
Apr 28th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.24 KB | None | 0 0
  1. with Ada.Text_IO; use Ada.Text_IO; with Ada.Numerics.Discrete_Random;
  2.  
  3. procedure lampa is
  4.  
  5.    type lampaszinek is (piros,sarga,pirossarga,zold);
  6.    type iranyok is (egyenesen_megy, fordul);
  7.    package Random_Irany is new Ada.Numerics.Discrete_Random(iranyok);
  8.    
  9.    task type auto is
  10.       entry setVar( name: in string := ""; time : in duration := 0.0); --afféle konstruktor
  11.    end auto;
  12.    type Pauto is access auto;
  13.    
  14.    protected veletlenGyarto is
  15.       function gyarto return iranyok;
  16.    end veletlenGyarto;
  17.    
  18.    protected body veletlenGyarto is
  19.       function gyarto return iranyok is
  20.          seed : Random_Irany.Generator;
  21.          irany : iranyok;
  22.       begin
  23.          Random_Irany.Reset(seed);
  24.          irany := Random_Irany.Random(seed);
  25.          return irany;
  26.       end gyarto;
  27.    end veletlenGyarto;
  28.    
  29.    protected jelzolampa is
  30.       entry athalad(rendszam : in string);
  31.       procedure valt;
  32.       procedure kiir;
  33.       function szin return lampaszinek;
  34.    private
  35.       aktszin : lampaszinek := piros;
  36.    end jelzolampa;
  37.  
  38.    protected body jelzolampa is
  39.       function szin return lampaszinek is begin return aktszin; end szin;
  40.       procedure valt is begin aktszin := lampaszinek'Val( (lampaszinek'Pos(aktszin) + 1) mod 4); end valt;
  41.       procedure kiir is begin put_line(lampaszinek'Image(aktszin)); end kiir;
  42.       entry athalad(rendszam : in string) when aktszin = zold is begin put_line(rendszam & " " & iranyok'Image(veletlenGyarto.gyarto) & "."); end athalad;
  43.    end jelzolampa;
  44.  
  45.    task utemezo is
  46.       entry leall;
  47.    end utemezo;
  48.    
  49.    task lampaszerelo;
  50.    task body lampaszerelo is
  51.    begin
  52.       delay 15.0;
  53.       utemezo.leall;
  54.    end lampaszerelo;
  55.      
  56.    task body utemezo is
  57.    begin
  58.       loop
  59.          select
  60.             accept leall do
  61.                put_line("A szimulacio veget ert.");
  62.             end leall;
  63.             exit;
  64.          else
  65.             jelzolampa.valt; jelzolampa.kiir;
  66.             delay 3.0;
  67.             jelzolampa.valt; jelzolampa.kiir;
  68.             delay 0.5;
  69.             jelzolampa.valt; jelzolampa.kiir;
  70.             delay 2.0;
  71.             jelzolampa.valt; jelzolampa.kiir;
  72.             delay 1.0;
  73.          end select;
  74.       end loop;
  75.    end utemezo;
  76.    
  77.    task body auto is
  78.       rendszam : access string;
  79.       menetidoalampaig : access duration;
  80.    begin
  81.       accept setVar( name : in string := ""; time : in duration := 0.0) do
  82.          rendszam := new string'(name);
  83.          menetidoalampaig := new duration'(time);
  84.       end;
  85.       put_line(rendszam.all & " elindult.");
  86.       delay menetidoalampaig.all;
  87.       put_line(rendszam.all & " odaert.");
  88.       loop
  89.          select
  90.             jelzolampa.athalad(rendszam.all);
  91.             exit;
  92.          or
  93.             delay 0.2;
  94.             put_line(rendszam.all & " var.");
  95.          end select;
  96.       end loop;
  97.    end auto;
  98.  
  99.    autok : array (0..2) of Pauto;
  100.    rendszamok : constant array (integer range 0..2) of string (1..7) := ("AAA-111", "BBB-222", "CCC-333");
  101.    varakozasok: constant array (integer range 0..2) of duration := (1.5, 3.3, 7.5);
  102.  
  103. begin
  104.    for I in 0..2 loop
  105.       autok(i) := new auto;
  106.       autok(i).setVar(rendszamok(i), varakozasok(i));
  107.    end loop;
  108. end lampa;
Advertisement
Add Comment
Please, Sign In to add comment