Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include <map>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // если имя неизвестно, возвращает пустую строку
  9. string FindNameByYear(const map<int, string>& names, int year, vector<string>& vec) {
  10. string name; // изначально имя неизвестно
  11.  
  12. // перебираем всю историю по возрастанию ключа словаря, то есть в хронологическом порядке
  13. for (const auto& item : names) {
  14. // если очередной год не больше данного, обновляем имя
  15. if (item.first <= year) {
  16. name = item.second;
  17. //ВОТ ЗДЕСЬ, ЕСЛИ РАСКОММЕНТИТЬ,
  18. //ПРОГРАММА КОМПИЛИРУЕТСЯ, НО В ВЫВОДЕ
  19. //ПУСТО, то есть это место ломает все, что странно,
  20. //ведь тут только вектор меняется, почему же тогда
  21. //ломается вообще все??
  22. /*if (vec.back()!=name) {
  23. vec.push_back(name);
  24. }*/
  25. } else {
  26. // иначе пора остановиться, так как эта запись и все последующие относятся к будущему
  27. break;
  28. }
  29. }
  30.  
  31. return name;
  32. }
  33.  
  34. string PrintVector (vector <string> vec) {
  35. string ans;
  36. for (auto i : vec) {
  37. ans = ans + i;
  38. }
  39. return (ans);
  40. }
  41.  
  42.  
  43. class Person {
  44. public:
  45. void ChangeFirstName(int year, const string& first_name) {
  46. first_names[year] = first_name;
  47. }
  48. void ChangeLastName(int year, const string& last_name) {
  49. last_names[year] = last_name;
  50. }
  51.  
  52. string GetFullNameWithHistory(int year) {
  53. vector <string> fnames;
  54. vector <string> lnames;
  55. // получаем имя и фамилию по состоянию на год year
  56. const string first_name = FindNameByYear(first_names, year, fnames);
  57. const string last_name = FindNameByYear(last_names, year, lnames);
  58. if (first_name.empty() && last_name.empty()) {
  59. return "Incognito";
  60.  
  61. // если неизвестно только имя
  62. } else if (first_name.empty()) {
  63. return last_name + " " + PrintVector(lnames) + " with unknown first name";
  64.  
  65. // если неизвестна только фамилия
  66. } else if (last_name.empty()) {
  67. return first_name + " " + PrintVector(fnames) + " with unknown last name";
  68.  
  69. // если известны и имя, и фамилия
  70. } else {
  71. return first_name + " " + PrintVector(fnames) + last_name + " " + PrintVector(lnames);
  72. }
  73.  
  74. }
  75.  
  76. private:
  77. map<int, string> first_names;
  78. map<int, string> last_names;
  79. };
  80.  
  81. int main() {
  82. Person person;
  83.  
  84. person.ChangeFirstName(1900, "Eugene");
  85. person.ChangeLastName(1900, "Sokolov");
  86. person.ChangeLastName(1910, "Sokolov");
  87. person.ChangeFirstName(1920, "Evgeny");
  88. person.ChangeLastName(1930, "Sokolov");
  89. cout << person.GetFullNameWithHistory(1920) << endl;
  90.  
  91. return 0;
  92. }
  93.  
  94. //тут ещё один пример функции main для теста
  95.  
  96. /*int main() {
  97. Person person;
  98.  
  99. person.ChangeFirstName(1965, "Polina");
  100. person.ChangeLastName(1967, "Sergeeva");
  101. for (int year : {1900, 1965, 1990}) {
  102. cout << person.GetFullNameWithHistory(year) << endl;
  103. }
  104.  
  105. person.ChangeFirstName(1970, "Appolinaria");
  106. for (int year : {1969, 1970}) {
  107. cout << person.GetFullNameWithHistory(year) << endl;
  108. }
  109.  
  110. person.ChangeLastName(1968, "Volkova");
  111. for (int year : {1969, 1970}) {
  112. cout << person.GetFullNameWithHistory(year) << endl;
  113. }
  114.  
  115. person.ChangeFirstName(1990, "Polina");
  116. person.ChangeLastName(1990, "Volkova-Sergeeva");
  117. cout << person.GetFullNameWithHistory(1990) << endl;
  118.  
  119. person.ChangeFirstName(1966, "Pauline");
  120. cout << person.GetFullNameWithHistory(1966) << endl;
  121.  
  122. person.ChangeLastName(1960, "Sergeeva");
  123. for (int year : {1960, 1967}) {
  124. cout << person.GetFullNameWithHistory(year) << endl;
  125. }
  126.  
  127. person.ChangeLastName(1961, "Ivanova");
  128. cout << person.GetFullNameWithHistory(1967) << endl;
  129.  
  130. return 0;
  131. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement