blob: 89de819c896ea246d5842c2ef96568f53f61e86b (
plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <filesystem>
#include <libgen.h>
/*
*
* 8.13
*
*
*/
struct PersonInfo {
std::string name;
std::vector<std::string> phones;
};
int valid(const std::string &nums) {
if(nums[0] == '1') {
return 0;
}
return 1;
}
std::string format(std::string phones) {
return phones;
}
int main (int argc, char **argv) {
if(argc < 2 || argc > 2) {
return -1;
}
std::ifstream file(argv[1]);
if(file.fail()) {
std::cerr << argv[0] << ": no such file '" << basename(argv[1]) << "'" << std::endl;
return -1;
}
std::string line;
std::string word;
std::vector<PersonInfo> people;
while (getline(file, line)) {
PersonInfo info;
std::istringstream record(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (const auto &entry : people) {
std::ostringstream formatted;
std::ostringstream badNums;
for (const auto &nums : entry.phones) {
if (!valid(nums)) {
badNums << " " << nums;
} else
formatted << " " << format(nums);
}
if (badNums.str().empty()) {
std::cout << entry.name << " "
<< formatted.str() << std::endl;
} else {
std::cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << std::endl;
}
}
return 0;
}
|