Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4. #ifndef DURATION_H
  5. #define DURATION_H
  6.  
  7. // Duration class
  8. class Duration {
  9. private:
  10.  
  11. int hours = 0;
  12. int minutes = 0;
  13. int seconds = 0;
  14.  
  15. public:
  16.  
  17. Duration (int x = 0, int y = 0, int z = 0);
  18.  
  19. void set (int sum);
  20.  
  21. Duration operator + (Duration other);
  22.  
  23. Duration operator - (Duration other);
  24.  
  25. Duration operator * (double other);
  26.  
  27. Duration operator / (double other);
  28.  
  29. double operator / (Duration other);
  30.  
  31. int inSeconds();
  32.  
  33. Duration format (int sum);
  34.  
  35. void randomize();
  36.  
  37. operator const char*();
  38.  
  39. int getHours();
  40.  
  41. void setHours(int x);
  42.  
  43. int getMinutes();
  44.  
  45. void setMinutes(int x);
  46.  
  47. int getSeconds();
  48.  
  49. void setSeconds(int x);
  50.  
  51.  
  52. };
  53.  
  54. Duration::Duration (int x, int y, int z) {
  55. hours = x;
  56. minutes = y;
  57. seconds = z;
  58.  
  59. set(inSeconds());
  60. }
  61.  
  62. void Duration::set (int sum) {
  63. if (sum < 0) {
  64. hours = 0;
  65. minutes = 0;
  66. seconds = 0;
  67. }
  68. else {
  69. hours = 0;
  70. minutes = 0;
  71. seconds = sum;
  72. while (seconds > 59) {
  73. minutes++;
  74. seconds -= 60;
  75. }
  76.  
  77. while (minutes > 59) {
  78. hours++;
  79. minutes -= 60;
  80. }
  81. }
  82. }
  83.  
  84. // Addition operator
  85. Duration Duration::operator + (Duration other) {
  86. return format(inSeconds() + other.inSeconds());
  87. }
  88.  
  89. // Subtraction operator
  90. Duration Duration::operator - (Duration other) {
  91. return format(inSeconds() - other.inSeconds());
  92. }
  93.  
  94. // Multiplication operator
  95. Duration Duration::operator * (double other) {
  96. return format(inSeconds() * other);
  97. }
  98.  
  99. // Division operator (by constant)
  100. Duration Duration::operator / (double other) {
  101. return format(inSeconds() / other);
  102. }
  103.  
  104. // Division operator (by another duration)
  105. double Duration::operator / (Duration other) {
  106. return (double)inSeconds() / other.inSeconds();
  107. }
  108.  
  109. // Converts the duration into seconds
  110. int Duration::inSeconds() {
  111. return hours * 3600 + minutes * 60 + seconds;
  112. }
  113.  
  114. // Converts the seconds to hours minutes and seconds
  115. Duration Duration::format (int sum) {
  116. if (sum < 0) {
  117. Duration duration (0, 0, 0);
  118. return duration;
  119. }
  120. int h = 0;
  121. int m = 0;
  122. int s = sum;
  123. while (s > 59) {
  124. m++;
  125. s -= 60;
  126. }
  127.  
  128. while (m > 59) {
  129. h++;
  130. m -= 60;
  131. }
  132.  
  133. Duration duration (h, m, s);
  134. return duration;
  135. }
  136.  
  137. // Random duration less than a day
  138. void Duration::randomize() {
  139. hours = rand() % 24;
  140. minutes = rand() % 60;
  141. seconds = rand() % 60;
  142. }
  143.  
  144. // Display operator
  145. Duration::operator const char*() {
  146. string s = "";
  147. ostringstream duration;
  148.  
  149. if (hours < 10) {
  150. duration << "0" << hours << ":";
  151. }
  152.  
  153. else {
  154. duration << hours << ":";
  155. }
  156.  
  157. if (minutes < 10) {
  158. duration << "0" << minutes << ":";
  159. }
  160.  
  161. else {
  162. duration << minutes << ":";
  163. }
  164.  
  165. if (seconds < 10) {
  166. duration << "0" << seconds;
  167. }
  168.  
  169. else {
  170. duration << seconds;
  171. }
  172.  
  173. s = duration.str();
  174. return s.c_str();
  175. }
  176.  
  177. int Duration::getHours() {
  178. return hours;
  179. }
  180.  
  181. void Duration::setHours(int x) {
  182. hours = x;
  183. }
  184.  
  185. int Duration::getMinutes() {
  186. return minutes;
  187. }
  188.  
  189. void Duration::setMinutes(int x) {
  190. minutes = x;
  191. }
  192.  
  193. int Duration::getSeconds() {
  194. return seconds;
  195. }
  196.  
  197. void Duration::setSeconds(int x) {
  198. seconds = x;
  199. }
  200. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement