This commit is contained in:
Alexander Zhirov 2021-09-29 01:52:51 +03:00
parent 587e45f9b3
commit 9e632e8119
1 changed files with 36 additions and 63 deletions

View File

@ -7,7 +7,6 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <optional> #include <optional>
#include <tuple>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@ -26,7 +25,9 @@ struct Person
optional<string> getOptStr(string &s) optional<string> getOptStr(string &s)
{ {
if (s == "") if (s == "")
{
return nullopt; return nullopt;
}
return s; return s;
} }
@ -49,20 +50,12 @@ ostream& operator<<(ostream &out, const Person &p)
bool operator<(const Person &p1, const Person &p2) bool operator<(const Person &p1, const Person &p2)
{ {
string patr1 = p1.patronymic.value_or(""); return tie(p1.firstname, p1.lastname, p1.patronymic) < tie(p2.firstname, p2.lastname, p2.patronymic);
string patr2 = p2.patronymic.value_or("");
return tie(p1.firstname, p1.lastname, patr1)
< tie(p2.firstname, p2.lastname, patr2);
} }
bool operator==(const Person &p1, const Person &p2) bool operator==(const Person &p1, const Person &p2)
{ {
string patr1 = p1.patronymic.value_or(""); return tie(p1.firstname, p1.lastname, p1.patronymic) == tie(p2.firstname, p2.lastname, p2.patronymic);
string patr2 = p2.patronymic.value_or("");
return tie(p1.firstname, p1.lastname, patr1)
== tie(p2.firstname, p2.lastname, patr2);
} }
struct PhoneNumber struct PhoneNumber
@ -73,10 +66,17 @@ struct PhoneNumber
optional<int> additional_number; optional<int> additional_number;
}; };
bool operator<(const PhoneNumber &p1, const PhoneNumber &p2)
{
return tie(p1.country_code, p1.city_code, p1.number, p1.additional_number) < tie(p2.country_code, p2.city_code, p2.number, p2.additional_number);
}
optional<int> getOptInt(string &s) optional<int> getOptInt(string &s)
{ {
if (s == "") if (s == "")
{
return nullopt; return nullopt;
}
return stoi(s); return stoi(s);
} }
@ -162,60 +162,30 @@ public:
void SortByName() void SortByName()
{ {
sort(m_data.begin(), m_data.end(), sort(m_data.begin(), m_data.end(), [](const pair<Person, PhoneNumber> &lhs, const pair<Person, PhoneNumber> &rhs)
[](const pair<Person, PhoneNumber> &p1,
const pair<Person, PhoneNumber> &p2)
{ {
if (p1.first.firstname != p2.first.firstname) return lhs.first < rhs.first;
{
return p1.first.firstname < p2.first.firstname;
}
else if (p1.first.lastname != p2.first.lastname)
{
return p1.first.lastname < p2.first.lastname;
}
else
{
return p1.first.patronymic < p2.first.patronymic;
}
}); });
} }
void SortByPhone() void SortByPhone()
{ {
sort(m_data.begin(), m_data.end(), sort(m_data.begin(), m_data.end(), [](const pair<Person, PhoneNumber> &lhs, const pair<Person, PhoneNumber> &rhs)
[](const pair<Person, PhoneNumber> &p1,
const pair<Person, PhoneNumber> &p2)
{ {
if (p1.second.country_code != p2.second.country_code) return lhs.second < rhs.second;
{
return p1.second.country_code < p2.second.country_code;
}
else if (p1.second.city_code != p2.second.city_code)
{
return p1.second.city_code < p2.second.city_code;
}
else if (p1.second.number != p2.second.number)
{
return p1.second.number < p2.second.number;
}
else
{
return p1.second.additional_number < p2.second.additional_number;
}
}); });
} }
pair<string, PhoneNumber> GetPhoneNumber(const string &firstname) pair<string, vector<PhoneNumber>> GetPhoneNumber(const string &firstname)
{ {
PhoneNumber searchPhone; vector<PhoneNumber> phoneNumbers;
int count = 0; int count = 0;
for_each(m_data.begin(), m_data.end(), [&](const auto &entry) for_each(m_data.begin(), m_data.end(), [&](const auto &entry)
{ {
if (entry.first.firstname == firstname) if (entry.first.firstname == firstname)
{ {
searchPhone = entry.second; phoneNumbers.push_back(entry.second);
++count; ++count;
} }
}); });
@ -223,11 +193,11 @@ public:
switch (count) switch (count)
{ {
case 0: case 0:
return make_pair("not found", searchPhone); return {"not found", phoneNumbers};
case 1: case 1:
return make_pair("", searchPhone); return {"", phoneNumbers};
default: default:
return make_pair("found more than 1", searchPhone); return {"found more than 1", phoneNumbers};
} }
} }
@ -266,9 +236,16 @@ int main()
cout << surname << "\t"; cout << surname << "\t";
auto answer = book.GetPhoneNumber(surname); auto answer = book.GetPhoneNumber(surname);
if (get<0>(answer).empty()) if (get<0>(answer).empty())
cout << get<1>(answer); {
for (size_t i = 0; i < get<1>(answer).size(); ++i)
{
cout << get<1>(answer)[i];
}
}
else else
{
cout << get<0>(answer); cout << get<0>(answer);
}
cout << endl; cout << endl;
}; };
@ -277,12 +254,8 @@ int main()
print_phone_number("Petrov"); print_phone_number("Petrov");
cout << "----ChangePhoneNumber----" << endl; cout << "----ChangePhoneNumber----" << endl;
book.ChangePhoneNumber(Person book.ChangePhoneNumber(Person { "Kotov", "Vasilii", "Eliseevich" }, PhoneNumber { 7, 123, "15344458", nullopt });
{ "Kotov", "Vasilii", "Eliseevich" }, PhoneNumber book.ChangePhoneNumber(Person { "Mironova", "Margarita", "Vladimirovna" }, PhoneNumber { 16, 465, "9155448", 13 });
{ 7, 123, "15344458", nullopt });
book.ChangePhoneNumber(Person
{ "Mironova", "Margarita", "Vladimirovna" }, PhoneNumber
{ 16, 465, "9155448", 13 });
cout << book; cout << book;
return 0; return 0;