Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #import <algorithm>
  2. #include <cctype>
  3. #include <functional>
  4. #include <iostream>
  5. #include <map>
  6. #include <vector>
  7. #include <set>
  8. #include <string>
  9. #include <numeric>
  10.  
  11. using namespace std;
  12.  
  13. class Person
  14. {
  15. public:
  16.     using events = map<int, string>;
  17.  
  18.     void ChangeFirstName(int year, const string &first_name)
  19.     {
  20.         first_names[year] = first_name;
  21.     }
  22.  
  23.     void ChangeLastName(int year, const string &last_name)
  24.     {
  25.         second_names[year] = last_name;
  26.     }
  27.  
  28.     string GetFullName(int year)
  29.     {
  30.         events::const_iterator fn, sn;
  31.         bool fn_status = FindName(first_names, year, fn);
  32.         bool sn_status = FindName(second_names, year, sn);
  33.  
  34.         if (fn_status && sn_status)
  35.             return fn->second + " " + sn->second;
  36.         else if (fn_status && !sn_status)
  37.             return fn->second + " with unknown last name";
  38.         else if (!fn_status && sn_status)
  39.             return sn->second + " with unknown first name";
  40.         else
  41.             return "Incognito";
  42.     }
  43.  
  44.     string GetFullNameWithHistory(int year)
  45.     {
  46.         events::const_iterator fn, sn;
  47.         bool fn_status = FindName(first_names, year, fn);
  48.         bool sn_status = FindName(second_names, year, sn);
  49.  
  50.         if (fn_status && sn_status)
  51.         {
  52.             return fn->second + " " + sn->second;
  53.         }
  54.         else if (fn_status && !sn_status)
  55.         {
  56.             string res = fn->second + " (";
  57.             for (auto it = make_reverse_iterator(fn); it != second_names.rend(); ++it)
  58.                 res += it->second + ", ";
  59.             res[res.size() - 2] = ')';
  60.             return fn->second + "with unknown last name";
  61.         }
  62.         else if (!fn_status && sn_status)
  63.         {
  64.             return sn->second + " with unknown first name";
  65.         }
  66.         else
  67.         {
  68.             return "Incognito";
  69.         }
  70.     }
  71.  
  72. private:
  73.     events first_names, second_names;
  74.  
  75.     bool FindName(const events& event_list, const int& year, events::const_iterator& out)
  76.     {
  77.         bool flag = false;
  78.         if ( event_list.size() == 0)
  79.             return flag;
  80.  
  81.         int last_change = event_list.begin()->first;
  82.         for (auto event = event_list.begin(); event != event_list.end(); ++event)
  83.         {
  84.             if (event->first <= year && event->first >= last_change)
  85.             {
  86.                 out = event;
  87.                 last_change = event->first;
  88.                 flag = true;
  89.             }
  90.         }
  91.         return flag;
  92.     }
  93. };
  94.  
  95. int main() {
  96.     Person person;
  97.  
  98.     person.ChangeFirstName(1965, "Polina");
  99.     person.ChangeLastName(1967, "Sergeeva");
  100.     for (int year : {1900, 1965, 1990}) {
  101.         cout << person.GetFullNameWithHistory(year) << endl;
  102.     }
  103.  
  104.     person.ChangeFirstName(1970, "Appolinaria");
  105.     for (int year : {1969, 1970}) {
  106.         cout << person.GetFullNameWithHistory(year) << endl;
  107.     }
  108.  
  109.     person.ChangeLastName(1968, "Volkova");
  110.     for (int year : {1969, 1970}) {
  111.         cout << person.GetFullNameWithHistory(year) << endl;
  112.     }
  113.  
  114.     person.ChangeFirstName(1990, "Polina");
  115.     person.ChangeLastName(1990, "Volkova-Sergeeva");
  116.     cout << person.GetFullNameWithHistory(1990) << endl;
  117.  
  118.     person.ChangeFirstName(1966, "Pauline");
  119.     cout << person.GetFullNameWithHistory(1966) << endl;
  120.  
  121.     person.ChangeLastName(1960, "Sergeeva");
  122.     for (int year : {1960, 1967}) {
  123.         cout << person.GetFullNameWithHistory(year) << endl;
  124.     }
  125.  
  126.     person.ChangeLastName(1961, "Ivanova");
  127.     cout << person.GetFullNameWithHistory(1967) << endl;
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement