C++Primer第五版——习题答案详解(七)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html
第8章 IO库
练习8.1
istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}
练习8.2
#include<iostream>
#include<string>
using namespace std;
istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}
int main() {
iofunc(cin);
return 0;
}
练习8.3
badbit、failbit和eofbit任一个被置位,则检测流状态的条件会失败。
练习8.4
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
int fileToVector(string fileName,vector<string> &svec){
ifstream inFile(fileName);
if (!inFile) {
return 1;
}
string s;
while (getline(inFile, s)) {
svec.push_back(s);
}
inFile.close();
if (inFile.eof()) {
return 4;
}
if (inFile.bad()) {
return 2;
}
if (inFile.fail()) {
return 3;
}
}
int main() {
vector<string> svec;
string fileName, s;
cout << "Enter fileName:" << endl;
cin >> fileName;
switch (fileToVector(fileName, svec))
{
case 1:
cout << "error: can not open file: " << fileName << endl;
return -1;
case 2:
cout << "error: system failure." << endl;
return -1;
case 3:
cout << "error: read failure." << endl;
return -1;
}
cout << "向量里面的内容:" << endl;
for (vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
cout << *iter << endl;
return 0;
}
练习8.5
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int fileToVector(string fileName, vector<string>& svec) {
ifstream inFile(fileName.c_str());
if (!inFile) {
return 1;
}
string s;
//习题8.4一次输入一行
//while (getline(inFile, s)) {
// svec.push_back(s);
//}
//习题8.5一次一个单词
while (inFile >> s) {
svec.push_back(s);
}
inFile.close();
if (inFile.eof()) {
return 4;
}
if (inFile.bad()) {
return 2;
}
if (inFile.fail()) {
return 3;
}
}
int main() {
cout << "测试下" << endl;
vector<string> svec;
string fileName, s;
cout << "Enter filename: ";
cin >> fileName;
switch (fileToVector(fileName,svec))
{
case 1:
cout << "error: can not open file: " << fileName << endl;
return -1;
case 2:
cout << "error: system failure." << endl;
return -1;
case 3:
cout << "error: read failure." << endl;
return -1;
}
cout << "向量里面的内容:" << endl;
for (vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
cout << *iter << endl;
return 0;
}
练习8.6-8.7
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Sales_data {
public:
Sales_data() {}
Sales_data(std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return this->bookNo; }
Sales_data& combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
double avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else return 0;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
public:
std::string bookNo; //书号
unsigned units_sold;
double revenue;
};
istream &read(istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = item.units_sold * price;
return is;
}
ostream &print(ostream &os, const Sales_data &item) {
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price()<<"\n";
return os;
}
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);
cout << endl;
total = trans;
}
}
print(output, total);
cout << endl;
return 0;
}
else
{
cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}

练习8.8
ofstream output(argv[2],ofstream::app);

练习8.9
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}
int main() {
string sss;
cin >> sss;
istringstream iss(sss);
iofunc(iss);
system("pause");
return 0;
}
练习8.10
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
int main() {
string infile = "test.txt";
vector<string> svec;
ifstream in(infile);
if (in) {
string buf;
while (getline(in, buf)) {
svec.push_back(buf);
}
}
else {
cerr << "can not open the file:" << infile << endl;
}
for (auto s : svec) {
istringstream iss(s);
string word;
while (iss >> word) {
cout << word << endl;
}
}
system("pause");
return 0;
}
练习8.11
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
int main() {
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line)) {
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word) {
info.phones.push_back(word);
}
record.clear();
people.push_back(info);
}
for (const auto &entry : people) {
cout << entry.name << " ";
for (const auto &ph : entry.phones) {
cout << ph << " ";
}
cout << endl;
}
return 0;
}
练习8.12
vector和string在定义后自动初始化。
练习8.13
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
int main() {
cout << "Please input the fileName:" << endl;
string infile;
cin >> infile;
ifstream in(infile);
if (!in) {
cerr << "can not open the file: " << infile << endl;
return 0;
}
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(in, line)) {
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word) {
info.phones.push_back(word);
}
record.clear();
people.push_back(info);
}
for (const auto &entry : people) {
cout << entry.name << " ";
for (const auto &ph : entry.phones) {
cout << ph << " ";
}
cout << endl;
}
system("pause");
return 0;
}
练习8.14
无需修改所以用const,另外引用传值更快。
C++Primer第五版——习题答案详解(七)的更多相关文章
- C++Primer第五版——习题答案详解(一)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- C++Primer第五版——习题答案详解(五)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- C++Primer第五版——习题答案详解(八)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...
- C++Primer第五版——习题答案详解(九)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...
- C++Primer第五版——习题答案详解(十)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...
随机推荐
- SSH框架学习摸索记
Unable to load configuration. - bean - jar:file:/E:/tomcat-7.0.11/webapps/struts/WEB-INF/lib/struts ...
- 【网址】.net/dot net可再发行组件下载哪家强
微软开发者文档中有篇文章包含了.net/dot net各版本下载全家福: https://msdn.microsoft.com/zh-cn/library/5a4x27ek.aspx 安全可靠.没有三 ...
- Python中的yield生成器的简单介绍
Python yield 使用浅析(整理自:廖 雪峰, 软件工程师, HP 2012 年 11 月 22 日 ) 初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关 ...
- 二十三. Python基础(23)--经典类和新式类
二十三. Python基础(23)--经典类和新式类 ●知识框架 ●接口类&抽象类的实现 # 接口类&抽象类的实现 #①抛出异常法 class Parent(object): ...
- keras在win下的安装,使用等
http://cache.baiducontent.com/c?m=9d78d513d99a16ef4fece42d4c01d6160e2482744cd7c7637ac3e34a84652b5637 ...
- Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据
效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...
- MacOs 安装cordova报无权访问题解决方案
在MacOS安装cordova后,执行cordova -v报错: Error: EACCES: permission denied, open '/Users/jianuonuo/.config/co ...
- 剑指Offer 55. 链表中环的入口结点 (链表)
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目地址 https://www.nowcoder.com/practice/253d2c59ec3e4bc68d ...
- SpringBoot的学习【1.初学之HelloWorld】
1.创建Maven项目. 2.引入pom依赖 在springboot官网中找到简单的依赖模板 <parent> <groupId>org.springframework.boo ...
- TensorFlow安装教程---windows8.1
首先,第一个,下载,python3.6.4版本 64位 安装python,由于,我是window8.1,所以我遇到这样的问题 参考解决方案:https://answers.microsoft.com/ ...