fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. class Time {
  5. public:
  6. Time(int h = 0, int m = 0, int s = 0) { setTime(h, m, s); }
  7.  
  8. void setTime(int h, int m, int s) {
  9. setHour(h);
  10. setMinute(m);
  11. setSecond(s);
  12. }
  13.  
  14. void setHour(int h) { hour = (h >= 0 && h < 24) ? h : 0; }
  15. void setMinute(int m) { minute = (m >= 0 && m < 60) ? m : 0; }
  16. void setSecond(int s) { second = (s >= 0 && s < 60) ? s : 0; }
  17.  
  18. int getHour() const { return hour; }
  19. int getMinute() const { return minute; }
  20. int getSecond() const { return second; }
  21.  
  22. void tick() {
  23. setSecond(second + 1);
  24. if (second == 0) {
  25. setMinute(minute + 1);
  26. if (minute == 0) {
  27. setHour(hour + 1);
  28. }
  29. }
  30. }
  31.  
  32. void printStandard() const {
  33. std::cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":"
  34. << std::setfill('0') << std::setw(2) << minute << ":"
  35. << std::setw(2) << second << (hour < 12 ? " AM" : " PM");
  36. }
  37.  
  38. private:
  39. int hour;
  40. int minute;
  41. int second;
  42. };
  43.  
  44. int main() {
  45. Time t(23, 59, 58);
  46.  
  47. for (int i = 0; i < 5; ++i) {
  48. t.printStandard();
  49. std::cout << std::endl;
  50. t.tick();
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
11:59:58 PM
11:59:59 PM
12:00:00 AM
12:00:01 AM
12:00:02 AM