Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iterator>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- #include <fstream>
- #include <set>
- #include <list>
- #include <vector>
- #include <string>
- using namespace std;
- struct Pet {
- int number;
- string name;
- vector<string> drivers;
- Pet() {}
- Pet(int number, const string & name, vector<string> & drivers) : name(name), number(number), drivers(drivers) {}
- };
- class Pred {
- private:
- string s;
- public:
- Pred(const string & s) : s(s) {};
- bool operator () (const Pet & t) const {
- return strcmpi(t.name.c_str(), s.c_str()) == 0;
- }
- };
- class MyPred {
- private:
- string s;
- public:
- MyPred(const string & s) : s(s) {};
- bool operator () (const string & t) const {
- return strcmpi(t.c_str(), s.c_str()) == 0;
- }
- };
- class Do {
- private:
- string s;
- public:
- static bool deleted;
- Do(const string & s) : s(s) {}
- void operator () (Pet & t) {
- deleted = 0;
- vector<string> :: iterator it = find_if(t.drivers.begin(), t.drivers.end(), MyPred(s));
- if (it != t.drivers.end()) {
- deleted = 1;
- t.drivers.erase(it);
- }
- }
- };
- bool Do :: deleted = 0;
- inline bool Cmp(const Pet & a, const Pet & b) {
- 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;
- }
- ostream& operator << (ostream & os, const Pet & t) {
- os << "Номер маршрута: " << t.number << "\nНазвание: " << t.name << "\nПеречень водителей: ";
- for (size_t i = 0; i < t.drivers.size(); ++i) os << t.drivers[i] << " ";
- os << endl;
- return os;
- }
- class SetCmp {
- public:
- bool operator () (const string & a, const string & b) {
- return strcmpi(a.c_str(), b.c_str()) < 0;
- }
- };
- int main() {
- setlocale(LC_ALL, ".1251");
- ifstream fin("input.txt");
- if (!fin.is_open()) {
- cout << "Файл не может быть открыт!\n";
- return 0;
- }
- list<Pet> lList;
- int c = 0;
- fin >> c;
- if (!c) {
- cout << "Ошибка чтения файла!\n";
- return 0;
- }
- while (c--) {
- Pet t;
- int g;
- fin >> t.number >> t.name >> g;
- if (g < 0) {
- cout << "Неправильные данные!\n";
- return 0;
- }
- while (g--) {
- string s;
- fin >> s;
- t.drivers.push_back(s);
- }
- lList.push_back(t);
- }
- lList.sort(Cmp);
- cout << "\nЧАСТЬ 1\n\n";
- cout << "Список после сортировки:\n";
- for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) cout << *it << endl;
- cout << "\nЧАСТЬ 2\n\n";
- cout << "\nСписок различных водителей:\n";
- set<string, SetCmp> d;
- for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it)
- for (vector<string> :: iterator jt = it -> drivers.begin(); jt != it -> drivers.end(); ++jt) d.insert(*jt);
- if (d.empty()) cout << "Нет никаких водителей.\n";
- else for (set<string, SetCmp> :: iterator it = d.begin(); it != d.end(); ++it) cout << *it << endl;
- string Name;
- fin >> Name;
- cout << "\nМаршруты, на которых не работает водитель " << Name << ":\n";
- for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) {
- vector<string> & v = it -> drivers;
- if (find_if(v.begin(), v.end(), MyPred(Name)) == v.end()) cout << *it << endl;
- }
- set<string, SetCmp> ans;
- for (vector<string> :: iterator it = lList.begin() -> drivers.begin(); it != lList.begin() -> drivers.end(); ++it)
- ans.insert(*it);
- for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) {
- set<string, SetCmp> temp;
- set<string, SetCmp> drivers;
- for (vector<string> :: iterator jt = it -> drivers.begin(); jt != it -> drivers.end(); ++jt)
- drivers.insert(*jt);
- set_intersection(ans.begin(), ans.end(), drivers.begin(), drivers.end(), inserter(temp, temp.begin()), SetCmp());
- ans = temp;
- }
- cout << "\nВодители, работающие на всех маршрутах:\n";
- if (ans.empty()) cout << "Нет таких водителей\n";
- else for (set<string, SetCmp> :: iterator it = ans.begin(); it != ans.end(); ++it) cout << *it << endl;
- fin >> Name;
- cout << "\nЧАСТЬ 3\n\n";
- cout << "\nКоличество маршрутов, с названием " << Name << ": " << count_if(lList.begin(), lList.end(), Pred(Name)) << endl;
- fin >> Name;
- for_each(lList.begin(), lList.end(), Do(Name));
- cout << "\nУдаление водителя " << Name << "\n";
- if (!Do :: deleted) cout << "Нет такого водителя!\n";
- else for (list<Pet> :: iterator it = lList.begin(); it != lList.end(); ++it) cout << *it << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment