2021-09-24 01:04:01 +00:00
|
|
|
|
/*
|
|
|
|
|
* main.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 22 сент. 2021 г.
|
|
|
|
|
* Author: alexander
|
|
|
|
|
*/
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <optional>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
2021-09-24 19:42:29 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iomanip>
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
struct Person
|
|
|
|
|
{
|
|
|
|
|
string firstname;
|
|
|
|
|
string lastname;
|
2021-09-24 19:42:29 +00:00
|
|
|
|
optional<string> patronymic;
|
2021-09-24 01:04:01 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
optional<string> getOptStr(string &s)
|
|
|
|
|
{
|
|
|
|
|
if (s == "")
|
|
|
|
|
return nullopt;
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 01:04:01 +00:00
|
|
|
|
ostream& operator<<(ostream &out, const Person &p)
|
|
|
|
|
{
|
2021-09-24 19:42:29 +00:00
|
|
|
|
out << setw(15) << p.firstname << setw(12) << p.lastname;
|
|
|
|
|
|
|
|
|
|
if (p.patronymic.has_value())
|
|
|
|
|
{
|
|
|
|
|
out << setw(17) << p.patronymic.value();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
out << setw(18);
|
|
|
|
|
}
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct PhoneNumber
|
|
|
|
|
{
|
|
|
|
|
int country_code;
|
|
|
|
|
int city_code;
|
|
|
|
|
string number;
|
2021-09-24 19:42:29 +00:00
|
|
|
|
optional<int> additional_number;
|
2021-09-24 01:04:01 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
optional<int> getOptInt(string &s)
|
|
|
|
|
{
|
|
|
|
|
if (s == "")
|
|
|
|
|
return nullopt;
|
|
|
|
|
|
|
|
|
|
return stoi(s);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 01:04:01 +00:00
|
|
|
|
ostream& operator<<(ostream &out, const PhoneNumber &p)
|
|
|
|
|
{
|
2021-09-24 19:42:29 +00:00
|
|
|
|
out << setw(3) << '+' << p.country_code << '(' << p.city_code << ')' << p.number;
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
|
|
|
|
if (p.additional_number.has_value())
|
|
|
|
|
{
|
|
|
|
|
out << ' ' << p.additional_number.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PhoneBook
|
|
|
|
|
{
|
|
|
|
|
private:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
vector<pair<Person, PhoneNumber>> m_data;
|
2021-09-24 01:04:01 +00:00
|
|
|
|
public:
|
|
|
|
|
PhoneBook(ifstream &file)
|
|
|
|
|
{
|
|
|
|
|
if (!file)
|
|
|
|
|
{
|
|
|
|
|
cout << "Не удаётся открыть файл!" << endl;
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
for (string line; getline(file, line);)
|
2021-09-24 01:04:01 +00:00
|
|
|
|
{
|
2021-09-24 19:42:29 +00:00
|
|
|
|
stringstream str(line);
|
|
|
|
|
vector<string> rowData;
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
for (string s; getline(str, s, ';');)
|
|
|
|
|
{
|
|
|
|
|
rowData.push_back(s);
|
|
|
|
|
}
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
|
|
|
|
pair<Person, PhoneNumber> entry;
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
for (size_t i = 0; i < rowData.size(); ++i)
|
2021-09-24 01:04:01 +00:00
|
|
|
|
{
|
|
|
|
|
switch (i)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.first.firstname = rowData[i];
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.first.lastname = rowData[i];
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.first.patronymic = getOptStr(rowData[i]);
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 3:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.second.country_code = stoi(rowData[i]);
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 4:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.second.city_code = stoi(rowData[i]);
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 5:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.second.number = rowData[i];
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 6:
|
2021-09-24 19:42:29 +00:00
|
|
|
|
entry.second.additional_number = getOptInt(rowData[i]);
|
2021-09-24 01:04:01 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_data.push_back(entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
friend ostream& operator<<(ostream &out, const PhoneBook &pb)
|
|
|
|
|
{
|
|
|
|
|
for (const auto& [first, second] : pb.m_data)
|
|
|
|
|
{
|
|
|
|
|
out << first << ' ' << second << endl;
|
|
|
|
|
}
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SortByName()
|
2021-09-24 01:04:01 +00:00
|
|
|
|
{
|
2021-09-24 19:42:29 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-24 01:04:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-09-24 01:04:01 +00:00
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
ifstream file("PhoneBook.txt");
|
|
|
|
|
PhoneBook book(file);
|
|
|
|
|
cout << book;
|
|
|
|
|
|
2021-09-24 19:42:29 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2021-09-24 01:04:01 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|