diff options
-rw-r--r-- | 8p11.cpp | 4 | ||||
-rw-r--r-- | 8p13.cpp | 78 | ||||
-rw-r--r-- | 8p14.cpp | 21 |
3 files changed, 101 insertions, 2 deletions
@@ -32,9 +32,9 @@ int main () { people.push_back(info); } - for(auto &a : people) { + for(const auto &a : people) { std::cout << a.name << "\n"; - for(auto &b : a.phones) { + for(const auto &b : a.phones) { std::cout << b << "\n"; } } 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; +} diff --git a/8p14.cpp b/8p14.cpp new file mode 100644 index 0000000..1fc3c1e --- /dev/null +++ b/8p14.cpp @@ -0,0 +1,21 @@ + +/* + * + * 8.14 + * + * + */ + +int main () { + + /* + + They are references because the vectors could be very + large and passing a reference means it does not have to + be copied. + + Const is because the data only needs to be read. + + */ + return 0; +} |