123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #include "time.h"
-
- /*************** Complementary work ***************/
- /* TODO: A better way to implement this function is to just return the
- * boolean condition in the if statement right away since it will have
- * the same affect and increase readability. The same way you did in
- * the "is_am" function*/
- // --> done
- /**************************************************/
-
- bool is_valid(Time const& t) {
- return (t.hours >= 0 && t.minutes >= 0 && t.secondes >= 0
- && t.hours < 24 && t.minutes < 60 && t.secondes < 60);
- }
-
- bool is_am(Time const& t) {
- return (t.hours % 12 == t.hours);
- }
-
- // A function which convert a int into a 2-digits formated string
- std::string formated_int_to_string(int i){
- std::string str = std::to_string(i);
- if (str.length() < 2){
- str = "0" + str;
- }
- return str;
- }
-
- /***************** Comment *****************/
- /* A better way to convert the Time object to a string is to use a
- * stringstream. A stringstream is just like the ostream that you are
- * used to, the same iomanip manipulators works on it and once your
- * done you can convert its contents to a string by using its .str() function*/
- // --> done
- /*******************************************/
- std::string to_string(Time const& t,bool format) {
- std::stringstream string_stream{};
- string_stream << std::setfill('0');
- if (format){
- string_stream << std::setw(2) << t.hours % 12;
- }
- else {
- string_stream << std::setw(2) << t.hours;
- }
- string_stream << ":" << std::setw(2) << t.minutes
- << ":" << std::setw(2) << t.secondes;
- if (format) {
- if (is_am(t)){
- string_stream << " am";
- }
- else {
- string_stream << " pm";
- }
- }
- return string_stream.str();
- }
-
- // Deprecated to_string function
- std::string old_to_string(Time const& t,bool format) {
- std::string time_string{};
-
- if (format){
- time_string = formated_int_to_string(t.hours % 12) + ":" + formated_int_to_string(t.minutes) + ":"
- + formated_int_to_string(t.secondes);
- if (is_am(t)) {
- time_string += " am";
- }
- else {
- time_string += " pm";
- }
- }
- else {
- time_string = formated_int_to_string(t.hours) + ":" + formated_int_to_string(t.minutes) + ":"
- + formated_int_to_string(t.secondes);
- }
- return time_string;
- }
-
- Time operator+(Time const& t1, Time const& t2){
- return Time{(t1.hours + t2.hours + (t1.minutes+t2.minutes + (t1.secondes+t2.secondes)/60)/60) % 24,
- (t1.minutes + t2.minutes + (t1.secondes+t2.secondes)/60) % 60,
- (t1.secondes + t2.secondes) % 60};
- }
-
- Time operator-(Time const& t1, Time const& t2){
- Time diff{(t1.hours - t2.hours + (t1.minutes-t2.minutes + (t1.secondes-t2.secondes)/60)/60) % 24,
- (t1.minutes - t2.minutes + (t1.secondes-t2.secondes)/60) % 60,
- (t1.secondes - t2.secondes) % 60};
-
- if (diff.hours < 0){
- diff.hours = 24 + diff.hours;
- }
- if (diff.minutes < 0){
- diff.minutes = 60 + diff.minutes;
- diff.hours--;
- }
- if (diff.secondes < 0){
- diff.secondes = 60 + diff.secondes;
- diff.minutes--;
- }
- return diff;
- }
-
- Time operator+(Time const& t, int x){
- return t + Time{0, 0, x};
- }
-
- Time operator-(Time const& t, int x){
- return t - Time{0, 0, x};
- }
-
- /*************** Complementary work ***************/
- /* TODO: Code duplication, you can use the operator+ here to make the
- * function a lot shorter. The same goes for operator--*/
- // --> done
- // --> 2nd complementary work done: Use of the operator implemented above.
- // Fixing operator--.
- /**************************************************/
- Time& operator++(Time& t){
- t = t + 1;
- return t;
- }
-
- Time operator++(Time& t, int){
- Time tmp{t};
- ++t;
- return tmp;
- }
-
- Time& operator--(Time& t){
- t = t - 1;
- return t;
- }
-
- Time operator--(Time& t, int){
- Time tmp = t;
- --t;
- return tmp;
- }
-
- bool operator<(Time const& t1, Time const& t2){
- if (t1.hours < t2.hours){
- return true;
- }
- else if (t1.hours > t2.hours){
- return false;
- }
- else {
- if (t1.minutes < t2.minutes){
- return true;
- }
- else if (t1.minutes > t2.minutes){
- return false;
- }
- else {
- if (t1.secondes < t2.secondes){
- return true;
- }
- else {
- return false;
- }
- }
- }
-
- }
-
- bool operator>(Time const& t1, Time const& t2){
- return t2 < t1;
- }
-
- bool operator==(Time const& t1, Time const& t2){
- return t1.hours == t2.hours && t1.minutes == t2.minutes && t1.secondes == t2.secondes;
- }
-
- bool operator!=(Time const& t1, Time const& t2){
- return !(t1 == t2);
- }
-
- bool operator<=(Time const& t1, Time const& t2){
- return t1 < t2 || t1 == t2;
- }
-
- bool operator>=(Time const& t1, Time const& t2){
- return t1 > t2 || t1 == t2;
- }
-
- std::ostream& operator<<(std::ostream& os, Time const& t){
- os << to_string(t, false);
- return os;
- }
-
- std::istream& operator>>(std::istream& is, Time& t){
-
- /*************** Complementary work ***************/
- /* TODO: It is not ok to modify the given Time object before knowing
- * if the input is good or not. First check if the input is fine then
- * modify the object, refrain from modifying the object if the input is bad.*/
- // --> done
- /**************************************************/
-
- char c{};
- Time tmp{};
-
- is >> tmp.hours;
- is >> c;
- is >> tmp.minutes;
- is >> c;
- is >> tmp.secondes;
-
- if (!is_valid(tmp)){
- is.setstate(std::ios_base::failbit);
- }
- else {
- t = tmp;
- }
-
- return is;
- }
|