Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <map>
  7.  
  8. struct record_t {
  9.     std::string name;
  10.     std::string type;
  11.     int year;
  12.     int cost;
  13.     int deaths;
  14. };
  15.  
  16. std::ostream& operator<<(std::ostream& os, const record_t& record) {
  17.     os << "Name:   " << record.name << '\n'
  18.        << "Type:   " << record.type << '\n'
  19.        << "Year:   " << record.year << '\n'
  20.        << "Cost:   " << record.cost << '\n'
  21.        << "Deaths: " << record.deaths << '\n';
  22.     return os;
  23. }
  24.  
  25. using records_t = std::vector<record_t>;
  26.  
  27. records_t load() {
  28.     std::ifstream f{"catastrophe.txt"};
  29.     records_t records;
  30.     std::string s;
  31.     while (std::getline(f, s)) {
  32.         record_t rec;
  33.         auto it = s.begin();
  34.         auto get_next = [&] {
  35.             auto start = it;
  36.             it = std::find(it, s.end(), ',');
  37.             return std::string{ start, it++ };
  38.         };
  39.         auto trim_quotes = [](std::string s) { return s.substr(1, s.length() - 2);  };
  40.         rec.name = trim_quotes(get_next());
  41.         rec.type = trim_quotes(get_next());
  42.         rec.year = std::stoi(get_next());
  43.         rec.cost = std::stof(get_next());
  44.         rec.deaths = std::stoi(std::string{ it, s.end() });
  45.         records.push_back(std::move(rec));
  46.     }
  47.     return records;
  48. }
  49.  
  50. template <class Pred>
  51. void filter_and_display(const records_t& records, Pred pr) {
  52.     bool seen_any = false;
  53.     for (auto& record : records) {
  54.         if (pr(record)) {
  55.             seen_any = true;
  56.             std::cout << record << '\n';
  57.             std::cout << "********\n";
  58.         }
  59.     }
  60.     if (!seen_any) {
  61.         std::cout << "None found\n";
  62.     }
  63. }
  64.  
  65. void display_range(const records_t& records) {
  66.     int start, end;
  67.     std::cout << "Enter starting year: ";
  68.     std::cin >> start;
  69.     std::cout << "Enter ending year: ";
  70.     std::cin >> end;
  71.     if (start > end) {
  72.         std::cout << "Error: End year < start year\n";
  73.         return;
  74.     }
  75.     filter_and_display(records, [=](const record_t& record) {
  76.         return record.year >= start && record.year < end;
  77.     });
  78. }
  79.  
  80. std::string to_lower(std::string s) {
  81.     std::transform(s.begin(), s.end(), s.begin(), std::tolower);
  82.     return s;
  83. }
  84.  
  85. void search(const records_t& records) {
  86.     std::cout << "Enter disaster: ";
  87.     std::string disaster;
  88.     std::cin >> disaster;
  89.     disaster = to_lower(std::move(disaster));
  90.     filter_and_display(records, [&](const record_t& record) {
  91.         return to_lower(record.type) == disaster;
  92.     });
  93. }
  94.  
  95. void summary(const records_t& records) {
  96.     struct totals_t {
  97.         int disasters = 0;
  98.         int deaths = 0;
  99.     };
  100.     std::map<int, totals_t> totals;
  101.     for (const record_t& record : records) {
  102.         auto& total = totals[record.year];
  103.         total.disasters++;
  104.         total.deaths += record.deaths;
  105.     }
  106.     std::ofstream os("report.txt");
  107.     for (auto [year, total] : totals) {
  108.         os << year << " - Disasters: " << total.disasters
  109.                    << " Deaths: " << total.deaths << '\n';
  110.     }
  111. }
  112.  
  113. int main() {
  114.     auto records = load();
  115.  
  116.     bool quit = false;
  117.     while (!quit) {
  118.         std::cout << "1. Display all catastrophes in a year range\n";
  119.         std::cout << "2. Search for a disaster\n";
  120.         std::cout << "3. Summary report\n";
  121.         std::cout << "4. Quit\n" << std::flush;
  122.         int choice;
  123.         std::cin >> choice;
  124.         switch (choice) {
  125.         case 1: display_range(records); break;
  126.         case 2: search(records); break;
  127.         case 3: summary(records); break;
  128.         case 4: quit = true; break;
  129.         default: std::cout << "Invalid choice\n"; break;
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement