C++ Primer(第五版) 第八章练习答案
目录
- C++ Primer(第五版) 第八章练习答案
-
-
- 8.1or2
- 8.3
- 8.4
- 8.5
- 8.6
- 8.7
- 8.8
- 8.9
- 8.10
- 8.11
- 8.12
- 8.13
- 8.14
8.1or2
#include
#include
#include
using namespace std;
istream &print(istream &is, string &item);
int main()
{
string str;
print(cin, str);
print(cin, str);
return 0;
}
istream &print(istream &is, string &item)
{
while (is >> item)
;
is.clear(cin.rdstate() & ~is.failbit & ~is.eofbit);
return is;
}
8.3
8.4
#include
#include
#include
#include
using namespace std;
int main()
{
ifstream input("test.txt");
vector<string> vec;
string str;
while (getline(input, str))
vec.push_back(str);
for (auto &i : vec)
cout << i << endl;
return 0;
}
8.5
#include
#include
#include
#include
using namespace std;
int main()
{
ifstream input("test.txt");
vector<string> vec;
string str;
while (input >> str)
vec.push_back(str);
for (auto &i : vec)
cout << i << endl;
input.close();
return 0;
}
8.6
#include
#include
#include
#include "../Sales_data/Sales_data.h"
using namespace std;
int main(int argv, char **argc)
{
ifstream input(argc[1]);
Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
}
else
{
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
8.7
#include
#include
#include
#include "../Sales_data/Sales_data.h"
using namespace std;
int main(int argv, char **argc)
{
if (argv != 3)
exit(EXIT_FAILURE);
ifstream input(argc[1]);
ofstream output(argc[2]);
Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(output, total) << endl;
total = trans;
}
}
print(output, total) << endl;
}
else
{
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
8.8
#include
#include
#include
#include "../Sales_data/Sales_data.h"
using namespace std;
int main(int argv, char **argc)
{
if (argv != 3)
exit(EXIT_FAILURE);
ifstream input(argc[1]);
ofstream output(argc[2], ofstream::app | ofstream::out);
Sales_data total;
if (read(input, total))
{
Sales_data trans;
while (read(input, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(output, total) << endl;
total = trans;
}
}
print(output, total) << endl;
}
else
{
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
8.9
#include
#include
using namespace std;
istream &print(istream &is, string &item)
{
while (is >> item)
;
is.clear(cin.rdstate() & ~is.failbit & ~is.eofbit);
return is;
}
int main()
{
istringstream input("hhhh hhhhh wwww wwww ffff");
string word;
print(input, word);
cout << word << endl;
return 0;
}
8.10
#include
#include
#include
#include
#include
using namespace std;
int main()
{
ifstream infile("test.txt");
vector<string> vec;
string str;
while (getline(infile, str))
vec.push_back(str);
for (decltype(vec.size()) i = 0; i < vec.size(); ++i)
{
istringstream instr(vec[i]);
while (instr >> str)
cout << str << ends;
cout << endl;
}
return 0;
}
8.11
#include
#include
#include
#include
#include
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
int main()
{
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;
record.clear(record.rdstate() & ~record.eofbit & ~record.failbit);
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (auto &i : people)
{
cout << i.name << ends;
for (auto &j : i.phones)
cout << j << ends;
cout << endl;
}
return 0;
}
8.12
8.13
#include
#include
#include
#include
#include
#include
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string &s);
void enphone(ifstream &infile, vector<PersonInfo> &people);
string format(const string &s, int i);
int main(int argc, char ** argv)
{
if (argc != 2)
exit(EXIT_FAILURE);
ifstream infile(argv[1]);
vector<PersonInfo> people;
enphone(infile, people);
infile.close();
for (const auto &entry : people)
{
ostringstream formatted, badNums;
int b = 0;
for (const auto &nums : entry.phones)
{
if (!(valid(nums)))
{
badNums << " " << nums;
}
else
{
formatted << " " << format(nums, ++b);
}
}
if (badNums.str().empty())
cout << entry.name << " "
<< formatted.str() << endl;
else
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
return 0;
}
bool valid(const string &s)
{
for (const auto &i : s)
if (!isdigit(i))
return false;
return true;
}
string format(const string &s, int i)
{
return string("电话号码") + to_string(i) + ": " + s;
}
void enphone(ifstream &infile, vector<PersonInfo> &people)
{
string str;
while (getline(infile, str))
{
PersonInfo info;
istringstream record(str);
record >> info.name;
while (record >> str)
info.phones.push_back(str);
people.push_back(info);
}
}
8.14