Advertisement
RobertDeMilo

WB4.12 Использование перегруженных операторов в собственных структурах

Oct 12th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <map>
  9.  
  10. using namespace std;
  11.  
  12. class Duration
  13. {
  14. private:
  15.  
  16.     int hour;
  17.     int min;
  18.  
  19. public:
  20.  
  21.     Duration(int hour_ = 0, int min_ = 0)
  22.     {
  23.         int total = hour_ * 60 + min_;
  24.         hour = total / 60;
  25.         min = total % 60;
  26.     }
  27.  
  28.     int Hour() const
  29.     {
  30.         return hour;
  31.     }
  32.  
  33.     int Min() const
  34.     {
  35.         return min;
  36.     }
  37. };
  38.  
  39. bool CompareDurations(const Duration& lhs, const Duration& rhs)
  40. {
  41.     if (lhs.Hour() == rhs.Hour())
  42.     {
  43.         return lhs.Min() < rhs.Min();
  44.     }
  45.  
  46.     return lhs.Hour() < rhs.Hour();
  47. }
  48.  
  49. bool operator<(const Duration& lhs, const Duration& rhs)
  50. {
  51.     if (lhs.Hour() == rhs.Hour())
  52.     {
  53.         return lhs.Min() < rhs.Min();
  54.     }
  55.     return lhs.Hour() < rhs.Hour();
  56. }
  57.  
  58. ostream& operator<<(ostream& out, const Duration& dur)
  59. {
  60.     out << setfill('0');
  61.     out << setw(2) << dur.Hour() << ':' << setw(2) << dur.Min();
  62.  
  63.     return out;
  64. }
  65.  
  66. istream& operator >> (istream& in, Duration& dur)
  67. {
  68.     int hour;
  69.     in >> hour;
  70.     in.ignore();
  71.  
  72.     int min;
  73.     in >> min;
  74.    
  75.     dur = Duration(hour, min); // !!!!!!!!!!!!!!!
  76.  
  77.     return in;
  78. }
  79.  
  80. Duration operator+(const Duration& lhs, const Duration& rhs)
  81. {
  82.     return Duration{ lhs.Hour() + rhs.Hour(),lhs.Min() + rhs.Min() };
  83. }
  84.  
  85. int main()
  86. {
  87.     **************************************************************************
  88.     vector <Duration> durations;
  89.     durations.push_back(Duration{ 1,50 });
  90.     durations.push_back(Duration{ 0,50 });
  91.     durations.push_back(Duration{ 0,1 });
  92.  
  93.     for (const auto& dur : durations)
  94.     {
  95.         cout << dur << '\t';
  96.     }
  97.     cout << endl;
  98.  
  99.     sort(durations.begin(), durations.end());
  100.  
  101.     for (const auto& dur : durations)
  102.     {
  103.         cout << dur << '\t';
  104.     }
  105.     **************************************************************************
  106.  
  107.     ifstream input("C:/Users/musta/Downloads/text.txt");
  108.  
  109.     Duration worst;
  110.  
  111.     map<Duration, string> all;
  112.  
  113.     if (input)
  114.     {
  115.         Duration dur;
  116.         string name;
  117.  
  118.         while (input >> dur >> name)
  119.         {
  120.             if (worst < dur)
  121.             {
  122.                 worst = dur;
  123.             }
  124.  
  125.             all[dur] += (name + " "); // если в словаре уже кто-то есть с таким интервалом забега,
  126.             // то мы просто добавлем в строчку имени новое имя и в конец дописываем пробел
  127.         }
  128.     }
  129.  
  130.     ofstream out("result.txt");
  131.  
  132.     for (const auto& durationNames : all)
  133.     {
  134.         out << durationNames.first << '\t' << durationNames.second << endl;
  135.     }
  136.  
  137.     cout << "Worst runner: " << all[worst] << endl;
  138.  
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement