diff options
author | Oskar <[email protected]> | 2024-10-06 23:05:52 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-10-06 23:05:52 +0200 |
commit | ee020f14877e5ed1708147a22133e9a413a948f1 (patch) | |
tree | e066b98e81ccab2bc02dc508919447beb17674b0 /8p13.cpp | |
parent | 0921163e1f88c8e80991f0c94e0fc70c6dcba72d (diff) |
more
Diffstat (limited to '8p13.cpp')
-rw-r--r-- | 8p13.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/8p13.cpp b/8p13.cpp new file mode 100644 index 0000000..89de819 --- /dev/null +++ b/8p13.cpp @@ -0,0 +1,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; +} |