Advertisement
thouxanbanuno

Imena i familii - 3

Oct 26th, 2020
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Person {
  7. public:
  8.     Person(const string& first_name, const string& last_name, int year){
  9.         birth_f = first_name;
  10.         first_name_changes[year] = first_name;
  11.         birth_l = last_name;
  12.         last_name_changes[year] = last_name;
  13.         birth_y = year;
  14.     }
  15.   void ChangeFirstName(int year, const string& first_name) {
  16.     // добавить факт изменения имени на first_name в год year
  17.             if(year >= birth_y){
  18.  
  19.             first_name_changes[year] = first_name;
  20.             }
  21.  
  22.   }
  23.   void ChangeLastName(int year, const string& last_name) {
  24.     // добавить факт изменения фамилии на last_name в год year
  25.             if(year >= birth_y){
  26.  
  27.             last_name_changes[year] = last_name;
  28.             }
  29.  
  30.   }
  31.   string GetFullName(int year) const{
  32.        if(year< birth_y){
  33.             return("No person");
  34.        }
  35.        string first_name="", last_name="",ans="";
  36.        for(auto i : first_name_changes ){
  37.             if(i.first>year) break;
  38.             first_name = i.second;
  39.        }
  40.        for(auto i : last_name_changes ){
  41.             if(i.first>year) break;
  42.             last_name = i.second;
  43.        }
  44.        if(first_name!=""){
  45.             if(last_name!=""){
  46.                 ans=first_name;
  47.                 ans+=" ";
  48.                 ans+=last_name;
  49.             }
  50.             else{
  51.                 ans+=first_name;
  52.                 ans+=" with unknown last name";
  53.             }
  54.        }
  55.        else{
  56.             if(last_name!=""){
  57.                 ans=last_name;
  58.                 ans+=" with unknown first name";
  59.             }
  60.             else{
  61.                 ans="Incognito";
  62.             }
  63.        }
  64.        return (ans);
  65.  
  66.   }
  67.   string GetFullNameWithHistory(int year)const {
  68.       if(year< birth_y){
  69.             return("No person");
  70.        }
  71.         vector <pair<int,string> > f_history, l_history;
  72.        string first_name="", last_name="",ans="";
  73.        string current_f="", current_l="";
  74.        for(auto i : first_name_changes ){
  75.             if(i.first>year) break;
  76.             first_name = i.second;
  77.             f_history.push_back(i);
  78.        }
  79.        for(auto i : last_name_changes ){
  80.             if(i.first>year) break;
  81.             last_name = i.second;
  82.             l_history.push_back(i);
  83.        }
  84.  
  85.         vector <string> res1, res2;
  86.         for(auto i : f_history){
  87.             if(i.first >year) break;
  88.             if(current_f!= i.second){
  89.                 res1.push_back(i.second);
  90.             }
  91.             current_f=i.second;
  92.         }
  93.         for(auto i : l_history){
  94.             if(i.first>year) break;
  95.             if(current_l!=i.second){
  96.                 res2.push_back(i.second);
  97.             }
  98.             current_l=i.second;
  99.         }
  100.         if(res1.size()){
  101.             if(res2.size()){
  102.                     ans+= res1[res1.size()-1];
  103.                     ans+=' ';
  104.                     res1.pop_back();
  105.                     if(res1.size()){
  106.                         ans+="(";
  107.                         ans+=res1[res1.size()-1];
  108.                         res1.pop_back();
  109.  
  110.                     while(res1.size()){
  111.                         ans+=", ";
  112.                         ans+=res1[res1.size()-1];
  113.                         res1.pop_back();
  114.                     }
  115.                     ans+=") ";
  116.                     }
  117.                     ans+= res2[res2.size()-1];
  118.                     res2.pop_back();
  119.                     if(res2.size()){
  120.                         ans+=" (";
  121.                         ans+=res2[res2.size()-1];
  122.                         res2.pop_back();
  123.                     while(res2.size()){
  124.                         ans+=", ";
  125.                         ans+=res2[res2.size()-1];
  126.                         res2.pop_back();
  127.                     }
  128.                     ans+=")";
  129.                     }
  130.  
  131.             }
  132.             else{
  133.                 ans+= res1[res1.size()-1];
  134.                     ans+=' ';
  135.                     res1.pop_back();
  136.                     if(res1.size()){
  137.                         ans+="(";
  138.                         ans+=res1[res1.size()-1];
  139.                         res1.pop_back();
  140.  
  141.                     while(res1.size()){
  142.                         ans+=", ";
  143.                         ans+=res1[res1.size()-1];
  144.                         res1.pop_back();
  145.                     }
  146.                     ans+=") ";
  147.                     }
  148.                 ans+="with unknown last name";
  149.             }
  150.  
  151.         }
  152.         else{
  153.             if(res2.size()){
  154.             ans+= res2[res2.size()-1];
  155.                     res2.pop_back();
  156.                     if(res2.size()){
  157.                         ans+=" (";
  158.                         ans+=res2[res2.size()-1];
  159.                         res2.pop_back();
  160.                     while(res2.size()){
  161.                         ans+=", ";
  162.                         ans+=res2[res2.size()-1];
  163.                         res2.pop_back();
  164.                     }
  165.                     ans+=")";
  166.                     }
  167.                 ans+=" with unknown first name";
  168.             }
  169.             else{
  170.                 ans+="Incognito";
  171.             }
  172.         }
  173.  
  174.  
  175.        return (ans);
  176.   }
  177. private:
  178.   map <int, string> first_name_changes;
  179.   map <int, string> last_name_changes;
  180.   string birth_f, birth_l;
  181.   int birth_y;
  182. };
  183.  
  184. int main() {
  185.   Person person("Polina", "Sergeeva", 1960);
  186.   for (int year : {1959, 1960}) {
  187.     cout << person.GetFullNameWithHistory(year) << endl;
  188.   }
  189.  
  190.   person.ChangeFirstName(1965, "Appolinaria");
  191.   person.ChangeLastName(1967, "Ivanova");
  192.   for (int year : {1965, 1967}) {
  193.     cout << person.GetFullNameWithHistory(year) << endl;
  194.   }
  195.  
  196.   return 0;
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement