scopolamine

Time.cpp

Mar 31st, 2011
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. // Fig. 9.9: Time.cpp
  2. // Member-function definitions for class Time.
  3. #include <iostream>
  4. #include <iomanip>
  5. #include "Time.h" // include definition of class Time from Time.h
  6. using namespace std;
  7.  
  8. // Time constructor initializes each data member to zero;
  9. // ensures that Time objects start in a consistent state
  10. Time::Time( int hr, int min, int sec )
  11. {
  12.    setTime( hr, min, sec ); // validate and set time
  13. } // end Time constructor
  14.  
  15. // set new Time value using universal time; ensure that
  16. // the data remains consistent by setting invalid values to zero
  17. void Time::setTime( int h, int m, int s )
  18. {
  19.    setHour( h ); // set private field hour
  20.    setMinute( m ); // set private field minute
  21.    setSecond( s ); // set private field second
  22. } // end function setTime
  23.  
  24. // set hour value
  25. void Time::setHour( int h )
  26. {
  27.    hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
  28. } // end function setHour
  29.  
  30. // set minute value
  31. void Time::setMinute( int m )
  32. {
  33.    minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
  34. } // end function setMinute
  35.  
  36. // set second value
  37. void Time::setSecond( int s )
  38. {
  39.    second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
  40. } // end function setSecond
  41.  
  42. // return hour value
  43. int Time::getHour()
  44. {
  45.    return hour;
  46. } // end function getHour
  47.  
  48. // return minute value
  49. int Time::getMinute()
  50. {
  51.    return minute;
  52. } // end function getMinute
  53.  
  54. // return second value
  55. int Time::getSecond()
  56. {
  57.    return second;
  58. } // end function getSecond
  59.  
  60. // print Time in universal-time format (HH:MM:SS)
  61. void Time::printUniversal()
  62. {
  63.    cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
  64.       << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
  65. } // end function printUniversal
  66.  
  67. // print Time in standard-time format (HH:MM:SS AM or PM)
  68. void Time::printStandard()
  69. {
  70.    cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 )
  71.       << ":" << setfill( '0' ) << setw( 2 ) << getMinute()
  72.       << ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
  73. } // end function printStandard
  74.  
  75. // time counter
  76. void Time::tickTime(int s)
  77. {
  78.      do
  79.      {
  80.      s++;
  81.       cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
  82.       << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
  83.       wait(1);
  84.      }
  85.      while( second != 100 ); // second 60 ı geçemeyeceği için sürekli döngü dönecek
  86. }
  87.  
  88. // waiting time
  89. void Time::wait(int seconds)
  90. {
  91.   clock_t endwait;
  92.   endwait = clock () + seconds * CLOCKS_PER_SEC ; // saniyeyi tanımlayan özel fonksiyon
  93.   while (clock() < endwait) {}
  94. }
Advertisement
Add Comment
Please, Sign In to add comment