#include #include #include class Activity { public: Activity(int start, int stop, std::string const& descr) : mstart(start), mstop(stop), mdescr(descr) {} Activity(std::istream& in) : mstart(0), mstop(0), mdescr("") { int h, m; char colon, minus; in >> h >> colon >> m; mstart = h*60 + m; in >> minus; in >> h >> colon >> m; mstop = h*60 + m; in >> std::ws; getline(in, mdescr); } bool operator<(Activity const& rhs) const { return mstop < rhs.mstart; } int timeTo(Activity const& rhs) const { return rhs.mstart - mstop; } friend std::ostream& operator<<(std::ostream& os, Activity const& a) { return os << a.mstart/60 << ':' << std::setfill('0') << std::setw(2) << a.mstart%60 << " - " << a.mstop/60 << ':' << std::setfill('0') << std::setw(2) << a.mstop%60 << " " << std::setfill(' ') << a.mdescr; } private: int mstart; int mstop; std::string mdescr; }; #include #include #include #include #include using namespace std; int main(int argc, char* argv[]) { vector schedule; if ( argc != 2 ) { cerr << "ERROR: no file given on command line" << endl; return 1; } ifstream ifs(argv[1]); if ( ! ifs ) { cerr << "ERROR: '" << argv[1] << "' could not be opened" << endl; return 2; } // schedule.emplace_back(8*60, 8*60, "Workday start"); string line; while (getline(ifs, line)) { istringstream iss(line); schedule.emplace_back(iss); } ifs.close(); // schedule.emplace_back(18*60, 18*60, "Workday end"); sort(schedule.begin(), schedule.end()); for (Activity& a : schedule) cout << a << endl; int max = 0; int max_index = -1; for (unsigned int i = 1; i < schedule.size(); ++i) { if (schedule.at(i-1).timeTo(schedule.at(i)) > max) { max = schedule.at(i-1).timeTo(schedule.at(i)); max_index = i; } } if (max_index != -1) { cout << "Largest consecutive block of unbooked time is between: " << endl << schedule.at(max_index-1) << endl << schedule.at(max_index) << endl; } else { cout << "No unbooked time found." << endl; } return 0; }