diff --git a/ch10/README.md b/ch10/README.md index eb36c096..032bafcb 100644 --- a/ch10/README.md +++ b/ch10/README.md @@ -69,3 +69,8 @@ The additional one is for the function to be bound itself. ## [Exercise 10.27](ex10_27.cpp) ## [Exercise 10.28](ex10_28.cpp) +## [Exercise 10.29](ex10_29.cpp) +## [Exercise 10.30](ex10_30.cpp) +## [Exercise 10.31](ex10_31.cpp) +## [Exercise 10.32](ex10_32.cpp) +## [Exercise 10.33](ex10_33.cpp) diff --git a/ch10/ex10_29.cpp b/ch10/ex10_29.cpp new file mode 100644 index 00000000..ef4a9724 --- /dev/null +++ b/ch10/ex10_29.cpp @@ -0,0 +1,28 @@ +// +// ex10_29.cpp +// Exercise 10.29 +// +// Created by pezy on 12/13/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Write a program using stream iterators to read a text file into a vector of strings. + + +#include +#include +#include +#include +#include + +using std::string; + +int main() +{ + std::ifstream ifs("../data/book.txt"); + std::istream_iterator in(ifs), eof; + std::vector vec; + std::copy(in, eof, back_inserter(vec)); + + // output + std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator(std::cout, "\n")); +} \ No newline at end of file diff --git a/ch10/ex10_29_30_31.cpp b/ch10/ex10_29_30_31.cpp deleted file mode 100644 index 641eed3a..00000000 --- a/ch10/ex10_29_30_31.cpp +++ /dev/null @@ -1,99 +0,0 @@ -//! @Alan -//! -//! Exercise 10.29: -//! Write a program using stream iterators to read a text file into a vector of strings. -//! -//! Exercise 10.30: -//! Use stream iterators, sort, and copy to read a sequence of integers from the standard -//! input, sort them, and then write them back to the standard output. -//! -//! Exercise 10.31: -//! Update the program from the previous exercise so that it prints only the unique elements. -//! Your program should use unqiue_copy (ยง 10.4.1, p. 403). -//! - -#include -#include -#include -#include -#include -#include -#include - -//! Exercise 10.29 -void -write2vecStr(const std::string &s, std::vector &v); - -//! Excercise 10.30 -void -input2output(std::istream &is, std::ostream &os); - -//! Excercise 10.31 -void input2output_unique(std::istream &is, std::ostream &os); -int main() -{ - //! test for ex10.29 - std::vector v; - write2vecStr("test.txt",v); - - std::ostream_iterator fout_iter(std::cout); - for(auto e :v) - *fout_iter++ = e; - std::cout << "\n=================\n"; - - //! test for ex10.30 - input2output(std::cin,std::cout); - std::cout << "\n=================\n"; - - //! test for ex10.31 - input2output_unique(std::cin, std::cout); - std::cout << "\n=================\n"; - return 0; -} - -//! Exercise 10.29 -void -write2vecStr(const std::string &s, std::vector &v) -{ - std::ifstream fin(s); - std::istream_iterator fin_iter(fin), eof; - while(fin_iter != eof) - { - v.push_back(*fin_iter++); - } -} - -//! Exercise 10.30 -//! read a sequence of integers from the standard -//! input, sort them, and then write them back to the standard output. -void input2output(std::istream &is, std::ostream &os) -{ - std::istream_iterator sin(is), eof; - std::vector v; - //! read into vector - while(sin != eof && *sin != 999) v.push_back(*sin++); - //! sort it - std::sort(v.begin(), v.end()); - - std::ostream_iterator sout(os,"\n"); - std::copy(v.begin(), v.end(), *sout++); -} - -//! Excercise 10.31 -void input2output_unique(std::istream &is, std::ostream &os) -{ - std::istream_iterator sin(is), eof; - std::vector v; - - //! read into vector - while(sin != eof && *sin != 999) - v.push_back(*sin++); - - //! sort it - std::sort(v.begin(), v.end()); - - //! write into output - std::ostream_iterator sout(os,"\n"); - std::unique_copy(v.begin(), v.end(), *sout++); -} - diff --git a/ch10/ex10_30.cpp b/ch10/ex10_30.cpp new file mode 100644 index 00000000..efe9f55e --- /dev/null +++ b/ch10/ex10_30.cpp @@ -0,0 +1,25 @@ +// +// ex10_30.cpp +// Exercise 10.30 +// +// Created by pezy on 12/13/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Use stream iterators, sort, and copy to read a sequence of integers from the standard input, +// sort them, and then write them back to the standard output. + + +#include +#include +#include +#include + +int main() +{ + std::istream_iterator in_iter(std::cin), eof; + std::vector vec; + while (in_iter != eof) + vec.push_back(*in_iter++); + std::sort(vec.begin(), vec.end()); + std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator(std::cout, " ")); +} \ No newline at end of file diff --git a/ch10/ex10_31.cpp b/ch10/ex10_31.cpp new file mode 100644 index 00000000..6897204c --- /dev/null +++ b/ch10/ex10_31.cpp @@ -0,0 +1,25 @@ +// +// ex10_31.cpp +// Exercise 10.31 +// +// Created by pezy on 12/13/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Update the program from the previous exercise so that it prints only the unique elements. +// Your program should use unqiue_copy + + +#include +#include +#include +#include + +int main() +{ + std::istream_iterator in_iter(std::cin), eof; + std::vector vec; + while (in_iter != eof) + vec.push_back(*in_iter++); + std::sort(vec.begin(), vec.end()); + std::unique_copy(vec.cbegin(), vec.cend(), std::ostream_iterator(std::cout, " ")); +} \ No newline at end of file diff --git a/ch10/ex10_32.cpp b/ch10/ex10_32.cpp new file mode 100644 index 00000000..6a362b40 --- /dev/null +++ b/ch10/ex10_32.cpp @@ -0,0 +1,32 @@ +// +// ex10_32.cpp +// Exercise 10.32 +// +// Created by pezy on 12/13/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Rewrite the bookstore problem from 1.6 (p. 24) using a vector to hold the transactions +// and various algorithms to do the processing. +// Use sort with your compareIsbn function from 10.3.1 (p. 387) to arrange the transactions in order, +// and then use find and accumulate to do the sum. + +#include +#include +#include +#include +#include +#include "../include/Sales_item.h" + +int main() +{ + std::istream_iterator in_iter(std::cin), in_eof; + std::vector vec; + + while (in_iter != in_eof) + vec.push_back(*in_iter++); + sort(vec.begin(), vec.end(), compareIsbn); + for (auto beg = vec.cbegin(), end = beg; beg != vec.cend(); beg = end) { + end = find_if(beg, vec.cend(), [beg](const Sales_item &item){return item.isbn() != beg->isbn();}); + std::cout << std::accumulate(beg, end, Sales_item(beg->isbn())) << std::endl; + } +} \ No newline at end of file diff --git a/ch10/ex10_33.cpp b/ch10/ex10_33.cpp index 3856c562..1d35544f 100644 --- a/ch10/ex10_33.cpp +++ b/ch10/ex10_33.cpp @@ -1,47 +1,35 @@ -//! @Alan -//! -//! Exercise 10.33: -//! Write a program that takes the names of an input file and two output files. -//! The input file should hold integers. Using an istream_iterator read the -//! input file. Using ostream_iterators, write the odd numbers into the first -//! output file. Each value should be followed by a space. Write the even numbers -//! into the second file. Each of these values should be placed on a separate line. -// note : many algorithms can be used directly on iostream iterators. -// This excerise can be seen as an example. -//! - +// +// ex10_33.cpp +// Exercise 10.33 +// +// Created by pezy on 12/13/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// Write a program that takes the names of an input file and two output files. +// The input file should hold integers. Using an istream_iterator read the input file. +// Using ostream_iterators, write the odd numbers into the first output file. +// Each value should be followed by a space.Write the even numbers into the second file. +// Each of these values should be placed on a separate line. +// +// Run: ./a.out "../data/input.txt" "../data/odd.txt" "../data/even.txt" #include -#include #include +#include - -void -one2two(const std::string &Ifnm, const std::string &OoddFnm, const std::string &OevenFnm); -int main() -{ - one2two("test_int.txt","odd.txt","even.txt"); - return 0; -} - - -void -one2two(const std::string &Ifnm, const std::string &OoddFnm, const std::string &OevenFnm) +int main(int argc, char **argv) { - //! build input file stream and its iterators. - std::ifstream fin(Ifnm); - std::istream_iterator fin_iter(fin), eof; - - //! build output file streams and their iterators. - std::ofstream fOddOutput(OoddFnm), fEvenOutput(OevenFnm); - std::ostream_iterator fOddOutput_iter(fOddOutput, " "), fEvenOutput_iter(fEvenOutput, "\n"); - - - //! using for_each algorithm to do operation on every element via a lambda - std::for_each(fin_iter, eof, [&](const int i) - { - //! if odd , output into the odd file and incerment the iterator - //! if even , output into the even file and incerment the iterator - i%2 ? *fOddOutput_iter++ = i : *fEvenOutput_iter++ = i; + if (argc != 4) return -1; + + std::ifstream ifs(argv[1]); + std::ofstream ofs_odd(argv[2]), ofs_even(argv[3]); + + std::istream_iterator in(ifs), in_eof; + std::ostream_iterator out_odd(ofs_odd, " "), out_even(ofs_even, "\n"); + + std::for_each(in, in_eof, [&out_odd, &out_even](const int i){ + *(i&0x1 ? out_odd : out_even)++ = i; }); -} + + return 0; +} \ No newline at end of file diff --git a/data/input.txt b/data/input.txt new file mode 100644 index 00000000..95a4bfea --- /dev/null +++ b/data/input.txt @@ -0,0 +1 @@ +1 2 3 4 5 6 7 8 9