Advertisement
_takumi

Имена_и_фамилии_2.2

Aug 27th, 2019
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Person {
  5. private:
  6.     map<int, string> fn;
  7.     map<int, string> sn;
  8.  
  9.  
  10.     string FirstName(const int &year) {
  11.         string first_name = " with unknown first name";
  12.         for (const pair<int, string> &p : fn) {
  13.             if (p.first <= year)
  14.                 first_name = p.second;
  15.             else
  16.                 break;
  17.         }
  18.         return first_name;
  19.     }
  20.  
  21.  
  22.     string LastName(const int &year) {
  23.             string last_name = "with unknown last name";
  24.             for (const pair<int, string> &p : sn) {
  25.                 if (p.first <= year)
  26.                     last_name = p.second;
  27.                 else
  28.                     break;
  29.             }
  30.             return last_name;
  31.         }
  32.  
  33.  
  34.     bool FirstNameHistoryExists(const int &year){
  35.     int cnt = 0;
  36.     for (const auto &p : fn) {
  37.         if (p.first > year) break;
  38.         else cnt++;
  39.     }
  40.     if (cnt >= 2) return true;
  41.     else return false;
  42. }
  43.  
  44.  
  45.     bool LastNameHistoryExists(const int &year) {
  46.         int cnt = 0;
  47.         for (const auto &p : sn) {
  48.             if (p.first > year)
  49.                 break;
  50.             else
  51.                 cnt++;
  52.         }
  53.         if (cnt >= 2)
  54.             return true;
  55.         else
  56.             return false;
  57.     }
  58.  
  59.  
  60.     void FillFirstNames(vector<string> &v, const int &year) {
  61.         for (const pair<int, string> &p : fn) {
  62.                 if (p.first <= year)
  63.                     v.push_back(p.second);
  64.                 else
  65.                     break;
  66.             }
  67.             reverse(begin(v), end(v));
  68.             v.erase(unique(begin(v), end(v)), end(v));
  69.     }
  70.  
  71.  
  72.     void FillLastNames(vector<string> &v, const int &year) {
  73.     for (const pair<int, string> &p : sn) {
  74.                 if (p.first <= year)
  75.                     v.push_back(p.second);
  76.                 else
  77.                     break;
  78.             }
  79.             reverse(begin(v), end(v));
  80.             v.erase(unique(begin(v), end(v)), end(v));
  81. }
  82. public:
  83.  
  84.  
  85.     void ChangeFirstName(const int &year, const string &first_name) {
  86.         fn[year] = first_name;
  87.     }
  88.  
  89.  
  90.     void ChangeLastName(const int &year, const string &last_name) {
  91.         sn[year] = last_name;
  92.     }
  93.  
  94.  
  95.     string GetFullName(const int &year) {
  96.         if (FirstName(year) == " with unknown first name" && LastName(year) == "with unknown last name")
  97.             return "Incognito";
  98.         if (FirstName(year) == " with unknown first name")
  99.             return LastName(year) + FirstName(year);
  100.         return FirstName(year) + ' ' + LastName(year);
  101.     }
  102.  
  103.  
  104.     string GetFullNameWithHistory(const int &year) {
  105.         string s = GetFullName(year);
  106.         if (s == "Incognito")
  107.             return "Incognito";
  108.         if (FirstName(year) == " with unknown first name") {
  109.             if (!LastNameHistoryExists(year))
  110.                 return LastName(year) + " with unknown first name";
  111.             vector<string> v;
  112.             FillLastNames(v, year);
  113.             s = v[0] + " (" + v[1];
  114.             for (int i = 2; i < v.size(); i++) {
  115.                 s += ", " + v[i];
  116.             }
  117.             s += ") with unknown first name";
  118.             return s;
  119.         }
  120.         if (LastName(year) == "with unknown last name") {
  121.             if (!FirstNameHistoryExists(year))
  122.                 return FirstName(year) + " with unknown last name";
  123.             vector<string> v;
  124.             FillFirstNames(v, year);
  125.             s = v[0] + " (" + v[1];
  126.             for (int i = 2; i < v.size(); i++) {
  127.                 s += ", " + v[i];
  128.             }
  129.             s += ") with unknown last name";
  130.             return s;
  131.         }
  132.         vector<string> v;
  133.         if (FirstNameHistoryExists(year)) {
  134.             FillFirstNames(v, year);
  135.             s = v[0] + " (" + v[1];
  136.             for (int i = 2; i < v.size(); i++) {
  137.                 s += ", " + v[i];
  138.             }
  139.             s += ") ";
  140.         } else
  141.             s = FirstName(year) + ' ';
  142.         v.clear();
  143.         if (LastNameHistoryExists(year)) {
  144.             FillLastNames(v, year);
  145.             s += v[0] + " (" + v[1];
  146.             for (int i = 2; i < v.size(); i++) {
  147.                 s += ", " + v[i];
  148.             }
  149.             s += ")";
  150.         } else
  151.             s += LastName(year);
  152.         return s;
  153.     }
  154. };
  155.  
  156. int main() {
  157.   Person person;
  158.  
  159.   person.ChangeFirstName(1965, "Polina");
  160.   person.ChangeLastName(1967, "Sergeeva");
  161.   for (int year : {1900, 1965, 1990}) {
  162.     cout << person.GetFullNameWithHistory(year) << endl;
  163.   }
  164.  
  165.   person.ChangeFirstName(1970, "Appolinaria");
  166.   for (int year : {1969, 1970}) {
  167.     cout << person.GetFullNameWithHistory(year) << endl;
  168.   }
  169.  
  170.   person.ChangeLastName(1968, "Volkova");
  171.   for (int year : {1969, 1970}) {
  172.     cout << person.GetFullNameWithHistory(year) << endl;
  173.   }
  174.  
  175.   person.ChangeFirstName(1990, "Polina");
  176.   person.ChangeLastName(1990, "Volkova-Sergeeva");
  177.   cout << person.GetFullNameWithHistory(1990) << endl;
  178.  
  179.   person.ChangeFirstName(1966, "Pauline");
  180.   cout << person.GetFullNameWithHistory(1966) << endl;
  181.  
  182.   person.ChangeLastName(1960, "Sergeeva");
  183.   for (int year : {1960, 1967}) {
  184.     cout << person.GetFullNameWithHistory(year) << endl;
  185.   }
  186.  
  187.   person.ChangeLastName(1961, "Ivanova");
  188.   cout << person.GetFullNameWithHistory(1967) << endl;
  189.  
  190.   return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement