forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split the ex8_4_5.cpp to ex8_04.cpp and ex8_05.cpp
1. use the book.txt of data folder 2. added ex8_06
- Loading branch information
Showing
6 changed files
with
126 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.