fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Time {
  4. private:
  5. int hours;
  6. int minutes;
  7. int seconds;
  8. public:
  9. Time(int h, int m, int s) : hours(0), minutes(0), seconds(0) {
  10. SetTime(h, m, s);
  11. }
  12. Time &SetTime(int h, int m, int s) {
  13. SetHours(h);
  14. SetMinutes(m);
  15. SetSeconds(s);
  16. return *this;
  17. }
  18. Time& SetHours(int h) {
  19. h = (h >= 0) ? (h % 24) : 0;
  20. this->hours = h;
  21. return *this;
  22. }
  23. int GetHours() {return hours;}
  24. Time& SetMinutes(int m) {
  25. m = (m >= 0 && m < 60) ? m : 0;
  26. this->minutes = m;
  27. return *this;
  28. }
  29. int GetMinutes() {return minutes;}
  30. int GetTotalMinutes() {
  31. return GetHours() * 60 + minutes;
  32. }
  33. Time& SetSeconds(int s) {
  34. s = (s >= 0 && s < 60) ? s : 0;
  35. this->seconds = s;
  36. return *this;
  37. }
  38. int GetSeconds() {return seconds;}
  39. int GetTotalSeconds() {
  40. return GetTotalMinutes() * 60 + seconds;
  41. }
  42. void PrintHHMMSS() {
  43. cout<<ToSring(":")<<"\n";
  44. }
  45. string ToSring(string seperator = "-") {
  46. string hours =to_string(this->hours);
  47. string minutes =to_string(this->minutes);
  48. string seconds =to_string(this->seconds);
  49. if (hours.size() == 1)
  50. hours = '0' + hours;
  51. if (minutes.size() == 1)
  52. minutes = '0' + minutes;
  53. if (seconds.size() == 1)
  54. seconds = '0' + seconds;
  55. ostringstream oss;
  56. oss<< hours<<seperator<<minutes<<seperator<<seconds;
  57. return oss.str();
  58. }
  59. };
  60. int main() {
  61. cout << "WARNING:"<< endl;
  62. cout << "The hours are calculated using a 24-hour cycle; any input exceeding 23 will wrap around." << endl;
  63. cout << "Hours (h), Minutes (m), Seconds (s), or All (a)?\n";
  64. string input;
  65. getline(cin, input);
  66. Time u(0, 0,0);
  67. if (input == "h" || input == "H") {
  68. cout << "Enter the hours: ";
  69. int h;
  70. cin >> h;
  71. u.SetHours(h);
  72. }
  73. else if (input == "m" || input == "M") {
  74. cout << "Enter the minutes: ";
  75. int m; cin >> m;
  76. u.SetMinutes(m);
  77. }
  78. else if (input == "s" || input == "S") {
  79. cout << "Enter the seconds:";
  80. int s; cin >> s;
  81. u.SetSeconds(s);
  82. }
  83. else {
  84. cout << "Enter Hours, Minutes, and Seconds:";
  85. int h, m, s;
  86. cin >> h >> m >> s;
  87. u.SetTime(h, m, s);
  88. }
  89. u.PrintHHMMSS();
  90. return 0;
  91. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
WARNING:
The hours are calculated using a 24-hour cycle; any input exceeding 23 will wrap around.
Hours (h), Minutes (m), Seconds (s), or All (a)?
Enter Hours, Minutes, and Seconds:01:00:01