Skip to content

Commit

Permalink
🍷 Finished ch8
Browse files Browse the repository at this point in the history
1. ex8_02 should print, fixed.
2. added ex8_09 ex8_11 and ex8_13 cpp file.
3. move data file to `data` folder.
4. simplify @Mooophy's codes.
  • Loading branch information
pezy committed Nov 28, 2014
1 parent 68700c8 commit e9b8ad8
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 191 deletions.
19 changes: 18 additions & 1 deletion ch08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
istream& func(istream &is)
{
std::string buf;
while (is >> buf);
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
Expand All @@ -28,3 +29,19 @@ putting `cin` in an error state cause to terminate. such as `eofbit`, `failbit`
## [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.

3 changes: 2 additions & 1 deletion ch08/ex8_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ using std::istream;
istream& func(istream &is)
{
std::string buf;
while (is >> buf);
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
Expand Down
29 changes: 29 additions & 0 deletions ch08/ex8_09.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ex8_09.cpp
// Exercise 8.9
//
// Created by pezy on 11/29/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Use the function you wrote for the first exercise in § 8.1.2 (p.314) to print the contents of an istringstream object.
// @See Exercise 8.1

#include <iostream>
#include <sstream>
using std::istream;

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

int main()
{
std::istringstream iss("hello");
func(iss);
return 0;
}
97 changes: 28 additions & 69 deletions ch08/ex8_10.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,42 @@
//! @Alan
//!
//! Exercise 8.10:
//! Write a program to store each line from a file in a vector<string>.
//! Now use an istringstream to read each element from the vector a word at a time.
//!
//!

//
// ex8_10.cpp
// Exercise 8.10
//
// Created by pezy on 11/29/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Write a program to store each line from a file in a vector<string>.
// Now use an istringstream to read each element from the vector a word at a time.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

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

std::vector<std::string> &ex84(const std::string &fn, std::vector<std::string> &v);
std::vector<std::string> &ex85(const std::string &fn, std::vector<std::string> &v);

//! Exercise 8.10
int main()
{
std::vector<std::string> v;
v = ex84("test.txt", v);
for (std::vector<std::string>::iterator it = v.begin(); it != v.end(); ++it)
{
std::istringstream sfin(*it);
std::string s;
while(sfin>>s)
{
std::cout<<s;
}

}
return 0;
}

std::vector<std::string> &ex85(const std::string &fn, std::vector<std::string> &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<std::string> &ex84(const std::string &fn, std::vector<std::string> &v)
{
std::string line;
std::ifstream fin(fn);

if(fin.is_open())
{
std::cout<<"opened\n";
}
else
ifstream ifs("../data/book.txt");
if (!ifs)
{
std::cout<<"failed\n";
cerr << "No data?" << endl;
return -1;
}

while(std::getline(fin,line))

vector<string> vecLine;
string line;
while (getline(ifs, line))
vecLine.push_back(line);

for (auto &s : vecLine)
{
v.push_back(line);
istringstream iss(s);
string word;
while (iss >> word)
cout << word << endl;
}

return v;
return 0;
}


48 changes: 48 additions & 0 deletions ch08/ex8_11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// ex8_11.cpp
// Exercise 8.11
//
// Created by pezy on 11/29/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief The program in this section defined its istringstream object inside the outer while loop.
// What changes would you need to make if record were defined outside that loop?
// Rewrite the program, moving the definition of record outside the while, and see whether you thought of all the changes that are needed.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::vector; using std::string; using std::cin; using std::istringstream;

struct PersonInfo {
string name;
vector<string> phones;
};

int main()
{
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}

for (auto &p : people)
{
std::cout << p.name << " ";
for (auto &s : p.phones)
std::cout << s << " ";
std::cout << std::endl;
}

return 0;
}
Loading

0 comments on commit e9b8ad8

Please sign in to comment.