Tranvick

KR_Var2

Mar 9th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iterator>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <set>
  8. #include <list>
  9. #include <vector>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. struct Pet {
  15.     int number;
  16.     string name;
  17.     vector<string> drivers;
  18.     Pet() {}
  19.     Pet(int number, const string & name, vector<string> & drivers) : name(name), number(number), drivers(drivers) {}
  20. };
  21.  
  22. class Pred {
  23. private:
  24.     string s;
  25. public:
  26.     Pred(const string & s) : s(s) {};
  27.     bool operator () (const Pet & t) const {
  28.         return strcmpi(t.name.c_str(), s.c_str()) == 0;
  29.     }
  30. };
  31.  
  32. class MyPred {
  33. private:
  34.     string s;
  35. public:
  36.     MyPred(const string & s) : s(s) {};
  37.     bool operator () (const string & t) const {
  38.         return strcmpi(t.c_str(), s.c_str()) == 0;
  39.     }
  40. };
  41.  
  42. class Do {
  43. private:
  44.     string s;
  45. public:
  46.     static bool deleted;
  47.     Do(const string & s) : s(s) {}
  48.     void operator () (Pet & t) {
  49.         deleted = 0;
  50.         vector<string> :: iterator it = find_if(t.drivers.begin(), t.drivers.end(), MyPred(s));
  51.         if (it != t.drivers.end()) {
  52.             deleted = 1;
  53.             t.drivers.erase(it);
  54.         }
  55.     }
  56. };
  57. bool Do :: deleted = 0;
  58.  
  59. inline bool Cmp(const Pet & a, const Pet & b) {
  60.     return strcmpi(a.name.c_str(), b.name.c_str()) < 0 || strcmpi(a.name.c_str(), b.name.c_str()) == 0 && a.number < b.number;
  61. }
  62.  
  63. ostream& operator << (ostream & os, const Pet & t) {
  64.     os << "Номер маршрута: " << t.number << "\nНазвание: " << t.name << "\nПеречень водителей: ";
  65.     for (size_t i = 0; i < t.drivers.size(); ++i) os << t.drivers[i] << " ";
  66.     os << endl;
  67.     return os;
  68. }
  69.  
  70. class SetCmp {
  71. public:
  72.     bool operator () (const string & a, const string & b) {
  73.         return strcmpi(a.c_str(), b.c_str()) < 0;
  74.     }
  75. };
  76.  
  77. int main() {
  78.     setlocale(LC_ALL, ".1251");
  79.     ifstream fin("input.txt");
  80.     if (!fin.is_open()) {
  81.         cout << "Файл не может быть открыт!\n";
  82.         return 0;
  83.     }
  84.     list<Pet> lList;
  85.     int c = 0;
  86.     fin >> c;
  87.     if (!c) {
  88.         cout << "Ошибка чтения файла!\n";
  89.         return 0;
  90.     }
  91.     while (c--) {
  92.         Pet t;
  93.         int g;
  94.         fin >> t.number >> t.name >> g;
  95.         if (g < 0) {
  96.             cout << "Неправильные данные!\n";
  97.             return 0;
  98.         }
  99.         while (g--) {
  100.             string s;
  101.             fin >> s;
  102.             t.drivers.push_back(s);
  103.         }
  104.         lList.push_back(t);
  105.     }
  106.     lList.sort(Cmp);
  107.     cout << "\nЧАСТЬ 1\n\n";
  108.     cout << "Список после сортировки:\n";
  109.     for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) cout << *it << endl;
  110.  
  111.     cout << "\nЧАСТЬ 2\n\n";
  112.     cout << "\nСписок различных водителей:\n";
  113.     set<string, SetCmp> d;
  114.     for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it)
  115.         for (vector<string> :: iterator jt = it -> drivers.begin(); jt != it -> drivers.end(); ++jt) d.insert(*jt);
  116.     if (d.empty()) cout << "Нет никаких водителей.\n";
  117.     else for (set<string, SetCmp> :: iterator it = d.begin(); it != d.end(); ++it) cout << *it << endl;
  118.     string Name;
  119.     fin >> Name;
  120.     cout << "\nМаршруты, на которых не работает водитель " << Name << ":\n";
  121.     for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) {
  122.         vector<string> & v = it -> drivers;
  123.         if (find_if(v.begin(), v.end(), MyPred(Name)) == v.end()) cout << *it << endl;
  124.     }
  125.     set<string, SetCmp> ans;
  126.     for (vector<string> :: iterator it = lList.begin() -> drivers.begin(); it != lList.begin() -> drivers.end(); ++it)
  127.         ans.insert(*it);
  128.     for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) {
  129.         set<string, SetCmp> temp;
  130.         set<string, SetCmp> drivers;
  131.         for (vector<string> :: iterator jt = it -> drivers.begin(); jt != it -> drivers.end(); ++jt)
  132.             drivers.insert(*jt);
  133.         set_intersection(ans.begin(), ans.end(), drivers.begin(), drivers.end(), inserter(temp, temp.begin()), SetCmp());
  134.         ans = temp;
  135.     }
  136.     cout << "\nВодители, работающие на всех маршрутах:\n";
  137.     if (ans.empty()) cout << "Нет таких водителей\n";
  138.     else for (set<string, SetCmp> :: iterator it = ans.begin(); it != ans.end(); ++it) cout << *it << endl;
  139.     fin >> Name;
  140.  
  141.  
  142.     cout << "\nЧАСТЬ 3\n\n";
  143.     cout << "\nКоличество маршрутов, с названием " << Name << ": " << count_if(lList.begin(), lList.end(), Pred(Name)) << endl;
  144.     fin >> Name;
  145.     for_each(lList.begin(), lList.end(), Do(Name));
  146.     cout << "\nУдаление водителя " << Name << "\n";
  147.     if (!Do :: deleted) cout << "Нет такого водителя!\n";
  148.     else for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) cout << *it << endl;
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment