Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <iomanip>
- #include <sstream>
- #include <algorithm>
- #include <map>
- using namespace std;
- class Duration
- {
- private:
- int hour;
- int min;
- public:
- Duration(int hour_ = 0, int min_ = 0)
- {
- int total = hour_ * 60 + min_;
- hour = total / 60;
- min = total % 60;
- }
- int Hour() const
- {
- return hour;
- }
- int Min() const
- {
- return min;
- }
- };
- bool CompareDurations(const Duration& lhs, const Duration& rhs)
- {
- if (lhs.Hour() == rhs.Hour())
- {
- return lhs.Min() < rhs.Min();
- }
- return lhs.Hour() < rhs.Hour();
- }
- bool operator<(const Duration& lhs, const Duration& rhs)
- {
- if (lhs.Hour() == rhs.Hour())
- {
- return lhs.Min() < rhs.Min();
- }
- return lhs.Hour() < rhs.Hour();
- }
- ostream& operator<<(ostream& out, const Duration& dur)
- {
- out << setfill('0');
- out << setw(2) << dur.Hour() << ':' << setw(2) << dur.Min();
- return out;
- }
- istream& operator >> (istream& in, Duration& dur)
- {
- int hour;
- in >> hour;
- in.ignore();
- int min;
- in >> min;
- dur = Duration(hour, min); // !!!!!!!!!!!!!!!
- return in;
- }
- Duration operator+(const Duration& lhs, const Duration& rhs)
- {
- return Duration{ lhs.Hour() + rhs.Hour(),lhs.Min() + rhs.Min() };
- }
- int main()
- {
- **************************************************************************
- vector <Duration> durations;
- durations.push_back(Duration{ 1,50 });
- durations.push_back(Duration{ 0,50 });
- durations.push_back(Duration{ 0,1 });
- for (const auto& dur : durations)
- {
- cout << dur << '\t';
- }
- cout << endl;
- sort(durations.begin(), durations.end());
- for (const auto& dur : durations)
- {
- cout << dur << '\t';
- }
- **************************************************************************
- ifstream input("C:/Users/musta/Downloads/text.txt");
- Duration worst;
- map<Duration, string> all;
- if (input)
- {
- Duration dur;
- string name;
- while (input >> dur >> name)
- {
- if (worst < dur)
- {
- worst = dur;
- }
- all[dur] += (name + " "); // если в словаре уже кто-то есть с таким интервалом забега,
- // то мы просто добавлем в строчку имени новое имя и в конец дописываем пробел
- }
- }
- ofstream out("result.txt");
- for (const auto& durationNames : all)
- {
- out << durationNames.first << '\t' << durationNames.second << endl;
- }
- cout << "Worst runner: " << all[worst] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement