Advertisement
F_THIAGO

Lista I - Questão 8

Apr 19th, 2019
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. /*
  2. *   8] Fazer um programa em C++ para ler dois tempos (hora, minuto e segundo) e escrever a
  3. * diferenca de tempo entre eles
  4. */
  5.  
  6. #include <iostream>
  7.  
  8. using std::cout;
  9. using std::cin;
  10.  
  11. int main( void )
  12. {
  13.     unsigned short hora;
  14.     unsigned short minutos;
  15.     unsigned short segundos;
  16.    
  17.     unsigned long tempo_a = 0;
  18.     unsigned long tempo_b = 0;
  19.     unsigned long resultado = 0;
  20.    
  21.     int i;
  22.    
  23.     // =========== Entrada de Dados ================
  24.     for( i=0; i<2; i++ )
  25.     {
  26.         cout << "\n========= Tempo " << i+1 << "==========\n";
  27.        
  28.         do
  29.         {
  30.             cout << "Hora (0-23): ";
  31.             cin  >> hora;
  32.         }while( !(hora >= 0 && hora <= 23) );
  33.        
  34.         do
  35.         {
  36.             cout << "minutos (0-59): ";
  37.             cin  >> minutos;       
  38.         }while( !(minutos >= 0 && minutos <= 59) );
  39.        
  40.         do
  41.         {
  42.             cout << "segundos (0-59): ";
  43.             cin  >> segundos;      
  44.         }while( !(segundos >= 0 && segundos <= 59 ) );
  45.        
  46.         // Converte a entrada em segundos
  47.         if( i == 0 )
  48.             tempo_a = hora*60*60 + minutos*60 + segundos;
  49.         else
  50.             tempo_b = hora*60*60 + minutos*60 + segundos;
  51.     }
  52.    
  53.     // Calcula a diferenca de tempos
  54.     resultado = (tempo_a >= tempo_b)?(tempo_a-tempo_b):(tempo_b-tempo_a);
  55.    
  56.     // Converte de segundos para formato: hh:mm:ss
  57.     hora = resultado/3600;
  58.     minutos = (resultado - 3600*hora)/60;
  59.     segundos = (resultado - 3600*hora - minutos*60);
  60.    
  61.     // ========== Saida de dados =============
  62.     cout << "\n============== RESULTADO =================\n";
  63.     cout << " :: " << hora << "h:" << minutos << "m:" << segundos << "s \n";
  64.    
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement