Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- using namespace std;
- struct fullname {
- string firstName;
- string lastName;
- };
- class Person {
- public:
- void ChangeFirstName(int year, const string& first_name) {
- // добавить факт изменения имени на firstName в год year
- changes[year].firstName = first_name;
- }
- void ChangeLastName(int year, const string& last_name) {
- // добавить факт изменения фамилии на lastName в год year
- changes[year].lastName = last_name;
- }
- string GetFullName(int year) {
- // получить имя и фамилию по состоянию на конец года year
- fullname current;
- for (auto x : changes){
- if (year < x.first){
- break;
- }else{
- current.firstName = !x.second.firstName.empty() ? x.second.firstName : current.firstName;
- current.lastName = !x.second.lastName.empty() ? x.second.lastName : current.lastName;
- }
- }
- if (!current.firstName.empty() && !current.lastName.empty()){
- return current.firstName + " " + current.lastName;
- } else if (current.firstName.empty() && !current.lastName.empty()){
- return current.lastName + " with unknown first name";
- } else if (!current.firstName.empty() && current.lastName.empty()) {
- return current.firstName + " with unknown last name";
- }
- return "Incognito";
- }
- string GetFullNameWithHistory(int year) {
- // получить все имена и фамилии по состоянию на конец года year
- fullname current;
- fullname res;
- vector<string> Fnames, Lnames;
- string answer;
- answer.clear();
- for (auto x : changes) {
- if (year < x.first) {
- break;
- } else {
- current.firstName = !x.second.firstName.empty() ? x.second.firstName : current.firstName;
- current.lastName = !x.second.lastName.empty() ? x.second.lastName : current.lastName;
- if (current.firstName != res.firstName) {
- Fnames.push_back(current.firstName);
- }
- if (current.lastName != res.lastName) {
- Lnames.push_back(current.lastName);
- }
- res = current;
- }
- }
- if (!Fnames.empty()) {
- Fnames.pop_back();
- }
- if (!Lnames.empty()) {
- Lnames.pop_back();
- }
- reverse(Fnames.begin(),Fnames.end());
- reverse(Lnames.begin(),Lnames.end());
- if (!res.firstName.empty() && !res.lastName.empty()){
- return res.firstName + Gethistory(Fnames) + " " + res.lastName + Gethistory(Lnames);
- } else if (res.firstName.empty() && !res.lastName.empty()){
- return res.lastName + " " + Gethistory(Lnames) + " with unknown first name";
- } else if (!res.firstName.empty() && res.lastName.empty()) {
- return res.firstName + " " + Gethistory(Fnames) + " with unknown last name";
- }
- return "Incognito";
- }
- string Gethistory(vector<string> names){
- string res = " (";
- for (auto x : names){
- res += x + ", ";
- }
- res.pop_back();
- if (res.empty()){
- return res;
- }
- res.pop_back();
- res += ")";
- if (res == ")"){
- res.clear();
- }
- return res;
- }
- // приватные поля
- private:
- map<int, fullname> changes;
- };
- int main() {
- Person person;
- person.ChangeFirstName(1965, "Polina");
- person.ChangeFirstName(1965, "Appolinaria");
- person.ChangeLastName(1965, "Sergeeva");
- person.ChangeLastName(1965, "Volkova");
- person.ChangeLastName(1965, "Volkova-Sergeeva");
- for (int year : {1964, 1965, 1966}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement