From 0921163e1f88c8e80991f0c94e0fc70c6dcba72d Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 6 Oct 2024 21:28:38 +0200 Subject: more --- 8p10.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 8p11.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 8p12.cpp | 13 +++++++++++++ 8p9.cpp | 38 ++++++++++++++++++++++++++++++++++++++ section-8.3.1-program.cpp | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 8p10.cpp create mode 100644 8p11.cpp create mode 100644 8p12.cpp create mode 100644 8p9.cpp create mode 100644 section-8.3.1-program.cpp diff --git a/8p10.cpp b/8p10.cpp new file mode 100644 index 0000000..6b1ce76 --- /dev/null +++ b/8p10.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +/* + * + * 8.10 + * + * + */ + +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] << ": cannot open file '" << argv[1] << "'" << std::endl; + return -1; + } + + std::vector all_lines; + std::string cur_line; + while(getline(file, cur_line)) { + all_lines.push_back(cur_line); + } + + for(auto &ref_all_lines : all_lines) { + std::istringstream is(ref_all_lines); + std::string is_tmp; + while(is >> is_tmp) { + std::cout << is_tmp << "\n"; + } + } + + std::cout << std::ends; + return 0; +} diff --git a/8p11.cpp b/8p11.cpp new file mode 100644 index 0000000..267e692 --- /dev/null +++ b/8p11.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +/* + * + * 8.11 + * + * + */ + +struct PersonInfo { + std::string name; + std::vector phones; +}; + +int main () { + + std::string line; + std::string word; + std::vector people; + std::istringstream record; + while (getline(std::cin, line)) { + PersonInfo info; + record.clear(); + record.str(line); + record >> info.name; // read the name + while (record >> word) { + info.phones.push_back(word); + } + + people.push_back(info); + } + + for(auto &a : people) { + std::cout << a.name << "\n"; + for(auto &b : a.phones) { + std::cout << b << "\n"; + } + } + + return 0; +} diff --git a/8p12.cpp b/8p12.cpp new file mode 100644 index 0000000..48dc542 --- /dev/null +++ b/8p12.cpp @@ -0,0 +1,13 @@ + +/* + * + * 8.12 + * + * + */ + +int main () { + + // Because we don't need them? + return 0; +} diff --git a/8p9.cpp b/8p9.cpp new file mode 100644 index 0000000..5889ca2 --- /dev/null +++ b/8p9.cpp @@ -0,0 +1,38 @@ +#include +#include + +/* + * + * 8.9 + * + * + */ + +std::istream &read_print_report(std::istringstream &rpr) { + + std::string a; + while(rpr >> a) { + std::cout << a; + } + + if(rpr.good()) { std::cout << "good" << std::endl; } + if(rpr.eof()) { std::cout << "eof" << std::endl; } + if(rpr.fail()) { std::cout << "fail" << std::endl; } + if(rpr.bad()) { std::cout << "bad" << std::endl; } + rpr.clear(); + std::cout << "--- cleared --- \n"; + if(rpr.good()) { std::cout << "good" << std::endl; } + if(rpr.eof()) { std::cout << "eof" << std::endl; } + if(rpr.fail()) { std::cout << "fail" << std::endl; } + if(rpr.bad()) { std::cout << "bad" << std::endl; } + return rpr; +} + + +int main () { + + std::string s = "wow i love c++!! This is so cool! I am loving these features, it really feels like C but without having to reimplement everything!"; + std::istringstream a(s); + read_print_report(a); + return 0; +} diff --git a/section-8.3.1-program.cpp b/section-8.3.1-program.cpp new file mode 100644 index 0000000..01e6de4 --- /dev/null +++ b/section-8.3.1-program.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +struct PersonInfo { + std::string name; + std::vector phones; +}; + +int main () { + + std::string line, word; // will hold a line and word from input, respectively + std::vector people; // will hold all the records from the input + // read the input a line at a time until cin hits end-of-file (or another error) + while (getline(std::cin, line)) { + PersonInfo info; + // create an object to hold this record’s data + std::istringstream record(line); // bind record to the line we just read + record >> info.name; // read the name + while (record >> word) + // read the phone numbers + info.phones.push_back(word); // and store them + people.push_back(info); // append this record to people + } + + for(auto &a : people) { + std::cout << a.name << "\n"; + for(auto &b : a.phones) { + std::cout << b << "\n"; + } + } + return 0; +} -- cgit v1.2.3