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