8.1
#include<iostream>
#include<stdexcept>
using namespace std;
istream & f(istream & in)
{
int v;
while (cin >> v, !in.eof())
{
if (in.bad())
throw runtime_error("IO流错误");
if (in.fail())
{
cerr << "数据错误,请重试:" << endl;
in.clear();
in.ignore(100, '\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
cout << "请输入一些整数,按Ctrl+Z结束" << endl;
f(cin);
return 0;
}
8.4
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
ifstream in("data");
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
string line;
vector<string> words;
while (getline(in, line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it != words.end())
{
cout << *it << endl;
++it;
}
return 0;
}
8.6
#include<iostream>
#include<fstream>
#include"Sales_data.h"
using namespace std;
int main(int argc, char *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) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cerr << "没有数据" << endl;
}
return 0;
}
8.7
#include<iostream>
#include<fstream>
#include"Sales_data.h"
using namespace std;
int main(int argc, char *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) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cerr << "没有数据" << endl;
}
return 0;
}
8.9
#include<iostream>
#include<sstream>
#include<string>
#include<stdexcept>
using namespace std;
istream& f(istream & in)
{
string v;
while (in >> v, !in.eof())
{
if (in.bad())
{
throw runtime_error("IO流错误");
}
if (in.fail())
{
cerr << "数据错误,请重试:" << endl;
in.clear();
in.ignore(100, '\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
ostringstream msg;
msg << "C++ Primer第五版" << endl;
istringstream in(msg.str());
f(in);
return 0;
}
8.10
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
ifstream in("data");
if (!in)
{
cerr << "无法打开输入文件" << endl;
return -1;
}
string line;
vector<string> words;
while (getline(in, line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it != words.end())
{
istringstream line_str(*it);
string word;
while (line_str>>word)
cout << word << " ";
cout << endl;
++it;
}
return 0;
}
8.11
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
int main()
{
string line, word;
vector<PersonInfo>people;
stringstream 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);
}
return 0;
}
8.12
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
struct PersonInfo
{
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<PersonInfo>people;
stringstream record;
if (argc != 2)
{
cerr << "请给出文件名" << endl;
return -1;
}
ifstream in(argv[1]);
if (!in)
{
if (!in){
cerr << "无法打开输入文件" << endl;
}
}
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);
}
ostringstream os;
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())
os << entry.name << " "
<< formatted.str() <<endl;
else
cerr << "input error:"<<entry.name
<< "invalid number(s)" << badNums.str() << endl;
}
cout << os.str() << endl;
return 0;
}