fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Time
  8. {
  9. private:
  10. int hours;
  11. int minutes;
  12. int seconds;
  13. public:
  14. Time(string timeString);
  15. int toSeconds();
  16. void displayTime();
  17. };
  18.  
  19. class Clock
  20. {
  21. private:
  22. string nameClock;
  23. Time currentTime;
  24. public:
  25. Clock(string clockName, string timeString);
  26. void showElapsedTime();
  27. };
  28.  
  29. Time::Time(string timeString) : hours(0), minutes(0), seconds(0)
  30. {
  31. int i = 0, n = timeString.length();
  32.  
  33. while (i < n && timeString[i] != ':')
  34. {
  35. hours = hours * 10 + (timeString[i] - '0');
  36. i++;
  37. }
  38.  
  39. i++;
  40. while (i < n && timeString[i] != ':')
  41. {
  42. minutes = minutes * 10 + (timeString[i] - '0');
  43. i++;
  44. }
  45.  
  46. i++;
  47. while (i < n)
  48. {
  49. seconds = seconds * 10 + (timeString[i] - '0');
  50. i++;
  51. }
  52. }
  53.  
  54. int Time::toSeconds()
  55. {
  56. return hours * 3600 + minutes * 60 + seconds;
  57. }
  58.  
  59. void Time::displayTime()
  60. {
  61. cout << (hours < 10 ? "0" : "") << hours << ":"
  62. << (minutes < 10 ? "0" : "") << minutes << ":"
  63. << (seconds < 10 ? "0" : "") << seconds << '\n';
  64. }
  65.  
  66. Clock::Clock(string clockName, string timeString) : nameClock(clockName), currentTime(timeString) {}
  67.  
  68. void Clock::showElapsedTime()
  69. {
  70. cout << "\nClock " << nameClock << " is displaying time: ";
  71. currentTime.displayTime();
  72. cout << "Elapsed time since 00:00:00: " << currentTime.toSeconds() << " seconds.\n";
  73. }
  74.  
  75. int main()
  76. {
  77. cout << "This program initializes a clock and displays elapsed time in seconds.\n";
  78. cout << "----------------------------------------------------------\n";
  79.  
  80. string clockName, timeString;
  81.  
  82. cout << "Enter clock name: ";
  83. getline(cin, clockName);
  84.  
  85. cout << "Enter current time (HH:MM:SS): ";
  86. getline(cin, timeString);
  87.  
  88. Clock myClock(clockName, timeString);
  89. myClock.showElapsedTime();
  90.  
  91. cout << "\nProgram execution completed. Exiting...\n";
  92.  
  93. return 0;
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
This program initializes a clock and displays elapsed time in seconds.
----------------------------------------------------------
Enter clock name: Enter current time (HH:MM:SS): 
Clock  is displaying time: 00:00:00
Elapsed time since 00:00:00: 0 seconds.

Program execution completed. Exiting...