Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- using namespace std;
- class Person {
- public:
- Person(const string& first_name, const string& last_name, int year){
- birth_f = first_name;
- first_name_changes[year] = first_name;
- birth_l = last_name;
- last_name_changes[year] = last_name;
- birth_y = year;
- }
- void ChangeFirstName(int year, const string& first_name) {
- // добавить факт изменения имени на first_name в год year
- if(year >= birth_y){
- first_name_changes[year] = first_name;
- }
- }
- void ChangeLastName(int year, const string& last_name) {
- // добавить факт изменения фамилии на last_name в год year
- if(year >= birth_y){
- last_name_changes[year] = last_name;
- }
- }
- string GetFullName(int year) const{
- if(year< birth_y){
- return("No person");
- }
- string first_name="", last_name="",ans="";
- for(auto i : first_name_changes ){
- if(i.first>year) break;
- first_name = i.second;
- }
- for(auto i : last_name_changes ){
- if(i.first>year) break;
- last_name = i.second;
- }
- if(first_name!=""){
- if(last_name!=""){
- ans=first_name;
- ans+=" ";
- ans+=last_name;
- }
- else{
- ans+=first_name;
- ans+=" with unknown last name";
- }
- }
- else{
- if(last_name!=""){
- ans=last_name;
- ans+=" with unknown first name";
- }
- else{
- ans="Incognito";
- }
- }
- return (ans);
- }
- string GetFullNameWithHistory(int year)const {
- if(year< birth_y){
- return("No person");
- }
- vector <pair<int,string> > f_history, l_history;
- string first_name="", last_name="",ans="";
- string current_f="", current_l="";
- for(auto i : first_name_changes ){
- if(i.first>year) break;
- first_name = i.second;
- f_history.push_back(i);
- }
- for(auto i : last_name_changes ){
- if(i.first>year) break;
- last_name = i.second;
- l_history.push_back(i);
- }
- vector <string> res1, res2;
- for(auto i : f_history){
- if(i.first >year) break;
- if(current_f!= i.second){
- res1.push_back(i.second);
- }
- current_f=i.second;
- }
- for(auto i : l_history){
- if(i.first>year) break;
- if(current_l!=i.second){
- res2.push_back(i.second);
- }
- current_l=i.second;
- }
- if(res1.size()){
- if(res2.size()){
- ans+= res1[res1.size()-1];
- ans+=' ';
- res1.pop_back();
- if(res1.size()){
- ans+="(";
- ans+=res1[res1.size()-1];
- res1.pop_back();
- while(res1.size()){
- ans+=", ";
- ans+=res1[res1.size()-1];
- res1.pop_back();
- }
- ans+=") ";
- }
- ans+= res2[res2.size()-1];
- res2.pop_back();
- if(res2.size()){
- ans+=" (";
- ans+=res2[res2.size()-1];
- res2.pop_back();
- while(res2.size()){
- ans+=", ";
- ans+=res2[res2.size()-1];
- res2.pop_back();
- }
- ans+=")";
- }
- }
- else{
- ans+= res1[res1.size()-1];
- ans+=' ';
- res1.pop_back();
- if(res1.size()){
- ans+="(";
- ans+=res1[res1.size()-1];
- res1.pop_back();
- while(res1.size()){
- ans+=", ";
- ans+=res1[res1.size()-1];
- res1.pop_back();
- }
- ans+=") ";
- }
- ans+="with unknown last name";
- }
- }
- else{
- if(res2.size()){
- ans+= res2[res2.size()-1];
- res2.pop_back();
- if(res2.size()){
- ans+=" (";
- ans+=res2[res2.size()-1];
- res2.pop_back();
- while(res2.size()){
- ans+=", ";
- ans+=res2[res2.size()-1];
- res2.pop_back();
- }
- ans+=")";
- }
- ans+=" with unknown first name";
- }
- else{
- ans+="Incognito";
- }
- }
- return (ans);
- }
- private:
- map <int, string> first_name_changes;
- map <int, string> last_name_changes;
- string birth_f, birth_l;
- int birth_y;
- };
- int main() {
- Person person("Polina", "Sergeeva", 1960);
- for (int year : {1959, 1960}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeFirstName(1965, "Appolinaria");
- person.ChangeLastName(1967, "Ivanova");
- for (int year : {1965, 1967}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement