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的类型,简化代码,&表示引用可以避免拷贝。

相关推荐
anlog3 分钟前
C#在自定义事件里传递数据
开发语言·c#·自定义事件
奶香臭豆腐16 分钟前
C++ —— 模板类具体化
开发语言·c++·学习
不想当程序猿_22 分钟前
【蓝桥杯每日一题】分糖果——DFS
c++·算法·蓝桥杯·深度优先
晚夜微雨问海棠呀24 分钟前
长沙景区数据分析项目实现
开发语言·python·信息可视化
graceyun25 分钟前
C语言初阶习题【9】数9的个数
c语言·开发语言
cdut_suye34 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
波音彬要多做1 小时前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
捕鲸叉1 小时前
C++软件设计模式之外观(Facade)模式
c++·设计模式·外观模式
Swift社区1 小时前
Excel 列名称转换问题 Swift 解答
开发语言·excel·swift
一道微光1 小时前
Mac的M2芯片运行lightgbm报错,其他python包可用,x86_x64架构运行
开发语言·python·macos