Skip to content

Commit

Permalink
added 10.29 ~ 33 to README.md
Browse files Browse the repository at this point in the history
1. split off 29 30 31 from ex10_29_30_31.cpp
2. added 32
3. simplify 10.33
  • Loading branch information
pezy committed Dec 14, 2014
1 parent ea7d4cf commit d3d32e4
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 140 deletions.
5 changes: 5 additions & 0 deletions ch10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 28 additions & 0 deletions ch10/ex10_29.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>

using std::string;

int main()
{
std::ifstream ifs("../data/book.txt");
std::istream_iterator<string> in(ifs), eof;
std::vector<string> vec;
std::copy(in, eof, back_inserter(vec));

// output
std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator<string>(std::cout, "\n"));
}
99 changes: 0 additions & 99 deletions ch10/ex10_29_30_31.cpp

This file was deleted.

25 changes: 25 additions & 0 deletions ch10/ex10_30.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
std::istream_iterator<int> in_iter(std::cin), eof;
std::vector<int> 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<int>(std::cout, " "));
}
25 changes: 25 additions & 0 deletions ch10/ex10_31.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
std::istream_iterator<int> in_iter(std::cin), eof;
std::vector<int> 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<int>(std::cout, " "));
}
32 changes: 32 additions & 0 deletions ch10/ex10_32.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
#include "../include/Sales_item.h"

int main()
{
std::istream_iterator<Sales_item> in_iter(std::cin), in_eof;
std::vector<Sales_item> 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;
}
}
70 changes: 29 additions & 41 deletions ch10/ex10_33.cpp
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <algorithm>
#include <iterator>
#include <algorithm>


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<int> fin_iter(fin), eof;

//! build output file streams and their iterators.
std::ofstream fOddOutput(OoddFnm), fEvenOutput(OevenFnm);
std::ostream_iterator<int> 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<int> in(ifs), in_eof;
std::ostream_iterator<int> 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;
}
1 change: 1 addition & 0 deletions data/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3 4 5 6 7 8 9

0 comments on commit d3d32e4

Please sign in to comment.