C++ Primer 第五版第八章习题答案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LL596214569/article/details/85223871

 

书籍版本:2019年9月第一版;王刚 杨巨峰译;电子工业出版社

编译器 : win10  && VS2015

8.1

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

istream& streamFunc(istream& in)
{
	string s;
	while (in >> s)
	{
		cout << s << " ";
	}
	cout << endl;
	in.clear();
	return in;
}

int main(int argc, char *argv[])
{
	streamFunc(cin);

	system("pause");
}

8.2

如上题。

8.3

遇到结束符或是输入错误,比如i是int,却输入了string

8.4

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

void readFunc(const string filename, vector<string>& vec)
{
	ifstream in(filename);
	string s;
	while ( getline(in, s) )
	{
		vec.push_back(s);
	}
}

int main(int argc, char *argv[])
{
	string filename = "G:/testt.txt";
	vector<string> strVec;
	readFunc(filename, strVec);

	system("pause");
}

8.5

void readFunc(const string filename, vector<string>& vec)
{
	ifstream in(filename);
	string s;
	while ( in>>s )
	{
		vec.push_back(s);
	}
}

8.6

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "Sales_data.h"
using namespace std;

void readFunc(const string filename, vector<string>& vec)
{
	ifstream in(filename);
	string s;
	while ( in>>s )
	{
		vec.push_back(s);
	}
}

int main(int argc, char** argv)
{
	ifstream ifs(argv[1]);
	if (ifs) 
	{
		CSales_data total;
		if ( read(ifs, total) ) 
		{     
			CSales_data trans;
			while (read(ifs, trans)) 
			{
				if (total.isbn() == trans.isbn()) 
				{
					total.combine(trans);
				}
				else
				{
					print(cout, total) << endl;
					total = trans;
				}
			}
			print(cout, total) << endl;
		}
		else {
			cout << "No data?" << endl;
		}
	}
	else {
		cout << "file name error?" << endl;
	}
	return 0;
}

8.7

int main(int argc, char** argv)
{
	ifstream ifs(argv[1]);
	ofstream ofs(argv[2]);

	if (ifs) 
	{
		CSales_data total;
		if (read(ifs, total)) 
		{
			CSales_data trans;
			while (read(ifs, trans)) 
			{
				if (total.isbn() == trans.isbn()) 
				{
					total.combine(trans);
				}
				else 
				{
					print(ofs, total) << endl;
					total = trans;
				}
			}
			print(ofs, total) << endl;
		}
		else 
		{
			cout << "No data?" << endl;
		}
	}
	else {
		cout << "file name error?" << endl;
	}
	return 0;
}

8.8

int main(int argc, char** argv)
{
	ifstream ifs(argv[1]);
	ofstream ofs(argv[2], ofstream::app);

	if (ifs) 
	{
		CSales_data total;
		if (read(ifs, total)) 
		{
			CSales_data trans;
			while (read(ifs, trans)) 
			{
				if (total.isbn() == trans.isbn()) 
				{
					total.combine(trans);
				}
				else 
				{
					print(ofs, total) << endl;
					total = trans;
				}
			}
			print(ofs, total) << endl;
		}
		else 
		{
			cout << "No data?" << endl;
		}
	}
	else {
		cout << "file name error?" << endl;
	}
	return 0;
}

8.9

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

istream& streamFunc(istream& in)
{
	string s;
	while (in >> s)
	{
		cout << s << " ";
	}
	cout << endl;
	in.clear();
	return in;
}

int main(int argc, char** argv)
{
	string s = "123456";
	istringstream record(s);
	streamFunc(record);

	system("pause");
	return 0;
}

8.10

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

void readFunc(const string filename, vector<string>& vec)
{
	ifstream in(filename);
	string s;
	while ( getline(in, s) )
	{
		vec.push_back(s);
	}
}

istream& streamFunc(istream& in)
{
	string s;
	while (in >> s)
	{
		cout << s << " ";
	}
	cout << endl;
	in.clear();
	return in;
}

int main(int argc, char** argv)
{
	string filename = "G:/testt.txt";
	vector<string> strVec;
	readFunc(filename, strVec);

	for (int i = 0; i < strVec.size(); i++)
	{
		istringstream iss(strVec.at(i));
		string str;
		while (iss >> str)
		{
			cout << str << endl;
		}
	}

	system("pause");
	return 0;
}

8.11

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

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

int main(int argc, char** argv)
{
	/*书中的源码*/
	/*string line, word;
	vector<PersonInfo> people;
	while ( getline(cin, line))
	{
		PersonInfo info;
		istringstream record(line);
		record >> info.name;
		while (record >> word)
		{
			info.phones.push_back(word);
		}
		people.push_back(info);
	}*/

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

	system("pause");
	return 0;
}

8.12

PersonInfo是一个聚合类:所有成员公有,未定义构造函数,无类内初始值,无基类无virtual函数 。

8.13

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

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

void readFunc(const string filename, vector<string>& vec)
{
	ifstream in(filename);
	string s;
	while (getline(in, s))
	{
		vec.push_back(s);
	}
}

int main(int argc, char** argv)
{
	string filename = "G:/PersonInfo.txt";
	vector<string> strVec;
	readFunc(filename, strVec);
	int i = 0;
	string line, word;
	vector<PersonInfo> people;
	istringstream record;
	while ( i < strVec.size() )
	{
		line = strVec.at(i++);
		PersonInfo info;
		record.clear();
		record.str(line);
		record >> info.name;
		while (record >> word)
		{
		info.phones.push_back(word);
		}
		people.push_back(info);
	}

	system("pause");
	return 0;
}

8.14

使用引用可以避免拷贝,提高效率,添加const标志是不希望对应对象的值被改变。

展开阅读全文

没有更多推荐了,返回首页