Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time.cc 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "time.h"
  2. /*************** Complementary work ***************/
  3. /* TODO: A better way to implement this function is to just return the
  4. * boolean condition in the if statement right away since it will have
  5. * the same affect and increase readability. The same way you did in
  6. * the "is_am" function*/
  7. // --> done
  8. /**************************************************/
  9. bool is_valid(Time const& t) {
  10. return (t.hours >= 0 && t.minutes >= 0 && t.secondes >= 0
  11. && t.hours < 24 && t.minutes < 60 && t.secondes < 60);
  12. }
  13. bool is_am(Time const& t) {
  14. return (t.hours % 12 == t.hours);
  15. }
  16. // A function which convert a int into a 2-digits formated string
  17. std::string formated_int_to_string(int i){
  18. std::string str = std::to_string(i);
  19. if (str.length() < 2){
  20. str = "0" + str;
  21. }
  22. return str;
  23. }
  24. /***************** Comment *****************/
  25. /* A better way to convert the Time object to a string is to use a
  26. * stringstream. A stringstream is just like the ostream that you are
  27. * used to, the same iomanip manipulators works on it and once your
  28. * done you can convert its contents to a string by using its .str() function*/
  29. // --> done
  30. /*******************************************/
  31. std::string to_string(Time const& t,bool format) {
  32. std::stringstream string_stream{};
  33. string_stream << std::setfill('0');
  34. if (format){
  35. string_stream << std::setw(2) << t.hours % 12;
  36. }
  37. else {
  38. string_stream << std::setw(2) << t.hours;
  39. }
  40. string_stream << ":" << std::setw(2) << t.minutes
  41. << ":" << std::setw(2) << t.secondes;
  42. if (format) {
  43. if (is_am(t)){
  44. string_stream << " am";
  45. }
  46. else {
  47. string_stream << " pm";
  48. }
  49. }
  50. return string_stream.str();
  51. }
  52. // Deprecated to_string function
  53. std::string old_to_string(Time const& t,bool format) {
  54. std::string time_string{};
  55. if (format){
  56. time_string = formated_int_to_string(t.hours % 12) + ":" + formated_int_to_string(t.minutes) + ":"
  57. + formated_int_to_string(t.secondes);
  58. if (is_am(t)) {
  59. time_string += " am";
  60. }
  61. else {
  62. time_string += " pm";
  63. }
  64. }
  65. else {
  66. time_string = formated_int_to_string(t.hours) + ":" + formated_int_to_string(t.minutes) + ":"
  67. + formated_int_to_string(t.secondes);
  68. }
  69. return time_string;
  70. }
  71. Time operator+(Time const& t1, Time const& t2){
  72. return Time{(t1.hours + t2.hours + (t1.minutes+t2.minutes + (t1.secondes+t2.secondes)/60)/60) % 24,
  73. (t1.minutes + t2.minutes + (t1.secondes+t2.secondes)/60) % 60,
  74. (t1.secondes + t2.secondes) % 60};
  75. }
  76. Time operator-(Time const& t1, Time const& t2){
  77. Time diff{(t1.hours - t2.hours + (t1.minutes-t2.minutes + (t1.secondes-t2.secondes)/60)/60) % 24,
  78. (t1.minutes - t2.minutes + (t1.secondes-t2.secondes)/60) % 60,
  79. (t1.secondes - t2.secondes) % 60};
  80. if (diff.hours < 0){
  81. diff.hours = 24 + diff.hours;
  82. }
  83. if (diff.minutes < 0){
  84. diff.minutes = 60 + diff.minutes;
  85. diff.hours--;
  86. }
  87. if (diff.secondes < 0){
  88. diff.secondes = 60 + diff.secondes;
  89. diff.minutes--;
  90. }
  91. return diff;
  92. }
  93. Time operator+(Time const& t, int x){
  94. return t + Time{0, 0, x};
  95. }
  96. Time operator-(Time const& t, int x){
  97. return t - Time{0, 0, x};
  98. }
  99. /*************** Complementary work ***************/
  100. /* TODO: Code duplication, you can use the operator+ here to make the
  101. * function a lot shorter. The same goes for operator--*/
  102. // --> done
  103. // --> 2nd complementary work done: Use of the operator implemented above.
  104. // Fixing operator--.
  105. /**************************************************/
  106. Time& operator++(Time& t){
  107. t = t + 1;
  108. return t;
  109. }
  110. Time operator++(Time& t, int){
  111. Time tmp{t};
  112. ++t;
  113. return tmp;
  114. }
  115. Time& operator--(Time& t){
  116. t = t - 1;
  117. return t;
  118. }
  119. Time operator--(Time& t, int){
  120. Time tmp = t;
  121. --t;
  122. return tmp;
  123. }
  124. bool operator<(Time const& t1, Time const& t2){
  125. if (t1.hours < t2.hours){
  126. return true;
  127. }
  128. else if (t1.hours > t2.hours){
  129. return false;
  130. }
  131. else {
  132. if (t1.minutes < t2.minutes){
  133. return true;
  134. }
  135. else if (t1.minutes > t2.minutes){
  136. return false;
  137. }
  138. else {
  139. if (t1.secondes < t2.secondes){
  140. return true;
  141. }
  142. else {
  143. return false;
  144. }
  145. }
  146. }
  147. }
  148. bool operator>(Time const& t1, Time const& t2){
  149. return t2 < t1;
  150. }
  151. bool operator==(Time const& t1, Time const& t2){
  152. return t1.hours == t2.hours && t1.minutes == t2.minutes && t1.secondes == t2.secondes;
  153. }
  154. bool operator!=(Time const& t1, Time const& t2){
  155. return !(t1 == t2);
  156. }
  157. bool operator<=(Time const& t1, Time const& t2){
  158. return t1 < t2 || t1 == t2;
  159. }
  160. bool operator>=(Time const& t1, Time const& t2){
  161. return t1 > t2 || t1 == t2;
  162. }
  163. std::ostream& operator<<(std::ostream& os, Time const& t){
  164. os << to_string(t, false);
  165. return os;
  166. }
  167. std::istream& operator>>(std::istream& is, Time& t){
  168. /*************** Complementary work ***************/
  169. /* TODO: It is not ok to modify the given Time object before knowing
  170. * if the input is good or not. First check if the input is fine then
  171. * modify the object, refrain from modifying the object if the input is bad.*/
  172. // --> done
  173. /**************************************************/
  174. char c{};
  175. Time tmp{};
  176. is >> tmp.hours;
  177. is >> c;
  178. is >> tmp.minutes;
  179. is >> c;
  180. is >> tmp.secondes;
  181. if (!is_valid(tmp)){
  182. is.setstate(std::ios_base::failbit);
  183. }
  184. else {
  185. t = tmp;
  186. }
  187. return is;
  188. }