c++ primer中文版第五版作业第八章

仓库地址

文章目录

8.1

8-1and8-2.cpp

复制代码
#include <iostream>
using namespace std;
istream &test(istream &is)
{
	string st;
A:	while(is.good())
	{
		is>>st;
		cout<<st<<'\n';
	}
	if(is.bad())
		cout<<"fatal error!"<<endl;
	else if(is.eof())
		cout<<"End of file."<<endl;
	else if(is.fail())
		goto A;
	is.clear();
	return is;
}
int main()
{
	test(cin);
	return 0;
}

8.2

见8.1

8.3

情况很多,比如cin流崩溃,IO操作失败或者遇到文件结束符等。

8.4

复制代码
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
void rf(const string &str)
{
	vector<string> vs;
	string stemp;
	ifstream ifst(str);
	if(ifst)
	{
		while(getline(ifst,stemp))
			vs.push_back(stemp);
		for(auto p:vs)
			cout<<p<<"#";
		cout<<endl;
	}
	else
		cout<<"open file failed."<<'\n';
}
int main(void)
{
	rf("text");
	return 0;
}

8.5

复制代码
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
void rf(const string &str)
{
	vector<string> vs;
	string stemp;
	ifstream ifst(str);
	if(ifst)
	{
		while(ifst>>stemp)
			vs.push_back(stemp);
		for(auto p:vs)
			cout<<p<<"#";
		cout<<endl;
	}
	else
		cout<<"open file failed."<<'\n';
}
int main(void)
{
	rf("text");
	return 0;
}

8.6

复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Sales_data
{
	string bookNo;
	unsigned units_sold{0};
	double revenue{0.0};
};
int main(int argc,char *argv[])
{
	struct Sales_data  total,trans;
	double price;
	ifstream ifst(argv[1]);

	if(ifst>>total.bookNo)
	{
		ifst>>total.units_sold>>price;
		total.revenue=total.units_sold*price;
		while(ifst>>trans.bookNo)
		{
			ifst>>trans.units_sold>>price;
			trans.revenue=trans.units_sold*price;
			if(total.bookNo==trans.bookNo)
			{
				total.units_sold+=trans.units_sold;
				total.revenue+=trans.revenue;
			}
			else
			{
				cout<<total.bookNo<<" "<<total.units_sold<<" "<<total.revenue<<" "<<total.revenue/total.units_sold<<endl;
				total.bookNo=trans.bookNo;
				total.units_sold=trans.units_sold;
				total.revenue=trans.revenue;
			}
		}
		cout<<total.bookNo<<" "<<total.units_sold<<" "<<total.revenue<<" "<<total.revenue/total.units_sold<<endl;
	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}
	return 0;
}

8.7

复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Sales_data
{
	string bookNo;
	unsigned units_sold{0};
	double revenue{0.0};
};
int main(int argc,char *argv[])
{
	struct Sales_data  total,trans;
	double price;
	ifstream ifst(argv[1]);
	ofstream ofst(argv[2]);

	if(ifst>>total.bookNo)
	{
		ifst>>total.units_sold>>price;
		total.revenue=total.units_sold*price;
		while(ifst>>trans.bookNo)
		{
			ifst>>trans.units_sold>>price;
			trans.revenue=trans.units_sold*price;
			if(total.bookNo==trans.bookNo)
			{
				total.units_sold+=trans.units_sold;
				total.revenue+=trans.revenue;
			}
			else
			{
				ofst<<total.bookNo<<" "<<total.units_sold<<" "<<total.revenue<<" "<<total.revenue/total.units_sold<<endl;
				total.bookNo=trans.bookNo;
				total.units_sold=trans.units_sold;
				total.revenue=trans.revenue;
			}
		}
		ofst<<total.bookNo<<" "<<total.units_sold<<" "<<total.revenue<<" "<<total.revenue/total.units_sold<<endl;
	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}
	return 0;
}

8.8

复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Sales_data
{
	string bookNo;
	unsigned units_sold{0};
	double revenue{0.0};
};
int main(int argc,char *argv[])
{
	struct Sales_data  total,trans;
	double price;
	ifstream ifst(argv[1]);
	ofstream ofst(argv[2],ofstream::app|ofstream::out);

	if(ifst>>total.bookNo)
	{
		ifst>>total.units_sold>>price;
		total.revenue=total.units_sold*price;
		while(ifst>>trans.bookNo)
		{
			ifst>>trans.units_sold>>price;
			trans.revenue=trans.units_sold*price;
			if(total.bookNo==trans.bookNo)
			{
				total.units_sold+=trans.units_sold;
				total.revenue+=trans.revenue;
			}
			else
			{
				ofst<<total.bookNo<<" "<<total.units_sold<<" "<<total.revenue<<" "<<total.revenue/total.units_sold<<endl;
				total.bookNo=trans.bookNo;
				total.units_sold=trans.units_sold;
				total.revenue=trans.revenue;
			}
		}
		ofst<<total.bookNo<<" "<<total.units_sold<<" "<<total.revenue<<" "<<total.revenue/total.units_sold<<endl;
	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}
	return 0;
}

8.9

复制代码
#include <sstream>
#include <iostream>
using namespace std;
istream &test(istream &is)
{
	string st;
A:	while(is.good())
	{
		is>>st;
		cout<<st<<'\n';
	}
	if(is.bad())
		cout<<"fatal error!"<<endl;
	else if(is.eof())
		cout<<"End of file."<<endl;
	else if(is.fail())
		goto A;
	is.clear();
	return is;
}
int main(void)
{
	istringstream istm("wo shi shei");
	test(istm);
	return 0;
}

8.10

复制代码
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc,char *argv[])
{
	string tmp;
	vector<string> svec;
	ifstream ifst(argv[1]);
	while(getline(ifst,tmp))
		svec.push_back(tmp);
	for(auto element:svec)
	{
		istringstream isstm(element);
		while(isstm>>tmp)
			cout<<tmp<<endl;
	}
	return 0;
}

8.11

复制代码
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
struct PersonInfo
{
	string name;
	vector<string> phones;
};
int main(void)
{
	string line,word;
	vector<PersonInfo> people;
	istringstream record;
	while(getline(cin,line))
	{
		PersonInfo info;
		record.clear(record.rdstate()&~record.failbit&~record.eofbit);
		record.str(line);
		record>>info.name;
		while(record>>word)
			info.phones.push_back(word);
		people.push_back(info);
	}
	for(auto pi:people)
	{
		cout<<pi.name<<endl;
		for(auto ph:pi.phones)
			cout<<ph<<" ";
		cout<<endl;
	}
	return 0;
}

8.12

因为每个人的电话条目数量不同,所以应该缺省初始化后在程序中设置人名,并逐个添加电话。

8.13

复制代码
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
bool valid(const string &str);
string format(const string &str);
struct PersonInfo
{
	string name;
	vector<string> phones;
};
int main(int argc,char *argv[])
{
	string line,word;
	vector<PersonInfo> people;
	istringstream record;
	ifstream ifst(argv[1]);
	ofstream ofst(argv[2]);
	while(getline(ifst,line))
	{
		PersonInfo info;
		record.clear(record.rdstate()&~record.failbit&~record.eofbit);
		record.str(line);
		record>>info.name;
		while(record>>word)
			info.phones.push_back(word);
		people.push_back(info);
	}
	for(const auto &pi:people)
	{
		ostringstream formatted,badnums;
		for(const auto &ph:pi.phones)
		{
			if(!valid(ph))
				badnums<<" "<<ph;
			else
				formatted<<" "<<format(ph);
		}
		if(badnums.str().empty())
			ofst<<pi.name<<" "<<formatted.str()<<endl;
		else
			cerr<<"input error: "<<pi.name<<" invalid numbers "<<badnums.str()<<endl;
	}
	return 0;
}
bool valid(const string &str)
{
	if(str.size()==11)
		return true;
	else
		return false;
}
string format(const string &str)
{
	return str;
}

8.14

const表明循环中不会改变这些项的值,auto利用编译器推断entrynums的类型,简化代码,&表示引用可以避免拷贝。

相关推荐
yuanpan6 分钟前
C#如何正确的停止一个多线程Task?CancellationTokenSource 的用法。
开发语言·c#
程高兴8 分钟前
单相交直交变频电路设计——matlab仿真+4500字word报告
开发语言·matlab
我真的不会C1 小时前
QT中的事件及其属性
开发语言·qt
Ethon_王1 小时前
走进Qt--工程文件解析与构建系统
c++·qt
2501_906314322 小时前
优化无头浏览器流量:使用Puppeteer进行高效数据抓取的成本降低策略
开发语言·数据结构·数据仓库
工藤新一¹2 小时前
C++/SDL进阶游戏开发 —— 双人塔防游戏(代号:村庄保卫战 13)
c++·游戏·游戏引擎·毕业设计·sdl·c++游戏开发·渲染库
让我们一起加油好吗2 小时前
【C++】类和对象(上)
开发语言·c++·visualstudio·面向对象
好想有猫猫2 小时前
【Redis】服务端高并发分布式结构演进之路
数据库·c++·redis·分布式·缓存
不是杠杠2 小时前
驼峰命名法(Camel Case)与匈牙利命名法(Hungarian Notation)详解
c++
Epiphany.5563 小时前
基于c++的LCA倍增法实现
c++·算法·深度优先