c++primer第五版第八章练习

8.1

std::istream &print(std::istream &is)
{
	char ch;
	auto old_state = is.rdstate();
	while (is>>ch)
		std::cout << ch;
	is.setstate(old_state);
	return is;
}

8.2

#include <iostream>
std::istream &print(std::istream &is);
int main()
{
	using namespace std;
	print(cin);
	system("pause");
	return 0;
}
std::istream &print(std::istream &is)
{
	char ch;
	auto old_state = is.rdstate();
	while (is>>ch)
		std::cout << ch;
	is.setstate(old_state);
	return is;
}

8.3

cin输入流处于错误状态:badbit、failbit、eofbit

例如类型与i不匹配、遇到文件结尾等


8.4

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
	using namespace std;
	ifstream ifile(argv[1]);
	if (!ifile)
	{
		cout << "open file error";
		exit(1);
	}
	vector<string> str;
	string temp;
	int i = 0;
	while (getline(ifile, temp))
		str.push_back(temp);
	for (auto &t : str)
		cout << t << endl;
	return 0;
}

8.5

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
	using namespace std;
	ifstream ifile(argv[1]);
	if (!ifile)
	{
		cout << "open file error";
		exit(1);
	}
	vector<string> str;
	string temp;
	int i = 0;
	while (ifile>>temp)
		str.push_back(temp);
	for (auto &t : str)
		cout << t << endl;
	return 0;
}

8.6

#include <iostream>
#include <fstream>
#include <string>
struct Sales_data{
	std::string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};
int main(int argc, char *argv[])
{
	using namespace std;
	ifstream ifile(argv[1]);
	if (!ifile)
	{
		cout << "open file error";
		exit(1);
	}
	Sales_data total;
	if (ifile >> total.bookNo >> total.units_sold >> total.revenue)
	{
		Sales_data trans;
		while (ifile >> trans.bookNo >> trans.units_sold >> trans.revenue) {
			if (total.bookNo == trans.bookNo) {
				total.units_sold += trans.units_sold;
				total.revenue += trans.revenue;
			}
			else {
				cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
				total = trans;
			}
		}
		cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
	}
	else
	{
		std::cerr << "No data?!" << std::endl;
		return -1;
	}
	return 0;
}

8.7

#include <iostream>
#include <fstream>
#include <string>
struct Sales_data{
	std::string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};
int main(int argc, char *argv[])
{
	using namespace std;
	ifstream ifile(argv[1]);
	if (!ifile)
	{
		cout << "open file error";
		exit(1);
	}
	ofstream ofile(argv[2]);
	if (!ofile)
	{
		cout << "open file error";
		exit(1);
	}
	Sales_data total;
	if (ifile >> total.bookNo >> total.units_sold >> total.revenue)
	{
		Sales_data trans;
		while (ifile >> trans.bookNo >> trans.units_sold >> trans.revenue) {
			if (total.bookNo == trans.bookNo) {
				total.units_sold += trans.units_sold;
				total.revenue += trans.revenue;
			}
			else {
				ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
				total = trans;
			}
		}
		ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
	}
	else
	{
		std::cerr << "No data?!" << std::endl;
		return -1;
	}
	return 0;
}

8.8

#include <iostream>
#include <fstream>
#include <string>
struct Sales_data{
	std::string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};
int main(int argc, char *argv[])
{
	using namespace std;
	ifstream ifile(argv[1]);
	if (!ifile)
	{
		cout << "open file error";
		exit(1);
	}
	ofstream ofile(argv[2], ios_base::app);	//追加
	if (!ofile)
	{
		cout << "open file error";
		exit(1);
	}
	Sales_data total;
	if (ifile >> total.bookNo >> total.units_sold >> total.revenue)
	{
		Sales_data trans;
		while (ifile >> trans.bookNo >> trans.units_sold >> trans.revenue) {
			if (total.bookNo == trans.bookNo) {
				total.units_sold += trans.units_sold;
				total.revenue += trans.revenue;
			}
			else {
				ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
				total = trans;
			}
		}
		ofile << total.bookNo << "\t" << total.units_sold << "\t" << total.revenue << endl;
	}
	else
	{
		std::cerr << "No data?!" << std::endl;
		return -1;
	}
	return 0;
}

8.9

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
istream &func(istream &is)
{
	auto olds = is.rdstate();
	string str;
	while (is >> str)
	{
		cout << str;
	}
	is.setstate(olds);
	return is;
}
int main()
{
	string s("c++ c c++p c++pp windows");
	istringstream ssfile(s);
	func(ssfile);
	system("pause");
	return 0;
}


8.10

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

int main(int argc, char **argv)
{
	using namespace std;
	ifstream ifile(argv[1]);
	if (!ifile)
	{
		cerr << "open file error!\n";
		exit(1);
	}
	vector<string> str;
	string st;
	while (getline(ifile, st))
	{
		istringstream iss(st);
		while (iss >> st)
		{
			cout << st << ends;
		}
	}
	return 0;
}


8.11
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct PersonInfo
{
	std::string name;
	std::vector<std::string> phones;
};
int main()
{
	using namespace std;
	istringstream record;
	string line, word;
	vector<PersonInfo> people;
	while (getline(cin, line))
	{
		PersonInfo info;
		record.str(line);
		record >> info.name;
		while (record >> word)
			info.phones.push_back(word);
		people.push_back(info);
		record.clear();
	}
	for (auto &x : people)
	{
		cout << x.name << "\t";
		for (auto a : x.phones)
			cout << a << "\t";
		cout << endl;
	}
	system("pause");
	return 0;
}


8.12
没有意义,这个类作为结构体使用,而且我们需要的是一个空的对象,会给他所有成员赋值,不存在成员空值

8.13

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
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.substr(0, 3) + "-" + str.substr(3, 3) + "-" + str.substr(6);
}

int main(int argc,char **argv)
{
	ifstream ifs(argv[1]);
	if (!ifs)
	{
		cerr << "open file error!" << endl;
		return -1;
	}
	string line, word;
	vector<PersonInfo> people;
	istringstream record;
	while (getline(ifs, line))
	{
		PersonInfo info;
		record.clear();
		record.str(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;
		}
	}
	return 0;
}

9.14

不需要改变数据和反正改变数据

引用会跟更快,因为不需要调用复制构造函数





so~~~~睡觉

2015年11月11日21:46:27 希望明年能够脱单



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值