Advertisement
RobertDeMilo

WB4.10 Перегрузка оператора сравнения

Oct 12th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. struct Duration
  11. {
  12.     int hour;
  13.     int min;
  14. };
  15.  
  16. bool CompareDurations(const Duration& lhs, const Duration& rhs)
  17. {
  18.     if (lhs.hour == rhs.hour)
  19.     {
  20.         return lhs.min < rhs.min;
  21.     }
  22.     return lhs.hour < rhs.hour;
  23. }
  24.  
  25. bool operator<(const Duration& lhs, const Duration& rhs)
  26. {
  27.     if (lhs.hour == rhs.hour)
  28.     {
  29.         return lhs.min < rhs.min;
  30.     }
  31.     return lhs.hour < rhs.hour;
  32. }
  33.  
  34. ostream& operator<<(ostream& stream, const Duration& duration)
  35. {
  36.     stream << setfill('0');
  37.     stream << setw(2) << duration.hour << ':' << setw(2) << duration.min;
  38.  
  39.     return stream;
  40. }
  41.  
  42. istream& operator >> (istream& stream, Duration& duration)
  43. {
  44.     stream >> duration.hour;
  45.     stream.ignore(1);
  46.  
  47.     stream >> duration.min;
  48.     stream.ignore(1);
  49.  
  50.     return stream;
  51. }
  52.  
  53. int main()
  54. {
  55.     Duration dur1{ 1,12 };
  56.     Duration dur2{ 1,13 };
  57.     //cout << boolalpha << CompareDurations(dur1, dur2) << endl;
  58.     cout << boolalpha << (dur1 < dur2) << endl;
  59.  
  60.     return 0;
  61. }
Tags: boolalpha
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement