From 4b2b58862acdff8f0af0af554783458b264c435f Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 24 Sep 2021 04:04:01 +0300 Subject: [PATCH 1/3] lesson_1 first commit --- lesson_1/PhoneBook.txt | 20 ++++++ lesson_1/main.cpp | 151 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 lesson_1/PhoneBook.txt create mode 100644 lesson_1/main.cpp diff --git a/lesson_1/PhoneBook.txt b/lesson_1/PhoneBook.txt new file mode 100644 index 0000000..f042824 --- /dev/null +++ b/lesson_1/PhoneBook.txt @@ -0,0 +1,20 @@ +Ilin Petr Artemovich 7 17 4559767 +Zaitsev Zakhar Artemovich 125 44 4164751 +Dubinin Aleksei Mikhailovich 7 473 7449054 +Solovev Artur Mikhailovich 4 940 2556793 +Gerasimov Miroslav Stanislavovich 7 367 7508887 +Makeev Marat 77 4521 8880876 999 +Solovev Ivan Vladimirovich 7 273 5699819 5543 +Egorov Savelii Stanislavovich 77 4521 8880876 99 +Sokolov Arsenii 93 163 1992257 16 +Davydov Filipp Grigorevich 7 247 1377660 +Morozov Vladimir Mikhailovich 37 2290 5613649 +Orekhov Matvei Petrovich 81 8281 7420182 2 +Titova Natalia 93 163 1992257 9 +Markelov Dmitrii Vadimovich 19 7576 5734416 2 +Kozlovskii Artem Daniilovich 81 8281 7420182 1 +Kuznetsov Kirill Kirillovich 7 17 8346563 +Mironova Margarita Aleksandrovna 7 273 5699819 5542 +Kotov Vasilii Eliseevich 7 367 7508888 +Ivanov Daniil Maksimovich 7 366 7508887 +Aleksandrov Georgii 493 7637 6114861 diff --git a/lesson_1/main.cpp b/lesson_1/main.cpp new file mode 100644 index 0000000..2e3602f --- /dev/null +++ b/lesson_1/main.cpp @@ -0,0 +1,151 @@ +/* + * main.cpp + * + * Created on: 22 сент. 2021 г. + * Author: alexander + */ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Person +{ + string firstname; + string lastname; + optional patronymic = nullopt; +}; + +ostream& operator<<(ostream &out, const Person &p) +{ + out << p.firstname << ' ' << p.lastname << ' ' << p.patronymic.value_or(""); + + 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; + optional additional_number = nullopt; +}; + +ostream& operator<<(ostream &out, const PhoneNumber &p) +{ + out << '+' << p.country_code << '(' << p.city_code << ')' << p.number; + + if (p.additional_number.has_value()) + { + out << ' ' << p.additional_number.value(); + } + + return out; +} + +using perPhone = vector>; + +class PhoneBook +{ +private: + perPhone m_data; +public: + PhoneBook(ifstream &file) + { + if (!file) + { + cout << "Не удаётся открыть файл!" << endl; + exit(-1); + } + + while (file) + { + string s; + + getline(file, s, '\n'); + + stringstream str; + str << s; + + pair entry; + + for (int i = 0; str; ++i) + { + switch (i) + { + case 0: + str >> entry.first.firstname; + break; + case 1: + str >> entry.first.lastname; + break; + case 2: + str >> s; + entry.first.patronymic = s; + break; + case 3: + str >> entry.second.country_code; + break; + case 4: + str >> entry.second.city_code; + break; + case 5: + str >> entry.second.number; + break; + case 6: + int num; + str >> num; + entry.second.additional_number = num; + break; + } + } + + m_data.push_back(entry); + } + } + + 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; + } + + return out; +} + +int main() +{ + ifstream file("PhoneBook.txt"); + PhoneBook book(file); + cout << book; + + return 0; +} -- 2.40.1 From 587e45f9b38400708b2dac808cc33f1b3c40998f Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 24 Sep 2021 22:42:29 +0300 Subject: [PATCH 2/3] lesson_1 --- lesson_1/PhoneBook.txt | 40 ++++---- lesson_1/main.cpp | 206 ++++++++++++++++++++++++++++++++++------- 2 files changed, 192 insertions(+), 54 deletions(-) diff --git a/lesson_1/PhoneBook.txt b/lesson_1/PhoneBook.txt index f042824..a4d32c1 100644 --- a/lesson_1/PhoneBook.txt +++ b/lesson_1/PhoneBook.txt @@ -1,20 +1,20 @@ -Ilin Petr Artemovich 7 17 4559767 -Zaitsev Zakhar Artemovich 125 44 4164751 -Dubinin Aleksei Mikhailovich 7 473 7449054 -Solovev Artur Mikhailovich 4 940 2556793 -Gerasimov Miroslav Stanislavovich 7 367 7508887 -Makeev Marat 77 4521 8880876 999 -Solovev Ivan Vladimirovich 7 273 5699819 5543 -Egorov Savelii Stanislavovich 77 4521 8880876 99 -Sokolov Arsenii 93 163 1992257 16 -Davydov Filipp Grigorevich 7 247 1377660 -Morozov Vladimir Mikhailovich 37 2290 5613649 -Orekhov Matvei Petrovich 81 8281 7420182 2 -Titova Natalia 93 163 1992257 9 -Markelov Dmitrii Vadimovich 19 7576 5734416 2 -Kozlovskii Artem Daniilovich 81 8281 7420182 1 -Kuznetsov Kirill Kirillovich 7 17 8346563 -Mironova Margarita Aleksandrovna 7 273 5699819 5542 -Kotov Vasilii Eliseevich 7 367 7508888 -Ivanov Daniil Maksimovich 7 366 7508887 -Aleksandrov Georgii 493 7637 6114861 +Ilin;Petr;Artemovich;7;17;4559767;; +Zaitsev;Zakhar;Artemovich;125;44;4164751;; +Dubinin;Aleksei;Mikhailovich;7;473;7449054;; +Solovev;Artur;Mikhailovich;4;940;2556793;; +Gerasimov;Miroslav;Stanislavovich;7;367;7508887;; +Makeev;Marat;;77;4521;8880876;999; +Solovev;Ivan;Vladimirovich;7;273;5699819;5543; +Egorov;Savelii;Stanislavovich;77;4521;8880876;99; +Sokolov;Arsenii;;93;163;1992257;16; +Davydov;Filipp;Grigorevich;7;247;1377660;; +Morozov;Vladimir;Mikhailovich;37;2290;5613649;; +Orekhov;Matvei;Petrovich;81;8281;7420182;2; +Titova;Natalia;;93;163;1992257;9; +Markelov;Dmitrii;Vadimovich;19;7576;5734416;2; +Kozlovskii;Artem;Daniilovich;81;8281;7420182;1; +Kuznetsov;Kirill;Kirillovich;7;17;8346563;; +Mironova;Margarita;Aleksandrovna;7;273;5699819;5542; +Kotov;Vasilii;Eliseevich;7;367;7508888;; +Ivanov;Daniil;Maksimovich;7;366;7508887;; +Aleksandrov;Georgii;;493;7637;6114861;; diff --git a/lesson_1/main.cpp b/lesson_1/main.cpp index 2e3602f..fa6f9fd 100644 --- a/lesson_1/main.cpp +++ b/lesson_1/main.cpp @@ -11,7 +11,8 @@ #include #include #include -#include +#include +#include using namespace std; @@ -19,12 +20,29 @@ struct Person { string firstname; string lastname; - optional patronymic = nullopt; + optional patronymic; }; +optional getOptStr(string &s) +{ + if (s == "") + return nullopt; + + return s; +} + 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; } @@ -52,12 +70,20 @@ struct PhoneNumber int country_code; int city_code; string number; - optional additional_number = nullopt; + optional additional_number; }; +optional getOptInt(string &s) +{ + if (s == "") + return nullopt; + + return stoi(s); +} + 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()) { @@ -67,12 +93,10 @@ ostream& operator<<(ostream &out, const PhoneNumber &p) return out; } -using perPhone = vector>; - class PhoneBook { private: - perPhone m_data; + vector> m_data; public: PhoneBook(ifstream &file) { @@ -82,44 +106,42 @@ public: exit(-1); } - while (file) + for (string line; getline(file, line);) { - string s; + stringstream str(line); + vector rowData; - getline(file, s, '\n'); - - stringstream str; - str << s; + for (string s; getline(str, s, ';');) + { + rowData.push_back(s); + } pair entry; - for (int i = 0; str; ++i) + for (size_t i = 0; i < rowData.size(); ++i) { switch (i) { case 0: - str >> entry.first.firstname; + entry.first.firstname = rowData[i]; break; case 1: - str >> entry.first.lastname; + entry.first.lastname = rowData[i]; break; case 2: - str >> s; - entry.first.patronymic = s; + entry.first.patronymic = getOptStr(rowData[i]); break; case 3: - str >> entry.second.country_code; + entry.second.country_code = stoi(rowData[i]); break; case 4: - str >> entry.second.city_code; + entry.second.city_code = stoi(rowData[i]); break; case 5: - str >> entry.second.number; + entry.second.number = rowData[i]; break; case 6: - int num; - str >> num; - entry.second.additional_number = num; + entry.second.additional_number = getOptInt(rowData[i]); break; } } @@ -128,18 +150,100 @@ public: } } - friend ostream& operator<<(ostream &out, const PhoneBook &pb); -}; - -ostream& operator<<(ostream &out, const PhoneBook &pb) -{ - for (const auto& [first, second] : pb.m_data) + friend ostream& operator<<(ostream &out, const PhoneBook &pb) { - 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 &p1, + const pair &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 &p1, + const pair &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 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() { @@ -147,5 +251,39 @@ int main() PhoneBook book(file); 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; } -- 2.40.1 From 9e632e8119b7d7cedd6d4944fade7a87ff467501 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Wed, 29 Sep 2021 01:52:51 +0300 Subject: [PATCH 3/3] lesson_1 --- lesson_1/main.cpp | 99 +++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 63 deletions(-) diff --git a/lesson_1/main.cpp b/lesson_1/main.cpp index fa6f9fd..19cf27e 100644 --- a/lesson_1/main.cpp +++ b/lesson_1/main.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -26,7 +25,9 @@ struct Person optional 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 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 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 &p1, - const pair &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 &lhs, const pair &rhs) + { + return lhs.first < rhs.first; + }); } void SortByPhone() { - sort(m_data.begin(), m_data.end(), - [](const pair &p1, - const pair &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 &lhs, const pair &rhs) + { + return lhs.second < rhs.second; + }); } - pair GetPhoneNumber(const string &firstname) + pair> GetPhoneNumber(const string &firstname) { - PhoneNumber searchPhone; + vector 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; -- 2.40.1