lesson_1 first commit

This commit is contained in:
Alexander Zhirov 2021-09-24 04:04:01 +03:00
parent fe8ca99a6b
commit 4b2b58862a
2 changed files with 171 additions and 0 deletions

20
lesson_1/PhoneBook.txt Normal file
View File

@ -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

151
lesson_1/main.cpp Normal file
View File

@ -0,0 +1,151 @@
/*
* main.cpp
*
* Created on: 22 сент. 2021 г.
* Author: alexander
*/
#include <iostream>
#include <string>
#include <optional>
#include <tuple>
#include <vector>
#include <fstream>
#include <sstream>
#include <any>
using namespace std;
struct Person
{
string firstname;
string lastname;
optional<string> 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<int> 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<pair<Person, PhoneNumber>>;
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<Person, PhoneNumber> 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;
}