C++ Primer第八章答案

//8.1
#include<iostream>
#include<stdexcept>
using namespace std;

istream &f(istream & in)
{
	int file;
	while (in>>file, !in.eof())//直到文件结束符才停止读取
	{
		if (in.bad())
			throw runtime_error("IO 流错误");
		if (in.fail())
		{
			cerr << "数据错误请重试" << endl;
			in.clear();
			in.ignore(100, '\n');
			continue;
		}
		cout << file << endl;
	}
	in.clear();
	return in;
}
int main()
{
	cout << "请输入一些整数,并以crtl z结束" << endl;
	f(cin);
	return 0;
}
//8.4
//内容:以读的方式打开文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中
//作者:xxx
//时间:2019年3月18日
#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

int main()
{
	ifstream in("E:/study/软件开发/vc学习/C++Primer(第五版)/code/IO_file/data.txt");//打开文件,注意是斜杠,撇丿
	if (!in)
	{
		cerr << "无法打开输入文件" << endl;
	}
	string line;
	vector<string> words;
	while (getline(in, line))//将读入的文件一行一行的存入string中,知道文件末尾
	{
		words.push_back(line);
	}

	in.close();//关闭文件

	vector<string>::const_iterator it = words.begin();//const_iterator可以修改其自身的值,但不可以修改其指向的元素的值
	while (it != words.end())
	{
		cout << *it << endl;
		++it;
	}
	return 0;

}
//8.6
#pragma once
#include<string>
#include<iostream>
using namespace std;

class Sales_data {
	friend istream &read(istream &is, Sales_data &item);
	friend ostream &print(ostream &os, Sales_data &item);
public:
	//受委托构造函数,4个参数
	Sales_data(const string &book, int num, double sell, double sale) :
		bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
		if (sellprice)
			discount = saleprice / sellprice;
		cout << "该函数输如四个参数" << endl;
	}
	Sales_data() :Sales_data("", 0, 0, 0)
	{
		cout << " 该函数不输入任何参数,执行默认实参" << endl;
	}
	Sales_data(const string &book) :Sales_data(book, 0, 0, 0)
	{
		cout << "该函数输入一个参数" << endl;
	}
	Sales_data(istream &is) :Sales_data()
	{
		read(is, *this);
		cout << "该函数输入book对象" << endl;
	}
private:
	string bookNo;//书型号
	int units_sold = 0;//销售数量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
public:
	Sales_data &combine(const Sales_data &rhs);
	string isbn() const { return bookNo; }
};
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
	if (units_sold)
	{
		saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (units_sold + rhs.units_sold);
	}
	else
		saleprice = 0;
	if (sellprice)
		discount = saleprice / sellprice;
	return *this;
}
istream &read(istream &is, Sales_data &item)
{
	is >> item.bookNo >> item.units_sold >> item.sellprice >> item.saleprice;
	return is;
}
ostream &print(ostream &os, Sales_data &item)
{
	os << item.bookNo << " " << item.units_sold << " " << item.sellprice << " " << item.saleprice << item.discount << endl;
	return os;
}
Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

//内容:从文件中读取交易记录,将文件名作为第一个参数传递给main
//作者:xxx
//时间:2019年4月11日
#include<iostream>
#include<fstream>
#include"Sales_data.h"

using namespace std;

int main(int argc,char *argv[])//argc为argv中元素的个数,argv是指向数组元素的指针
{
	if (argc != 2)
	{
		cerr << "请给出文件名" << endl;
		return -1;
	}
	ifstream in(argv[1]);
	if (!in)
	{
		cerr<< "无法打开输入文件" << endl;
		return -1;
	}
	Sales_data total;
	if(read(in, total))
	{
		Sales_data trans;
		while (read(in, trans))
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				print(cout, total);
				cout << endl;
			}
		}
		print(cout, total);
		cout << endl;
	}
	
	else
	{
		cerr << "没有任何数据" << endl;
	}
	return 0;
}
//8.7
//内容:从文件中读取交易记录,将文件名作为第一个参数传递给main
//作者:xxx
//时间:2019年4月11日
#include<iostream>
#include<fstream>
#include"Sales_data.h"

using namespace std;

int main(int argc,char *argv[])//argc为argv中元素的个数,argv是指向数组元素的指针
{
	if (argc != 3)
	{
		cerr << "请给出文件名" << endl;
		return -1;
	}
	ifstream in(argv[1]);
	if (!in)
	{
		cerr<< "无法打开输入文件" << endl;
		return -1;
	}
	ofstream out(argv[2]);
	if (!out)
	{
		cerr << "无法打开输出文件" << endl;
		return -1;
	}
	Sales_data total;
	if(read(in, total))
	{
		Sales_data trans;
		while (read(in, trans))
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				print(cout, total);
				cout << endl;
			}
		}
		print(cout, total);
		cout << endl;
	}
	
	else
	{
		cerr << "没有任何数据" << endl;
	}
	return 0;
}
//8.8
将Ofstream out(argv[2]);改为ofstream out(argv[2],ofstream::qpp);
//8.9
//内容:打印istringstream对象的内容
//作者:xxx
//时间:2019年4月11日
#include<iostream>
#include<sstream>
#include<string>
#include<stdexcept>

using namespace std;

istream &read(istream &in)
{
	string str;
	while (in >> str, !in.eof())
	{
		if (in.bad())
			throw runtime_error("IO error");
		if (in.fail())
		{
			cerr << "数据错误,请重试" << endl;
			in.clear();
			in.ignore(100, '\n');
			continue;
		}
		cout << str << endl;
	}
	in.clear();
	return in;
}

int main()
{
	ostringstream outstr;
	outstr << "be always working" << endl;
	istringstream in(outstr.str());
	read(in);
	return 0;
}
//8.10
//内容:将来自一个文件中的行保存在一个vector<stirng>中,然后用一个
//     istringsteam从vector读取数据元素
//作者:xxx
//时间:2019年4月11日
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;


int main()
{
	ifstream in("E:/study/软件开发/vc学习/C++Primer(第五版)/code/IO_file/data.txt");
	if (!in)
	{
		cerr << "无法打开输入文件" << endl;
		return -1;
	}
	vector<string> words;
	string line;
	while (getline(in, line))
	{
		words.push_back(line);
	}

	in.close();//关闭文件
	vector<string>::const_iterator it = words.begin();
	while (it != words.end())
	{
		istringstream str(*it);
		string word;
		while (str >> word)
			cout << word << " ";
		cout << endl;
		++it;
	}
	return 0;
}
//8.11
//内容:记录人名和电话号码
//作者:xxx
//时间:2019年4月11日
#include<iostream>
#include<sstream>
#include<string>
#include<vector>

using namespace std;

class PersonInformation {
public:
	string name;
	vector<string> phones;
};
int main()
{
	string line, word;
	vector<PersonInformation> people;
	istringstream record;
	while (getline(cin, line))
	{
		PersonInformation Pin;
		record.clear();
		record.str(line);
		record >> Pin.name;//把record的第一个字符串给name,其余的给word
		while (record >> word)
			Pin.phones.push_back(word);
		people.push_back(Pin);
	}
	return 0;
}
//8.13
//内容:从命名文件中读取记录人名和电话号码
//作者:xxx
//时间:2019年4月11日
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>

using namespace std;

class PersonInformation {
public:
	string name;
	vector<string> phones;
};
string format(const string &s)
{
	return s;
}
bool valid(const string &s)
{
	return true;
}
int main(int argc,char *argv[])
{
	string line, word;
	vector<PersonInformation> people;
	istringstream record;
	if (argc != 2)
	{
		cerr << "请给出文件名" << endl;
		return -1;
	}
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "输入文件无法打开" << endl;
		return -1;
	}
	while (getline(cin, line))
	{
		PersonInformation Pin;
		record.clear();
		record.str(line);
		record >> Pin.name;//把record的第一个字符串给name,其余的给word
		while (record >> word)
			Pin.phones.push_back(word);
		people.push_back(Pin);
	}
	ostringstream out;
	for (const auto &entry : people)
	{
		ostringstream fomatted, badNums;
		for (const auto &nums : entry.phones)
		{
			if (!valid(nums))
			{
				badNums << " " << nums;
			}
			else
				fomatted << " " << format(nums);
		}
		if (badNums.str().empty())
		{
			out << entry.name << " " << fomatted.str() << endl;
		}
		else
		{
			cerr << "input error:" << entry.name << "ivalid numbers(s)" << badNums.str() << endl;
		}
		
	}
	cout << out.str() << endl;
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值