《C++ primer》第五版习题答案整理——第八章 IO库

8.1 && 8.2

#include <iostream>
using namespace std;

istream& func(istream& is)
{
	string s;
	while (is >> s)
	{
		cout << s << endl;
	}
	cout << is.rdstate() << endl;
	is.clear();
	return is;
}

int main()
{
	istream& is = func(cin);
	cout << is.rdstate() << endl;
}

8.3

一个流一旦发生错误,其上后续的IO操作都会失败。
如果badbit、failbit和eofbit任一个被置位,则检测流状态的条件会失败。

8.4

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
	fstream fstrm;
	fstrm.open("data.txt", fstream::in | fstream::app);
	vector<string> data;
	string str;
	if (fstrm)
	{
		while (getline(fstrm, str))
		{
			data.push_back(str);
		}
	}
	else {
		cout << "file open error!" << endl;
		return -1;
	}

	for (const auto &i:data)
	{
		cout << i << endl;
	}
}

8.5

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
	fstream fstrm;
	vector<string> data;
	string str;
	fstrm.open("data.txt", fstream::in | fstream::app);
	if (fstrm)
	{
		while (fstrm >> str)
		{
			data.push_back(str);
		}
	}
	else {
		cout << "file open error!" << endl;
		return -1;
	}

	for (const auto &i:data)
	{
		cout << i << endl;
	}
}

8.6

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;
	}
}

8.7

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;
	}
}

8.8

int main(int argc, char** argv)
{
	ifstream input(argv[1]);
	ofstream output(argv[2],ofstream::app);
	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;
	}
}

8.9

#include <iostream>
#include <sstream>
using namespace std;

istream& func(istream& is)
{
	string s;
	while (is >> s)
	{
		cout << s << endl;
	}
	cout << is.rdstate() << endl;
	is.clear();
	return is;
}

int main()
{
	istringstream str("hello world\n");
	istream& is = func(str);
	cout << is.rdstate() << endl;
}

8.10

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;

istringstream& Read(vector<string>& vs, istringstream& is)
{
	for (auto &i: vs)
	{
		is.str(i);
		string temp;
		while (is >> temp)
		{
			cout << temp << endl;
		}
		is.clear();
	}
	return is;
}

int main(int argc, char** argv)
{
	ifstream input(argv[1]);
	vector<string> vs;
	string temp;
	istringstream is;
	if (input)
	{
		while (getline(input, temp))
		{
			vs.push_back(temp);
		}
		Read(vs, is);
		cout << is.rdstate() << endl;
	}
	else {
		cerr << "open file error!" << endl;
	}
}

8.11

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;

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

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

	for (const auto& pe : people)
	{
		cout << pe.name;
		for (const auto& ph : pe.phones)
			cout << " " << ph;
		cout << endl;
	}
}

8.12

使用聚合类已满足要求。
不确定每个人号码的数量,无需使用类内初始化。

8.13

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;

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

bool valid(const string& str)
{
	return isdigit(str[0]);		//首字符不是数字则无效
}

string format(const string& str)	//号码格式化
{
	return str;
}

int main(int argc, char** argv)
{
	ifstream input(argv[1]);
	string line, word;
	vector<PersonInfo> people;
	
	while (getline(input, line))
	{
		PersonInfo info;
		istringstream record(line);
		record >> info.name;
		while (record >> word)
			info.phones.push_back(word);
		people.push_back(info);
	}

	for (const auto& entry : people)
	{
		ostringstream formatted, badNums;
		for (const auto& nums : entry.phones)
		{
			if (!valid(nums))
				badNums << " " << nums;
			else
				formatted << " " << format(nums);	//号码格式化
		}
		if (badNums.str().empty())
			cout << entry.name << " " << formatted.str() << endl;	//输出名字及格式化的号码
		else
			cerr << "input error: " << entry.name
			<< " invalid number(s) " << badNums.str() << endl;
	}
}

8.14

  • 它们都是类类型,因此使用引用避免拷贝
  • 在循环当中不会改变它们的值,因此用 const
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值