Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. struct fullname {
  11.     string firstName;
  12.     string lastName;
  13. };
  14.  
  15. class Person {
  16. public:
  17.     void ChangeFirstName(int year, const string& first_name) {
  18.         // добавить факт изменения имени на firstName в год year
  19.         changes[year].firstName = first_name;
  20.  
  21.     }
  22.     void ChangeLastName(int year, const string& last_name) {
  23.         // добавить факт изменения фамилии на lastName в год year
  24.         changes[year].lastName = last_name;
  25.  
  26.     }
  27.     string GetFullName(int year) {
  28.         // получить имя и фамилию по состоянию на конец года year
  29.         fullname current;
  30.  
  31.         for (auto x : changes){
  32.             if (year < x.first){
  33.                 break;
  34.             }else{
  35.                 current.firstName = !x.second.firstName.empty() ? x.second.firstName : current.firstName;
  36.                 current.lastName = !x.second.lastName.empty() ? x.second.lastName : current.lastName;
  37.             }
  38.         }
  39.  
  40.         if  (!current.firstName.empty() && !current.lastName.empty()){
  41.             return current.firstName + " " + current.lastName;
  42.         } else if (current.firstName.empty() && !current.lastName.empty()){
  43.             return  current.lastName + " with unknown first name";
  44.         } else if (!current.firstName.empty() && current.lastName.empty()) {
  45.             return current.firstName + " with unknown last name";
  46.         }
  47.  
  48.         return "Incognito";
  49.     }
  50.  
  51.     string GetFullNameWithHistory(int year) {
  52.         // получить все имена и фамилии по состоянию на конец года year
  53.         fullname current;
  54.         fullname res;
  55.         vector<string> Fnames, Lnames;
  56.         string answer;
  57.         answer.clear();
  58.         for (auto x : changes) {
  59.             if (year < x.first) {
  60.                 break;
  61.             } else {
  62.                 current.firstName = !x.second.firstName.empty() ? x.second.firstName : current.firstName;
  63.                 current.lastName = !x.second.lastName.empty() ? x.second.lastName : current.lastName;
  64.  
  65.                 if (current.firstName != res.firstName) {
  66.                     Fnames.push_back(current.firstName);
  67.                 }
  68.                 if (current.lastName != res.lastName) {
  69.                     Lnames.push_back(current.lastName);
  70.                 }
  71.  
  72.                 res = current;
  73.             }
  74.         }
  75.         if (!Fnames.empty()) {
  76.         Fnames.pop_back();
  77.         }
  78.         if (!Lnames.empty()) {
  79.             Lnames.pop_back();
  80.         }
  81.  
  82.         reverse(Fnames.begin(),Fnames.end());
  83.         reverse(Lnames.begin(),Lnames.end());
  84.  
  85.         if  (!res.firstName.empty() && !res.lastName.empty()){
  86.             return res.firstName + Gethistory(Fnames) + " " + res.lastName + Gethistory(Lnames);
  87.         } else if (res.firstName.empty() && !res.lastName.empty()){
  88.             return  res.lastName + " " + Gethistory(Lnames) + " with unknown first name";
  89.         } else if (!res.firstName.empty() && res.lastName.empty()) {
  90.             return res.firstName + " " + Gethistory(Fnames) + " with unknown last name";
  91.         }
  92.  
  93.         return "Incognito";
  94.     }
  95.  
  96.     string Gethistory(vector<string> names){
  97.         string res = " (";
  98.         for (auto x : names){
  99.             res += x + ", ";
  100.         }
  101.         res.pop_back();
  102.         if (res.empty()){
  103.             return res;
  104.         }
  105.         res.pop_back();
  106.         res += ")";
  107.  
  108.         if (res == ")"){
  109.             res.clear();
  110.         }
  111.  
  112.         return res;
  113.     }
  114.  
  115.     // приватные поля
  116. private:
  117.     map<int, fullname> changes;
  118. };
  119.  
  120.  
  121. int main() {
  122.  
  123.     Person person;
  124.  
  125.     person.ChangeFirstName(1965, "Polina");
  126.     person.ChangeFirstName(1965, "Appolinaria");
  127.  
  128.     person.ChangeLastName(1965, "Sergeeva");
  129.     person.ChangeLastName(1965, "Volkova");
  130.     person.ChangeLastName(1965, "Volkova-Sergeeva");
  131.  
  132.     for (int year : {1964, 1965, 1966}) {
  133.         cout << person.GetFullNameWithHistory(year) << endl;
  134.     }
  135.  
  136.     return 0;
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement