Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class time {
  6. private:
  7. int hour, minute, sec, millisec;
  8. public:
  9. time() {
  10. cout << "Вызвался конструктор по умолчанию\n";
  11. cout << this << "\n";
  12. hour = 0;
  13. minute = 0;
  14. sec = 0;
  15. millisec = 0;
  16. }
  17. time(int h, int m, int s, int ms) {
  18. cout << "Вызвался конструктор с параматром\n";
  19. cout << this << "\n";
  20. hour = h;
  21. minute = m;
  22. sec = s;
  23. millisec = ms;
  24. }
  25. ~time() {
  26. cout << "Вызвался деструктор " << this << "\n";
  27. }
  28. void SetHour(int h);
  29. void SetMinute(int m);
  30. void SetSec(int s);
  31. void SetMillisec(int ms);
  32. void Print();
  33. int GetHour() const;
  34. int GetMinute() const;
  35. int GetSec() const;
  36. int GetMillisec() const;
  37. time operator -(time & other) {
  38. cout << "Вызвался оператор -\n" << this << "\n";
  39. time temp;
  40. int sM1 = 0, sM2 = 0,sM3=0;
  41. sM1 = this->hour * 3600000 + this->minute * 60000 + this->sec * 1000 + this->millisec;
  42. sM2 = other.hour * 3600000 + other.minute * 60000 + other.sec * 1000 + other.millisec;
  43. sM3 = abs(sM1 - sM2);
  44. temp.millisec = sM3 % 1000;
  45. sM3 /= 1000;
  46. temp.hour = sM3 / 3600;
  47. while (temp.hour > 23) temp.hour -= 24;
  48. sM3 %= 3600;
  49. temp.minute = sM3 / 60;
  50. while (temp.minute > 59) temp.minute -= 60;
  51. sM3 %= 60;
  52. temp.sec = sM3;
  53. while (temp.sec > 59) temp.sec -= 60;
  54.  
  55. return temp;
  56. }
  57. time (const time & other) {
  58. cout << "Вызвался конструктор копирования\n";
  59. cout << this << "\n";
  60. this->hour = other.hour;
  61. this->minute = other.minute;
  62. this->sec = other.sec;
  63. this->millisec = other.millisec;
  64. }
  65. time operator +(time & other) {
  66. cout << "Вызвался оператор +\n" << this << "\n";
  67. time temp;
  68. int sM1 = 0, sM2 = 0, sM3 = 0;
  69. sM1 = this->hour * 3600000 + this->minute * 60000 + this->sec * 1000 + this->millisec;
  70. sM2 = other.hour * 3600000 + other.minute * 60000 + other.sec * 1000 + other.millisec;
  71. sM3 = sM1 + sM2;
  72. temp.millisec = sM3 % 1000;
  73. sM3 /= 1000;
  74. temp.hour = sM3 / 3600;
  75. while (temp.hour > 23) temp.hour -= 24;
  76. sM3 %= 3600;
  77. temp.minute = sM3 / 60;
  78. while (temp.minute > 59) temp.minute -= 60;
  79. sM3 %= 60;
  80. temp.sec = sM3;
  81. while (temp.sec > 59) temp.sec -= 60;
  82.  
  83. return temp;
  84. }
  85. time & operator = (const time & other) {
  86. this->hour = other.hour;
  87. this->minute = other.minute;
  88. this->sec = other.sec;
  89. this->millisec = other.millisec;
  90. }
  91. };
  92. void time::SetHour(int h) {
  93. hour = h;
  94. }
  95. void time::SetMinute(int m) {
  96. minute = m;
  97. }
  98. void time::SetSec(int s) {
  99. sec = s;
  100. }
  101. void time::SetMillisec(int ms) {
  102. millisec = ms;
  103. }
  104. int time::GetHour() const {
  105. return hour;
  106. }
  107. int time::GetMinute() const {
  108. return minute;
  109. }
  110. int time::GetSec() const{
  111. return sec;
  112. }
  113. int time::GetMillisec() const {
  114. return millisec;
  115. }
  116. void time::Print() {
  117. cout << "Время: " << GetHour() << ":" << GetMinute() << ":" << GetSec() << ":" << GetMillisec() << "\n\n";
  118. }
  119. int main()
  120. {
  121. setlocale(LC_ALL, "Russian");
  122. int h, m, s, ms;
  123. cin >> h >> m >> s >> ms;
  124. time to(h,m,s,ms);
  125. to.Print();
  126.  
  127. time to2(12, 0, 34, 0);
  128. to2.Print();
  129.  
  130. time to3 = to + to2;
  131. to3.Print();
  132.  
  133. time to4 = to - to2;
  134. to4.Print();
  135.  
  136. time to5 = to;
  137. to5.Print();
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement