#include #include #include #include #include using namespace std; class Five_Year { public: Five_Year(int y) : year((y/5*5)) {} bool operator<(Five_Year const& fy) const { return year < fy.year; } string interval() const { return to_string(year) + "-" + to_string(year+4); } private: int year; }; class Average { public: Average() : sum(0), count(0) {} Average& operator+=(int x) { sum += x; ++count; return *this; } double average() const { return double(sum)/count; } private: int sum; int count; }; int main() { string db; cout << "Enter movie information file: "; cin >> db; ifstream ifs(db); if ( ! ifs ) { cerr << '"' << db << "\" could not be opened." << endl; return 1; } map m; string line; while ( getline(ifs, line) ) { istringstream iss(line); int year; int runtime; if (iss >> year >> runtime) m[year] += runtime; else cerr << "ERROR: Bad read: " << line << endl; } cout << setprecision(1) << fixed; for ( pair const& i : m ) { cout << i.first.interval() << " : " << i.second.average() << endl; } return 0; }