Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <string>
- #include <iostream>
- #include <vector>
- using namespace std;
- // если имя неизвестно, возвращает пустую строку
- string FindNameByYear(const map<int, string>& names, int year, vector<string>& vec) {
- string name; // изначально имя неизвестно
- // перебираем всю историю по возрастанию ключа словаря, то есть в хронологическом порядке
- for (const auto& item : names) {
- // если очередной год не больше данного, обновляем имя
- if (item.first <= year) {
- name = item.second;
- //ВОТ ЗДЕСЬ, ЕСЛИ РАСКОММЕНТИТЬ,
- //ПРОГРАММА КОМПИЛИРУЕТСЯ, НО В ВЫВОДЕ
- //ПУСТО, то есть это место ломает все, что странно,
- //ведь тут только вектор меняется, почему же тогда
- //ломается вообще все??
- /*if (vec.back()!=name) {
- vec.push_back(name);
- }*/
- } else {
- // иначе пора остановиться, так как эта запись и все последующие относятся к будущему
- break;
- }
- }
- return name;
- }
- string PrintVector (vector <string> vec) {
- string ans;
- for (auto i : vec) {
- ans = ans + i;
- }
- return (ans);
- }
- class Person {
- public:
- void ChangeFirstName(int year, const string& first_name) {
- first_names[year] = first_name;
- }
- void ChangeLastName(int year, const string& last_name) {
- last_names[year] = last_name;
- }
- string GetFullNameWithHistory(int year) {
- vector <string> fnames;
- vector <string> lnames;
- // получаем имя и фамилию по состоянию на год year
- const string first_name = FindNameByYear(first_names, year, fnames);
- const string last_name = FindNameByYear(last_names, year, lnames);
- if (first_name.empty() && last_name.empty()) {
- return "Incognito";
- // если неизвестно только имя
- } else if (first_name.empty()) {
- return last_name + " " + PrintVector(lnames) + " with unknown first name";
- // если неизвестна только фамилия
- } else if (last_name.empty()) {
- return first_name + " " + PrintVector(fnames) + " with unknown last name";
- // если известны и имя, и фамилия
- } else {
- return first_name + " " + PrintVector(fnames) + last_name + " " + PrintVector(lnames);
- }
- }
- private:
- map<int, string> first_names;
- map<int, string> last_names;
- };
- int main() {
- Person person;
- person.ChangeFirstName(1900, "Eugene");
- person.ChangeLastName(1900, "Sokolov");
- person.ChangeLastName(1910, "Sokolov");
- person.ChangeFirstName(1920, "Evgeny");
- person.ChangeLastName(1930, "Sokolov");
- cout << person.GetFullNameWithHistory(1920) << endl;
- return 0;
- }
- //тут ещё один пример функции main для теста
- /*int main() {
- Person person;
- person.ChangeFirstName(1965, "Polina");
- person.ChangeLastName(1967, "Sergeeva");
- for (int year : {1900, 1965, 1990}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeFirstName(1970, "Appolinaria");
- for (int year : {1969, 1970}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeLastName(1968, "Volkova");
- for (int year : {1969, 1970}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeFirstName(1990, "Polina");
- person.ChangeLastName(1990, "Volkova-Sergeeva");
- cout << person.GetFullNameWithHistory(1990) << endl;
- person.ChangeFirstName(1966, "Pauline");
- cout << person.GetFullNameWithHistory(1966) << endl;
- person.ChangeLastName(1960, "Sergeeva");
- for (int year : {1960, 1967}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeLastName(1961, "Ivanova");
- cout << person.GetFullNameWithHistory(1967) << endl;
- return 0;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement