习题答案目录: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,另外引用传值更快。

最新文章

  1. UTFGrid
  2. 从客户端(Content=&quot;&lt;EM &gt;&lt;STRONG &gt;&lt;U &gt;这是测试这...&quot;)中检测到有潜在危险的Request.Form 值。
  3. AAL template: ROI to brain lobe
  4. Android安全机制(2) Android Permission权限控制机制
  5. RMQ(log2储存方法)
  6. windows phone版的一个儿教app
  7. 如何安装altium designer 10
  8. js学习笔记第二篇
  9. Lambda表达式 =&gt;(msdn)
  10. 【转】深入了解android平台的jni---注册native函数
  11. 我想要个pc和手机共有的客户端,就像百度云(iBarn网盘好用)
  12. Yii2.0中场景的使用小记
  13. ctf中常见注入题源码及脚本分析
  14. String painter HDU - 2476 -区间DP
  15. pytorch加载预训练模型参数的方式
  16. 2019西湖论剑web wp
  17. AGC001E BBQ Hard 组合、递推
  18. 41. 包含min函数的栈
  19. wewqe
  20. Duilib Edit编辑框禁止输入中文的方法

热门文章

  1. JavaScript基础知识点总结
  2. zzw原创_解决Could not chdir to home directory /test/bdctool: Permission denied一例
  3. Sql server数据库连接Oracle库的步骤
  4. XSS/XSRF
  5. Mysql 截取日期的方法
  6. Mysql 监控脚本
  7. oracle数据库连接 ORA-12638:身份证明检索失败
  8. C语言冒泡(起泡)排序与选择排序的循环条件区别
  9. leetcode python 010
  10. 用setTimeout模拟setInterval的功能