1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| #include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/algorithm/string.hpp>
using namespace boost::gregorian;
int main ()
{
setlocale(0, ".1251");
std::ifstream ifs("D:/file.txt"); //Washington 1789-1797 2/22/1732 12/14/1799
if (ifs.is_open())
{
std::string text{std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>()};
std::vector<std::string> lines;
boost::split(lines, text, boost::is_any_of("\n"));
std::string name, date_of_birthday, date_of_die;
boost::regex reg("(\\D*) \\s*\\d{4}-\\d{4}\\s*(\\d{1,2}/\\d{1,2}/\\d{4})\\s*(\\d{1,2}/\\d{1,2}/\\d{4})");
boost::regex reg_date("(\\d{1,2})/(\\d{1,2})/(\\d{4})");
boost::smatch results;
std::for_each(lines.begin(), lines.end(), [&](const std::string& line)
{
if (boost::regex_search(line, results, reg))
{
name = results[1];
date_of_birthday = results[2];
date_of_die = results[3];
date d_birthday, d_die;
if(boost::regex_search(date_of_birthday, results, reg_date))
{
d_birthday = date(from_simple_string(results[3]+"-"+results[1]+"-"+results[2]));
}
if(boost::regex_search(date_of_die, results, reg_date))
{
d_die = date(from_simple_string(results[3]+"-"+results[1]+"-"+results[2]));
}
date_period period(d_birthday, d_die);
size_t count_months = 0; //счетчик месяцов
for (month_iterator it = period.begin() + months(1); it <= period.end(); ++it)
++count_months;
std::cout<<"Имя: "<<name<<", Дата рождения: "<<d_birthday<<", Дата смерти: "<<d_die<<'\n';
std::cout<<"Умер в возврасте: "<<count_months/12<<" лет\n";
std::cout<<"\n########################################################################\n\n";
}
});
}
else
std::cout<<"Error...";
std::cout<<std::endl;
return 0;
} |