Skip to content

Commit

Permalink
Merge pull request pezy#80 from pezy/master
Browse files Browse the repository at this point in the history
Finished Chapter 8.
  • Loading branch information
Mooophy committed Nov 28, 2014
2 parents ee25093 + af8757e commit b5e5d25
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 274 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
- [Chapter 4. Expressions](ch04/README.md)
- [Chapter 5. Statements](ch05/README.md)
- [Chapter 6. Functions](ch06/README.md)
- [Chapter 7. Classes](ch07)
- [Chapter 8. The IO Library](ch08)
- [Chapter 7. Classes](ch07/README.md)
- Part II: The C++ Library
- [Chapter 8. The IO Library](ch08/README.md)
- [Chapter 9. Sequential Containers](ch09)
- [Chapter 10. Generic Algorithms](ch10)
- [Chapter 11. Associative Containers](ch11)
Expand Down
47 changes: 47 additions & 0 deletions ch08/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Chapter 8. The IO Library

## Exercise 8.1:
>Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream.
```cpp
istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
```
## [Exercise 8.2](ex8_02.cpp)
## Exercise 8.3:
>What causes the following while to terminate?
```cpp
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)
## [Exercise 8.7](ex8_07.cpp)
## [Exercise 8.8](ex8_08.cpp)
## [Exercise 8.9](ex8_09.cpp)
## [Exercise 8.10](ex8_10.cpp)
## [Exercise 8.11](ex8_11.cpp)

## Exercise 8.12:
>Why didn’t we use in-class initializers in PersonInfo?
Cause we need a aggregate class here. so it should have no in-class initializers.

## [Exercise 8.13](ex8_13.cpp)

## Exercise 8.14:
>Why did we declare entry and nums as const auto &?
- cause they are all class type, not the built-in type. so **reference** more effective.
- output shouldn't change their values. so we added the `const`.
120 changes: 0 additions & 120 deletions ch08/ex8.13.14/main.cpp

This file was deleted.

27 changes: 27 additions & 0 deletions ch08/ex8_02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ex8_02.cpp
// Exercise 8.2
//
// Created by pezy on 11/27/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Test your function by calling it, passing cin as an argument

#include <iostream>
using std::istream;

istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}

int main()
{
istream& is = func(std::cin);
std::cout << is.rdstate() << std::endl;
return 0;
}
37 changes: 37 additions & 0 deletions ch08/ex8_04.cpp
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <string>
#include <vector>
#include <iostream>

using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl;

void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (std::getline(ifs, buf))
vec.push_back(buf);
}
}

int main()
{
vector<string> vec;
ReadFileToVec("../data/book.txt", vec);
for (const auto &str : vec)
cout << str << endl;
return 0;
}

37 changes: 37 additions & 0 deletions ch08/ex8_05.cpp
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <string>
#include <vector>
#include <iostream>

using std::vector; using std::string; using std::ifstream; using std::cout; using std::endl;

void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (ifs >> buf)
vec.push_back(buf);
}
}

int main()
{
vector<string> vec;
ReadFileToVec("../data/book.txt", vec);
for (const auto &str : vec)
cout << str << endl;
return 0;
}

47 changes: 47 additions & 0 deletions ch08/ex8_06.cpp
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <iostream>

#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;
}
46 changes: 46 additions & 0 deletions ch08/ex8_07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// ex8_07.cpp
// Exercise 8.7
//
// Created by pezy on 11/28/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Revise the bookstore program from the previous section to write its output to a file.
// Pass the name of that file as a second argument to main.
// @See ex8_06.cpp
// @Run give a parameter: "../data/book.txt ../data/out.txt"

#include <fstream>
#include <iostream>

#include "../ch07/ex7_26.h"
using std::ifstream; using std::ofstream; using std::endl; using std::cerr;

int main(int argc, char **argv)
{
ifstream input(argv[1]);
ofstream output(argv[2]);

Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans))
{
if (total.isbn() == trans.isbn())
total.combine(trans);
else
{
print(output, total) << endl;
total = trans;
}
}
print(output, total) << endl;
}
else
{
cerr << "No data?!" << endl;
}

return 0;
}
Loading

0 comments on commit b5e5d25

Please sign in to comment.