CH8 课后习题

Posted Emma_U

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CH8 课后习题相关的知识,希望对你有一定的参考价值。

8.1和8.2

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 istream& f(istream& in)
 6 {
 7     int v;
 8     in >> v;
 9     if (in.bad())
10         throw runtime_error("IO is bad");
11     if (in.fail())
12     {
13         cerr << "bad data,try again" << endl;
14         in.clear();
15         in.ignore(100, \'\\n\');
16         //忽略的知识点continue只能用在循环中
17     }
18     cout << v << endl;
19     in.clear();
20     return in;
21 }
22 int main()
23 {
24     cout << "please enter numbers:(Ctrl+Z to end)" << endl;
25     f(cin);//cin遇到空格会结束,如果要读取整行,可以使用getline
26     system("pause");
27     return 0;
28 }

8.3

遇到了文件结束符标志,或者是输入错误的流或无效数据

8.4

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <fstream>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     string fileName;
11     cout << "please enter the name of file:" << endl;
12     cin >> fileName;
13     ifstream infile(fileName);
14     if (!infile)
15     {
16         cerr << "can not open the file you entered,please check!" << endl;
17         return -1;
18     }
19     string line;
20     vector<string> row;
21     while (getline(infile, line))
22     {
23         row.push_back(line);
24     }
25     for (auto it : row)
26         cout << it << endl;
27 
28     system("pause");
29     return 0;
30 }

8.5

将8.4的第21行改为while(infile>>line)即可

8.9

 1 #include <iostream>
 2 //#include <vector>
 3 #include <sstream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 istream& f(istream& in)
 9 {
10     string str;
11 
12     //in >> str;//遇到空格就结束,使用循环
13     while (in >> str, !in.eof())
14     {
15         if (in.bad())
16             throw runtime_error("IO IS bad!");
17         if (in.fail())
18         {
19             cerr << "bad data or not matched" << endl;
20             in.clear();
21             in.ignore(100, \'\\n\');
22             continue;
23         }
24         cout << str << endl;
25     }
26 
27 
28 }
29 
30 int main()
31 {
32     ostringstream os;
33     os << "Hello C++" << endl;;
34     istringstream in(os.str());
35     f(in);
36     system("pause");
37     return 0;
38 }

 8.10

 1 #include <iostream>
 2 #include <sstream>
 3 #include <vector>
 4 #include <string>
 5 #include <fstream>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     string fileName;
12     cout << "please enter the name of the file:" << endl;
13     cin >> fileName;
14     ifstream infile(fileName);
15     if (!infile)
16     {
17         cerr << "can not open the file" << endl;
18         return -1;
19     }
20 
21     string line;
22     vector<string>txt_row;
23     while (getline(infile, line))
24     {
25         txt_row.push_back(line);
26         //istringstream record(line);//题目要求从vector中读取数据,不是从文件,所以这样不符合要求
27     }
28     infile.close();
29     for (auto it : txt_row)
30     {
31         istringstream line_str(it);
32         string word;
33         while (line_str >> word)
34             cout << word << " ";
35         cout << endl;
36 
37     }
38     system("pause");
39     return 0;
40 }

8.11

istringstream 的对象record若定义在while循环外,则会被循环调用,重复使用流,需要恢复流状态,使用clear恢复

 1 #include <iostream>
 2 #include <vector>
 3 #include <sstream>
 4 
 5 using namespace std;
 6 struct PersonInfo {
 7     string name;
 8     vector<string>phone;
 9 
10 };
11 
12 int main()
13 {
14     
15     string line;
16     string word;
17     vector<PersonInfo>person;
18     istringstream record;
19     while (getline(cin, line))
20     {
21         PersonInfo info;
22         record.clear();
23         record >> info.name;
24         while (record >> word)
25         {
26             info.phone.push_back(word);
27 
28         }
29         person.push_back(info);
30     }
31     system("pause");
32     return 0;
33 }

8.12

因为每个人的电话号码数量不固定。

8.13

此题,我写的存在问题,测试时和我的文件分别是这样的

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 struct PersonInfo {
 9     string name;
10     vector<string>phone;
11 };
12 
13 bool valid(const string& str)
14 {
15     cout << "check the string valid or not" << endl;
16     return true;
17 }
18 
19 //string format(string& str)
20 string format(const string& str)
21 {
22     cout << "format the phone numbers " << endl;
23     return str;
24 }
25 int main(int argc,char*argv[])
26 {
27     string line;
28     string word;
29     vector<PersonInfo> person;
30     istringstream record;
31     if (argc != 2)
32     {
33         cerr << "please enter the name of file." << endl;
34         return -1;
35     }
36     ifstream infile(argv[1]);
37     if (!infile)
38     {
39         cerr << "can not open the file" << endl;
40         return -1;
41     }
42     while (getline(infile, line))
43     {
44         PersonInfo info;
45         infile.clear();
46         record.str(line);
47         record >> info.name;
48         
49         while (record >> word)
50         {
51             info.phone.push_back(word);
52         }
53         person.push_back(info);
54     }
55 
56     //实现将从文件读取的记录输出
57     ostringstream  os;
58     for (const auto& entry : person)
59     {
60         ostringstream badNums;
61         ostringstream formatted;
62         for (auto &nums : entry.phone)
63         {
64             if (!valid(nums))
65             {
66                 //badNums << nums << "  is invalid" << endl;
67                 badNums << " " << nums;
68 
69             }
70             else
71                 //formatted << "after foramtted:  " << format(nums) << endl;
72                 formatted << " " << format(nums);
73         }
74 
75         if (badNums.str().empty())
76             os << entry.name << " " << formatted.str() << endl;
77         else
78             cerr << "input error,try again" << endl;
79         
80     }
81     cout << os.str() << endl;//
82     //system("pasue");
83     return 0;
84 }

另外,我认为format函数的参数应该是非const 型的,但是若是非const型,则62行就不能是引用型,否则编译出错,我暂时还没想明白原因

8.14

如上,使用引用是因为person和phone的元素分别是struct和string对象,使用引用可避免拷贝,但是教材中定义为const,我就无法理解了,通常定义为const是避免改变这些项的值,name成员不能改变,可是phone成员需要格式化处理,需要改变

 

以上是关于CH8 课后习题的主要内容,如果未能解决你的问题,请参考以下文章

算法1.1课后问答及习题

课后习题2

第四章课后习题

课后习题第一题--0703

课后习题篇

《java JDK7 学习笔记》课后练习题3