Vserosbuybuy

Practice

Mar 24th, 2021
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. #include <vector>
  5. #include <string>
  6. #include <sstream>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11. class Day {
  12. private:
  13.     int _year;
  14.     int _month;
  15.     int _day_number;
  16.     string _name;
  17. public:
  18.     Day(int year, int month, int day_number): _year(year),
  19.         _month(month), _day_number(day_number) {
  20.         _generateName();
  21.     }
  22.  
  23.     int getDayNumber() const {
  24.         return _day_number;
  25.     }
  26.  
  27.     const string& getName() const {
  28.         return _name;
  29.     }
  30.  
  31.     bool operator<(const Day& d) const {
  32.         return toSeconds() < d.toSeconds();
  33.  
  34.         if (_year == d._year) {
  35.             if (_month == d._month) {
  36.                 return _day_number < d._day_number;
  37.             }
  38.  
  39.             return _month < d._month;
  40.         }
  41.  
  42.         return _year < d._year;
  43.     }
  44.  
  45.     int toSeconds() const {
  46.         return _year * 1000000 + _month * 10000 + _day_number * 10;
  47.     }
  48.  
  49.     friend Day operator+(const Day& a, const Day& b);
  50.     friend istream& operator>>(istream& in, Day& d);
  51.     friend ostream& operator<<(ostream& out, const Day& d);
  52.  
  53. private:
  54.     void _generateName() {
  55.         vector<string> a = { "M", "T", "W", "T", "F", "S", "S" };
  56.         _name = a[_day_number % 7];
  57.     }
  58. };
  59.  
  60. Day operator+(const Day& a, const Day& b) {
  61.     Day result(b._year, a._month, a._day_number + b._day_number);
  62.     return result;
  63. }
  64.  
  65. istream& operator>>(istream& in, Day& d) {
  66.     in >> d._year >> d._month >> d._day_number;
  67.     d._generateName();
  68.     return in;
  69. }
  70.  
  71. ostream& operator<<(ostream& out, const Day& d) {
  72.     out << "Year:\t" << d._year << "\n";
  73.     out << "Month:\t" << d._month << "\n";
  74.     out << "Day number: " << d._day_number << "\n";
  75.     out << "Name:\t" << d._name << "\n\n";
  76.  
  77.     return out;
  78. }
  79.  
  80. struct Time {
  81.     int hour;
  82.     int mins;
  83.     int seconds;
  84.  
  85.     int toSeconds() const {
  86.         return hour * 3600 + mins * 60 + seconds;
  87.     }
  88.  
  89.     bool operator<(const Time& t2) const {
  90.         return toSeconds() < t2.toSeconds();
  91.     }
  92. };
  93.  
  94. struct Earthquake {
  95.     double lat;
  96.     double lon;
  97.     double depth;
  98.     double magnitude;
  99.     string location;
  100. };
  101.  
  102. // 23:59:23
  103. istream& operator>>(istream& in, Time& t) {
  104.     string s;
  105.     getline(in, s, ':');
  106.     t.hour = stoi(s);
  107.     getline(in, s, ':');
  108.     t.mins = stoi(s);
  109.     getline(in, s);
  110.     t.seconds = stoi(s);
  111.  
  112.     return in;
  113. }
  114.  
  115.  
  116. void fillMap(std::multimap<Time, Earthquake>& m) {
  117.     ifstream input("earthquakes.csv");
  118.  
  119.     if (!input.is_open()) {
  120.         cout << "Error.\n";
  121.         return;
  122.     }
  123.  
  124.     string s;
  125.     getline(input, s);
  126.  
  127.     while (getline(input, s)) {
  128.         Time t;
  129.         Earthquake e;
  130.         istringstream s_stream(s);
  131.        
  132.         getline(s_stream, s, ',');
  133.         // s = "23:57:33"
  134.         istringstream s_stream2(s);
  135.         s_stream2 >> t;
  136.        
  137.         getline(s_stream, s, ',');
  138.         e.lat = stod(s);
  139.  
  140.         getline(s_stream, s, ',');
  141.         e.lon = stod(s);
  142.  
  143.         getline(s_stream, s, ',');
  144.         e.depth = stod(s);
  145.  
  146.         getline(s_stream, s, ',');
  147.         e.magnitude = stod(s);
  148.  
  149.         getline(s_stream, s);
  150.         e.location = s;
  151.  
  152.         m.insert({t, e});
  153.     }
  154.  
  155.     input.close();
  156. }
  157.  
  158. void queryEarthquakes(std::multimap<Time, Earthquake>& m,
  159.                       const Time& tstart, const Time& tend,
  160.                       std::string x) {
  161.     auto start = m.lower_bound(tstart);
  162.     for (; start != m.end() && start->first < tend; ++start) {
  163.         if (start->second.location.find(x) != string::npos) {
  164.             cout << start->second;
  165.         }
  166.     }
  167. }
  168.  
  169. int main() {
  170.  
  171.     multimap<Day, int> mp;
  172.     Day d(1980, 90, 3);
  173.     Day d2(1981, 90, 3);
  174.  
  175.     mp.insert({d, 3});
  176.     mp.insert({d, 4});
  177.     mp.insert({d2, 4});
  178.  
  179.     cout << "Before:\n";
  180.     for (auto& x : mp) {
  181.         cout << x.first << " " << x.second << "\n";
  182.     }
  183.  
  184.     mp.erase(mp.find(d));
  185.  
  186.     cout << "After:\n";
  187.     for (auto& x : mp) {
  188.         cout << x.first << " " << x.second << "\n";
  189.     }
  190.  
  191.     return 0;
  192.  
  193.     ifstream input("earthquakes.csv");
  194.  
  195.     string s;
  196.     getline(input, s);
  197.  
  198.     while (getline(input, s)) {
  199.         cout << s << "\n";
  200.         istringstream s_stream(s);
  201.         getline(s_stream, s, ':');
  202.         cout << s << " | ";
  203.         getline(s_stream, s, ',');
  204.         cout << s << " | ";
  205.         getline(s_stream, s, ',');
  206.         cout << s << " | ";
  207.         getline(s_stream, s, ',');
  208.         cout << s << " | \n";
  209.     }
  210.  
  211.     input.close();
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment