diff --git a/ch08/README.md b/ch08/README.md index e7b6d391..d9191b5a 100644 --- a/ch08/README.md +++ b/ch08/README.md @@ -22,3 +22,7 @@ while (cin >> i) /* ... */ ``` putting `cin` in an error state cause to terminate. such as `eofbit`, `failbit` and `badbit`. + +## [Exercise 8.4](ex8_04.cpp) +## [Exercise 8.5](ex8_05.cpp) +## [Exercise 8.6](ex8_06.cpp) diff --git a/ch08/ex8_02.cpp b/ch08/ex8_02.cpp index 326dd22f..957f686f 100644 --- a/ch08/ex8_02.cpp +++ b/ch08/ex8_02.cpp @@ -5,7 +5,7 @@ // Created by pezy on 11/27/14. // Copyright (c) 2014 pezy. All rights reserved. // -// Test your function by calling it, passing cin as an argument +// @Brief Test your function by calling it, passing cin as an argument #include using std::istream; diff --git a/ch08/ex8_04.cpp b/ch08/ex8_04.cpp new file mode 100644 index 00000000..01430f04 --- /dev/null +++ b/ch08/ex8_04.cpp @@ -0,0 +1,37 @@ +// +// ex8_04.cpp +// Exercise 8.4 +// +// Created by pezy on 11/9/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// @Brief Write a function to open a file for input and read its contents into a vector of strings, +// storing each line as a separate element in the vector. + +#include +#include +#include +#include + +using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl; + +void ReadFileToVec(const string& fileName, vector& vec) +{ + ifstream ifs(fileName); + if (ifs) + { + string buf; + while (std::getline(ifs, buf)) + vec.push_back(buf); + } +} + +int main() +{ + vector vec; + ReadFileToVec("../data/book.txt", vec); + for (const auto &str : vec) + cout << str << endl; + return 0; +} + diff --git a/ch08/ex8_05.cpp b/ch08/ex8_05.cpp new file mode 100644 index 00000000..b54658e3 --- /dev/null +++ b/ch08/ex8_05.cpp @@ -0,0 +1,37 @@ +// +// ex8_05.cpp +// Exercise 8.5 +// +// Created by pezy on 11/9/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// @Brief Rewrite the previous program to store each word in a separate element. +// @See ex8_04.cpp + +#include +#include +#include +#include + +using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl; + +void ReadFileToVec(const string& fileName, vector& vec) +{ + ifstream ifs(fileName); + if (ifs) + { + string buf; + while (ifs >> buf) + vec.push_back(buf); + } +} + +int main() +{ + vector vec; + ReadFileToVec("../data/book.txt", vec); + for (const auto &str : vec) + cout << str << endl; + return 0; +} + diff --git a/ch08/ex8_06.cpp b/ch08/ex8_06.cpp new file mode 100644 index 00000000..afd0023c --- /dev/null +++ b/ch08/ex8_06.cpp @@ -0,0 +1,47 @@ +// +// ex8_06.cpp +// Exercise 8.6 +// +// Created by pezy on 11/27/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// @Brief Rewrite the bookstore program from ¡ì7.1.1 (p.256) to read its transactions from a file. +// Pass the name of the file as an argument to main (¡ì6.2.5, p.218). +// @See ex7_26.h (use the Sales_data) +// @Run give a parameter: "../data/book.txt" +// @Output 0-201-78345-X 5 110 +// 0-201-78346-X 9 839.2 + +#include +#include + +#include "../ch07/ex7_26.h" +using std::ifstream; using std::cout; using std::endl; using std::cerr; + +int main(int argc, char **argv) +{ + ifstream input(argv[1]); + + Sales_data total; + if (read(input, total)) + { + Sales_data trans; + while (read(input, trans)) + { + if (total.isbn() == trans.isbn()) + total.combine(trans); + else + { + print(cout, total) << endl; + total = trans; + } + } + print(cout, total) << endl; + } + else + { + cerr << "No data?!" << endl; + } + + return 0; +} diff --git a/ch08/ex8_4_5.cpp b/ch08/ex8_4_5.cpp deleted file mode 100644 index 737fd761..00000000 --- a/ch08/ex8_4_5.cpp +++ /dev/null @@ -1,83 +0,0 @@ -//! @Alan -//! -//! Exercise 8.4: -//! Write a function to open a file for input and read its contents into a vector -//! of strings, storing each line as a separate element in the vector. -// Note: the txt file must be placed at the folder of the exe file rather than the -// source file. -//! -//! Exercise 8.5: -//! Rewrite the previous program to store each word in a separate element. -//! -#include -#include -#include -#include - -std::vector &ex84(const std::string &fn, std::vector &v); -std::vector &ex85(const std::string &fn, std::vector &v); - -int main() -{ - std::vector v; - ex84("test.txt",v); - std::cout << v[0] < w; - w = ex85("test.txt",w); - - for (std::vector::iterator it = w.begin(); it != w.end(); ++it) - { - std::cout<<*it; - } - - return 0; -} - -std::vector &ex85(const std::string &fn, std::vector &v) -{ - std::string word; - std::ifstream fin(fn); - - if(fin.is_open()) - { - std::cout<<"opened\n"; - } - else - { - std::cout<<"failed\n"; - } - - while(fin>>word) - { - v.push_back(word); - } - - return v; -} - - -std::vector &ex84(const std::string &fn, std::vector &v) -{ - std::string line; - std::ifstream fin(fn); - - if(fin.is_open()) - { - std::cout<<"opened\n"; - } - else - { - std::cout<<"failed\n"; - } - - while(std::getline(fin,line)) - { - v.push_back(line); - } - - return v; -} - -