-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.cpp
61 lines (54 loc) · 1.35 KB
/
main.cpp
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
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<cstring>
#include<regex>
#include "PersonInfo.h"
bool valid(const std::smatch &m)
{
if(m[1].matched)
return m[3].matched && (m[4].matched == 0 || m[4].str() == " ");
else
return !m[3].matched && m[4].str() == m[6].str();
}
int main()
{
std::string line, word;
std::vector<PersonInfo> people;
std::string phoneRe = "(\\()?(\\d{3})(\\))?[ ]*([-. ])?[ ]*(\\d{3})[ ]*([-. ])?[ ]*(\\d{4})";
std::regex r(phoneRe);
std::smatch m;
while(std::getline(std::cin, line))
{
PersonInfo info;
std::istringstream record(line);
record >> info.name;
word = "";
std::getline(record, word);
for(std::sregex_iterator it(word.begin(), word.end(), r), end_it; it != end_it; ++it)
{
if(!valid(*it))
std::cout << "not valid: " << it->str() << std::endl;
else
{
std::cout << "valid: " << it->str() << std::endl;
info.phones.push_back(it->format("$2.$5.$7 ", std::regex_constants::format_no_copy));
}
}
people.push_back(info);
}
for(const PersonInfo &entry : people)
{
std::cout << entry.name << " ";
if(entry.phones.size() == 1)
std::cout << entry.phones[0];
for(std::vector<std::string>::size_type i = 1; i < entry.phones.size(); ++i)
{
std::cout << entry.phones[i] << " ";
}
std::cout << std::endl;
}
return 0;
}