Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <math.h>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <map>
- using namespace std;
- class Day {
- private:
- int _year;
- int _month;
- int _day_number;
- string _name;
- public:
- Day(int year, int month, int day_number): _year(year),
- _month(month), _day_number(day_number) {
- _generateName();
- }
- int getDayNumber() const {
- return _day_number;
- }
- const string& getName() const {
- return _name;
- }
- bool operator<(const Day& d) const {
- return toSeconds() < d.toSeconds();
- if (_year == d._year) {
- if (_month == d._month) {
- return _day_number < d._day_number;
- }
- return _month < d._month;
- }
- return _year < d._year;
- }
- int toSeconds() const {
- return _year * 1000000 + _month * 10000 + _day_number * 10;
- }
- friend Day operator+(const Day& a, const Day& b);
- friend istream& operator>>(istream& in, Day& d);
- friend ostream& operator<<(ostream& out, const Day& d);
- private:
- void _generateName() {
- vector<string> a = { "M", "T", "W", "T", "F", "S", "S" };
- _name = a[_day_number % 7];
- }
- };
- Day operator+(const Day& a, const Day& b) {
- Day result(b._year, a._month, a._day_number + b._day_number);
- return result;
- }
- istream& operator>>(istream& in, Day& d) {
- in >> d._year >> d._month >> d._day_number;
- d._generateName();
- return in;
- }
- ostream& operator<<(ostream& out, const Day& d) {
- out << "Year:\t" << d._year << "\n";
- out << "Month:\t" << d._month << "\n";
- out << "Day number: " << d._day_number << "\n";
- out << "Name:\t" << d._name << "\n\n";
- return out;
- }
- struct Time {
- int hour;
- int mins;
- int seconds;
- int toSeconds() const {
- return hour * 3600 + mins * 60 + seconds;
- }
- bool operator<(const Time& t2) const {
- return toSeconds() < t2.toSeconds();
- }
- };
- struct Earthquake {
- double lat;
- double lon;
- double depth;
- double magnitude;
- string location;
- };
- // 23:59:23
- istream& operator>>(istream& in, Time& t) {
- string s;
- getline(in, s, ':');
- t.hour = stoi(s);
- getline(in, s, ':');
- t.mins = stoi(s);
- getline(in, s);
- t.seconds = stoi(s);
- return in;
- }
- void fillMap(std::multimap<Time, Earthquake>& m) {
- ifstream input("earthquakes.csv");
- if (!input.is_open()) {
- cout << "Error.\n";
- return;
- }
- string s;
- getline(input, s);
- while (getline(input, s)) {
- Time t;
- Earthquake e;
- istringstream s_stream(s);
- getline(s_stream, s, ',');
- // s = "23:57:33"
- istringstream s_stream2(s);
- s_stream2 >> t;
- getline(s_stream, s, ',');
- e.lat = stod(s);
- getline(s_stream, s, ',');
- e.lon = stod(s);
- getline(s_stream, s, ',');
- e.depth = stod(s);
- getline(s_stream, s, ',');
- e.magnitude = stod(s);
- getline(s_stream, s);
- e.location = s;
- m.insert({t, e});
- }
- input.close();
- }
- void queryEarthquakes(std::multimap<Time, Earthquake>& m,
- const Time& tstart, const Time& tend,
- std::string x) {
- auto start = m.lower_bound(tstart);
- for (; start != m.end() && start->first < tend; ++start) {
- if (start->second.location.find(x) != string::npos) {
- cout << start->second;
- }
- }
- }
- int main() {
- multimap<Day, int> mp;
- Day d(1980, 90, 3);
- Day d2(1981, 90, 3);
- mp.insert({d, 3});
- mp.insert({d, 4});
- mp.insert({d2, 4});
- cout << "Before:\n";
- for (auto& x : mp) {
- cout << x.first << " " << x.second << "\n";
- }
- mp.erase(mp.find(d));
- cout << "After:\n";
- for (auto& x : mp) {
- cout << x.first << " " << x.second << "\n";
- }
- return 0;
- ifstream input("earthquakes.csv");
- string s;
- getline(input, s);
- while (getline(input, s)) {
- cout << s << "\n";
- istringstream s_stream(s);
- getline(s_stream, s, ':');
- cout << s << " | ";
- getline(s_stream, s, ',');
- cout << s << " | ";
- getline(s_stream, s, ',');
- cout << s << " | ";
- getline(s_stream, s, ',');
- cout << s << " | \n";
- }
- input.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment