This commit is contained in:
Alexander Zhirov 2021-09-24 22:42:29 +03:00
parent 4b2b58862a
commit 587e45f9b3
2 changed files with 192 additions and 54 deletions

View File

@ -1,20 +1,20 @@
Ilin Petr Artemovich 7 17 4559767 Ilin;Petr;Artemovich;7;17;4559767;;
Zaitsev Zakhar Artemovich 125 44 4164751 Zaitsev;Zakhar;Artemovich;125;44;4164751;;
Dubinin Aleksei Mikhailovich 7 473 7449054 Dubinin;Aleksei;Mikhailovich;7;473;7449054;;
Solovev Artur Mikhailovich 4 940 2556793 Solovev;Artur;Mikhailovich;4;940;2556793;;
Gerasimov Miroslav Stanislavovich 7 367 7508887 Gerasimov;Miroslav;Stanislavovich;7;367;7508887;;
Makeev Marat 77 4521 8880876 999 Makeev;Marat;;77;4521;8880876;999;
Solovev Ivan Vladimirovich 7 273 5699819 5543 Solovev;Ivan;Vladimirovich;7;273;5699819;5543;
Egorov Savelii Stanislavovich 77 4521 8880876 99 Egorov;Savelii;Stanislavovich;77;4521;8880876;99;
Sokolov Arsenii 93 163 1992257 16 Sokolov;Arsenii;;93;163;1992257;16;
Davydov Filipp Grigorevich 7 247 1377660 Davydov;Filipp;Grigorevich;7;247;1377660;;
Morozov Vladimir Mikhailovich 37 2290 5613649 Morozov;Vladimir;Mikhailovich;37;2290;5613649;;
Orekhov Matvei Petrovich 81 8281 7420182 2 Orekhov;Matvei;Petrovich;81;8281;7420182;2;
Titova Natalia 93 163 1992257 9 Titova;Natalia;;93;163;1992257;9;
Markelov Dmitrii Vadimovich 19 7576 5734416 2 Markelov;Dmitrii;Vadimovich;19;7576;5734416;2;
Kozlovskii Artem Daniilovich 81 8281 7420182 1 Kozlovskii;Artem;Daniilovich;81;8281;7420182;1;
Kuznetsov Kirill Kirillovich 7 17 8346563 Kuznetsov;Kirill;Kirillovich;7;17;8346563;;
Mironova Margarita Aleksandrovna 7 273 5699819 5542 Mironova;Margarita;Aleksandrovna;7;273;5699819;5542;
Kotov Vasilii Eliseevich 7 367 7508888 Kotov;Vasilii;Eliseevich;7;367;7508888;;
Ivanov Daniil Maksimovich 7 366 7508887 Ivanov;Daniil;Maksimovich;7;366;7508887;;
Aleksandrov Georgii 493 7637 6114861 Aleksandrov;Georgii;;493;7637;6114861;;

View File

@ -11,7 +11,8 @@
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <any> #include <algorithm>
#include <iomanip>
using namespace std; using namespace std;
@ -19,12 +20,29 @@ struct Person
{ {
string firstname; string firstname;
string lastname; string lastname;
optional<string> patronymic = nullopt; optional<string> patronymic;
}; };
optional<string> getOptStr(string &s)
{
if (s == "")
return nullopt;
return s;
}
ostream& operator<<(ostream &out, const Person &p) ostream& operator<<(ostream &out, const Person &p)
{ {
out << p.firstname << ' ' << p.lastname << ' ' << p.patronymic.value_or(""); out << setw(15) << p.firstname << setw(12) << p.lastname;
if (p.patronymic.has_value())
{
out << setw(17) << p.patronymic.value();
}
else
{
out << setw(18);
}
return out; return out;
} }
@ -52,12 +70,20 @@ struct PhoneNumber
int country_code; int country_code;
int city_code; int city_code;
string number; string number;
optional<int> additional_number = nullopt; optional<int> additional_number;
}; };
optional<int> getOptInt(string &s)
{
if (s == "")
return nullopt;
return stoi(s);
}
ostream& operator<<(ostream &out, const PhoneNumber &p) ostream& operator<<(ostream &out, const PhoneNumber &p)
{ {
out << '+' << p.country_code << '(' << p.city_code << ')' << p.number; out << setw(3) << '+' << p.country_code << '(' << p.city_code << ')' << p.number;
if (p.additional_number.has_value()) if (p.additional_number.has_value())
{ {
@ -67,12 +93,10 @@ ostream& operator<<(ostream &out, const PhoneNumber &p)
return out; return out;
} }
using perPhone = vector<pair<Person, PhoneNumber>>;
class PhoneBook class PhoneBook
{ {
private: private:
perPhone m_data; vector<pair<Person, PhoneNumber>> m_data;
public: public:
PhoneBook(ifstream &file) PhoneBook(ifstream &file)
{ {
@ -82,44 +106,42 @@ public:
exit(-1); exit(-1);
} }
while (file) for (string line; getline(file, line);)
{ {
string s; stringstream str(line);
vector<string> rowData;
getline(file, s, '\n'); for (string s; getline(str, s, ';');)
{
stringstream str; rowData.push_back(s);
str << s; }
pair<Person, PhoneNumber> entry; pair<Person, PhoneNumber> entry;
for (int i = 0; str; ++i) for (size_t i = 0; i < rowData.size(); ++i)
{ {
switch (i) switch (i)
{ {
case 0: case 0:
str >> entry.first.firstname; entry.first.firstname = rowData[i];
break; break;
case 1: case 1:
str >> entry.first.lastname; entry.first.lastname = rowData[i];
break; break;
case 2: case 2:
str >> s; entry.first.patronymic = getOptStr(rowData[i]);
entry.first.patronymic = s;
break; break;
case 3: case 3:
str >> entry.second.country_code; entry.second.country_code = stoi(rowData[i]);
break; break;
case 4: case 4:
str >> entry.second.city_code; entry.second.city_code = stoi(rowData[i]);
break; break;
case 5: case 5:
str >> entry.second.number; entry.second.number = rowData[i];
break; break;
case 6: case 6:
int num; entry.second.additional_number = getOptInt(rowData[i]);
str >> num;
entry.second.additional_number = num;
break; break;
} }
} }
@ -128,18 +150,100 @@ public:
} }
} }
friend ostream& operator<<(ostream &out, const PhoneBook &pb); friend ostream& operator<<(ostream &out, const PhoneBook &pb)
};
ostream& operator<<(ostream &out, const PhoneBook &pb)
{
for (const auto& [first, second] : pb.m_data)
{ {
out << first << ' ' << second << endl; for (const auto& [first, second] : pb.m_data)
{
out << first << ' ' << second << endl;
}
return out;
} }
return out; 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;
}
});
}
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;
}
});
}
pair<string, PhoneNumber> GetPhoneNumber(const string &firstname)
{
PhoneNumber searchPhone;
int count = 0;
for_each(m_data.begin(), m_data.end(), [&](const auto &entry)
{
if (entry.first.firstname == firstname)
{
searchPhone = entry.second;
++count;
}
});
switch (count)
{
case 0:
return make_pair("not found", searchPhone);
case 1:
return make_pair("", searchPhone);
default:
return make_pair("found more than 1", searchPhone);
}
}
void ChangePhoneNumber(const Person &p, const PhoneNumber &pn)
{
auto entry = find_if(m_data.begin(), m_data.end(), [&](const auto &entry)
{
return entry.first == p;
});
if (entry != m_data.end())
{
entry->second = pn;
}
}
};
int main() int main()
{ {
@ -147,5 +251,39 @@ int main()
PhoneBook book(file); PhoneBook book(file);
cout << book; cout << book;
cout << "------SortByPhone-------" << endl;
book.SortByPhone();
cout << book;
cout << "------SortByName--------" << endl;
book.SortByName();
cout << book;
cout << "-----GetPhoneNumber-----" << endl;
// лямбда функция, которая принимает фамилию и выводит номер телефона этого человека, либо строку с ошибкой
auto print_phone_number = [&book](const string &surname)
{
cout << surname << "\t";
auto answer = book.GetPhoneNumber(surname);
if (get<0>(answer).empty())
cout << get<1>(answer);
else
cout << get<0>(answer);
cout << endl;
};
// вызовы лямбды
print_phone_number("Ivanov");
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 });
cout << book;
return 0; return 0;
} }