forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.13.cpp
66 lines (58 loc) · 1.56 KB
/
8.13.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
61
62
63
64
65
66
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
using std::vector; using std::string; using std::cin; using std::cout;
using std::istringstream; using std::ostringstream; using std::endl;
using std::ifstream; using std::getline; using std::cerr;
struct PersonInfo {
string name;
vector<string> phones;
};
constexpr int kPhoneNumberLength = 11;
bool valid(const string &num) {
if (num.size() != kPhoneNumberLength)
return false;
for (const auto &c : num)
if (c < '0' || c > '9')
return false;
return true;
}
string format(const string &num) {
return num.substr(0, 3) + '-' + num.substr(3, 4) + '-' + num.substr(7,4);
}
int main() {
string filename;
cin >> filename;
ifstream in(filename);
if (!in.is_open()) {
std::cerr << "Fail to open file: " << filename << std::endl;
return -1;
}
string line, word;
vector<PersonInfo> people;
while (getline(in, line)) {
istringstream record(line);
PersonInfo info;
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (const auto &entry : people) {
ostringstream formatted, badNums;
for (const auto &nums : entry.phones) {
if (!valid(nums)) {
badNums << " " << nums;
} else
formatted << " " << format(nums);
}
if (badNums.str().empty())
cout << entry.name << " " << formatted.str() << endl;
else
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
return 0;
}