Time Machine

Time Machine - Divers - Programmation

Marsh Posté le 16-02-2025 à 12:57:00    

Bonour,
 
Voici le code de la Time Machine avec Ada.
Le paquetage Ansi est de Hiboux.
 
il permet de saisir 12 chiffres, la date et l'heure "202502161145" pour voyager dans le temps.
 
 
 

Code :
  1. with Ada.Calendar;
  2. with Ada.Calendar.Formatting;
  3. use Ada.Calendar;
  4. with Sky.Ansi.Console;
  5. with Sky.Ansi.Windows;
  6. use Sky.Ansi.Console;
  7. with Ada.Characters.Handling;
  8. use Ada.Characters.Handling;
  9. with Text_Io;
  10. use Text_Io;
  11. procedure Main is
  12.  
  13.  
  14.   package Windows is new Sky.Ansi.Windows(67, 240);
  15.   use Windows;
  16.  
  17.  
  18.   Past_Win : Window_Type := (30, 110, 3, 22, Single_Line_Frame, Green, Green);
  19.   Present_Win : Window_Type := (33, 110, 3, 22, Single_Line_Frame, Yellow, Yellow);
  20.   Future_Win : Window_Type := (36, 110, 3, 22, Single_Line_Frame, Red, Red);
  21.  
  22.  
  23.   Start   : Time := Clock;
  24.   now    : Time := Start;
  25.   Target  : Time := Now + 0.025;
  26.  
  27.  
  28.   subtype Date_Str is String (1..8);
  29.   subtype Hour_str is String (1..4);
  30.  
  31.   Date_Index : Natural := 1;
  32.   Hour_Index : Natural := 1;
  33.  
  34.   type Part_Enum is (Date_part, Hour_part);
  35.  
  36.  
  37.   task Time_Machine is
  38.      entry Init;
  39.      entry Receive(Char : in Character);
  40.      entry Halt;
  41.   end Time_Machine;
  42.  
  43.  
  44.   function Part2date (Date : in Date_Str; Hour : in Hour_Str) return Time is
  45.      Target : Time := Clock;
  46.   begin
  47.      Target := Time_Of(Year_Number'Value(Date(1..4)), Month_Number'Value(date(5..6)), Day_Number'Value(Date(7..8)), Duration'Value(hour(1..2))*3600.0+Duration'Value(Hour(3..4))*60.0);
  48.      return Target;
  49.   end Part2date;
  50.  
  51. task body Time_Machine is
  52.      
  53.      Part : Part_Enum := Date_Part;
  54.      Date : Date_Str := "19700101";
  55.      Hour : Hour_Str := "0000";
  56.      
  57.      Edit : Boolean := False;
  58.      
  59.      Going : Boolean := False;
  60.      Done  : Boolean := False;
  61.      
  62.      
  63.   begin
  64.      Target := Part2date(Date, Hour);
  65.      loop
  66.         select
  67.            accept Init;
  68.            Sky.Ansi.Console.Clear_Screen;
  69.            Draw_Window(Past_win);
  70.            Draw_Window(Present_Win);
  71.            Draw_Window(Future_Win);
  72.            
  73.         or
  74.            when not Going =>
  75.               accept Receive(Char : in Character) do
  76.                  Edit  := True;
  77.                  if Is_Digit(Char) then
  78.                    
  79.                     if Part = Date_part then
  80.                        Date(Date_Index+1) := Char;
  81.                        
  82.                     else
  83.                        Hour(Hour_Index) := Char;
  84.                        
  85.                     end if;
  86.                     if Hour_Index = 4 then
  87.                        Hour_Index := 1;
  88.                        Part := Date_Part;
  89.                        Edit := False;
  90.                        Start := Now;
  91.                        Going := True;
  92.                        Target := Part2date(Date, Hour);
  93.                     elsif Part = Hour_Part then
  94.                        Hour_Index := Hour_Index + 1;
  95.                     end if;
  96.                     if Date_Index = 8 then
  97.                        Date_Index := 0;
  98.                        Part := Hour_Part;                  
  99.                     elsif Part = Date_Part then
  100.                        Date_Index := Date_Index + 1;
  101.                     end if;
  102.                    
  103.                    
  104.                  end if;              
  105.               end Receive;
  106.         or
  107.            accept Halt;
  108.            exit;
  109.         or
  110.            delay 0.0;
  111.                Draw(Past_Win, 1, 1, White, To_Wide_String(Formatting.Image(Start)));
  112.            Draw(Present_Win, 1, 1, White, To_Wide_String(Formatting.Image(now)));
  113.            Draw(Future_Win, 1, 1, White, To_Wide_String(Formatting.Image(target)));
  114.            if not Going then
  115.               delay 0.05;
  116.               Now :=   Now + 0.05;
  117.               if Done then
  118.                  Target := now + 1.0;
  119.               end if;
  120.            else
  121.               Going := not Going;
  122.               Strat := Now;
  123.               Now := Target;
  124.               Target := now + 1.0;
  125.               Done := True;
  126.            end if;
  127.            
  128.      end select;
  129.   end loop;
  130.      
  131.   end Time_Machine;
  132.  
  133.   Char : Character;
  134. begin
  135.   Time_Machine.Init;
  136.   loop
  137.      Get_Immediate(Char);
  138.      case Char is
  139.         when Character'Val(27) =>          
  140.            Time_Machine.Halt;
  141.            New_Line;
  142.            exit;
  143.         when others =>
  144.            Time_Machine.Receive(Char);
  145.      end case;
  146.   end loop;
  147.  
  148. end Main;


 
Affiche trois fenêtre :
L'heure de départ
L'heure courante
L'heure de destination
 
 
Result
 


                                                                                                             +-----------------[X]+
                                                                                                             |2025-02-16 11:54:10 |
                                                                                                             +--------------------+
                                                                                                             +-----------------[X]+
                                                                                                             |2025-02-16 11:54:13 |
                                                                                                             +--------------------+
                                                                                                             +-----------------[X]+
                                                                                                             |1970-01-01 00:00:00 |
                                                                                                             +--------------------+
 
 


 
Voir le paquetage Calendar pour les ilimites de timer.


Message édité par lady287 le 16-02-2025 à 13:55:54

---------------
Skywalker
Reply

Marsh Posté le 16-02-2025 à 12:57:00   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed