Advertisement
AntonioVillanueva

Contrôle du temps en C++

Mar 1st, 2024
621
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 1
  1. /*
  2.  * contrôle du temps avec library chrono ... C++ A.Villanueva
  3.  */
  4. #include <iostream>
  5. #include <chrono>
  6. using namespace std;
  7.  
  8. #define TEMPS_REF 4
  9.  
  10. //
  11. bool chronometre( std::chrono::time_point<std::chrono::system_clock> t1,
  12.         std::chrono::time_point<std::chrono::system_clock> t0,
  13.         std::chrono::duration<double> ref){
  14.  
  15.      std::chrono::duration<double> segundos = t1 - t0;
  16.      if (segundos > ref){ return true;}
  17.      
  18.      return false;
  19.      
  20. }
  21.  
  22. //temps écoulé
  23. void tempsEcoule( std::chrono::time_point<std::chrono::system_clock> t1,
  24.        std::chrono::time_point<std::chrono::system_clock> t0){
  25.  
  26.      std::chrono::duration<double> segundos = t1 - t0;
  27.      cout << "temps écoulé : " << segundos.count() << endl;
  28.      
  29. }
  30.  
  31.  
  32. int main (){
  33.      std::chrono::duration<double> t_ref(TEMPS_REF);
  34.     std::chrono::time_point<std::chrono::system_clock> t0, t1,ctrl;
  35.    
  36.     t0= std::chrono::system_clock::now();
  37.  
  38.    
  39.     while (true){
  40.         t1= std::chrono::system_clock::now();//heure actuelle
  41.         tempsEcoule(t1,t0);//DEBUG analyse du temps écoulé
  42.        
  43.         //Le programme se termine lorsqu'il atteint le temps t_ref
  44.         if (chronometre(t1,t0,t_ref)){return (0);}
  45.  
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement