首页 > 代码库 > c++ primer 5.3.2节练习答案

c++ primer 5.3.2节练习答案

练习5.9

 1 int main()
 2 {
 3     vector<string> str;
 4     string str1;
 5     int cnt = 0;
 6     while (cin >> str1)
 7         str.push_back(str1);
 8     for (auto &c : str)
 9         for(auto d : c)
10             {
11                 if (d == a || d == e || d == i || d == o || d == u)
12                     cnt++;
13             }
14     cout << cnt << endl;
15     system("pause");
16     return 0;
17 }

练习5.10

 1 int main()
 2 {
 3     unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
 4     char ch;
 5     while (cin >> ch)
 6     {
 7         switch (ch)
 8         {
 9         case a: case A:
10                 ++aCnt;
11                 break;
12         case e: case E:
13                 ++eCnt;
14                 break;
15         case i: case I:
16                 ++iCnt;
17                 break;
18         case o: case O:
19                 ++oCnt;
20                 break;
21         case u: case U:
22                 ++uCnt;
23                 break;
24         }
25     }
26     cout << aCnt << " " << eCnt << " " << iCnt << " " << oCnt << " " << uCnt << endl;
27     system("pause");
28     return 0;
29 }

 

练习5.11

 1 int main()
 2 {
 3     unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, spaceNum = 0, tabNum = 0, n_num = 0;
 4     vector<string> str1;
 5     string str;
 6     while ((str = cin.get()) != "#")   //如果用getline函数会自动判定换行符为字符串结束条件,无法计算换行符的数量,而用cin则空格都无法计算
 7         str1.push_back(str);
 8     for (auto &c : str1)
 9         for (auto d : c)
10         {
11 
12             switch (d)
13             {
14                 case a: case A:
15                     ++aCnt;
16                     break;
17                 case e: case E:
18                     ++eCnt;
19                     break;
20                 case i: case I:
21                     ++iCnt;
22                     break;
23                 case o: case O:
24                     ++oCnt;
25                     break;
26                 case u: case U:
27                     ++uCnt;
28                     break;
29                 case  :
30                     ++spaceNum;
31                     break;
32                 case \t:
33                     ++tabNum;
34                     break;
35                 case \n:
36                     ++n_num;
37                     break;
38             }
39         }
40     cout << aCnt << " " << eCnt << " " << iCnt << " " << oCnt << " " << uCnt << " " << spaceNum << " " << tabNum << " " << n_num << endl;
41     system("pause");
42     return 0;
43 }

 

练习5.12

 1 int main()
 2 {
 3     unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,ff = 0, fl = 0, fi = 0;
 4     vector<char> str1;
 5     char str;
 6     while (cin >> str)   
 7         str1.push_back(str);
 8     for (auto i = 0; i != str1.size(); ++i)
 9         {    
10             switch (str1[i])
11             {
12                 case a: case A:
13                     ++aCnt;
14                     break;
15                 case e: case E:
16                     ++eCnt;
17                     break;
18                 case i: case I:
19                     ++iCnt;
20                     break;
21                 case o: case O:
22                     ++oCnt;
23                     break;
24                 case u: case U:
25                     ++uCnt;
26                     break;
27                 case f:
28                     if (str1[i + 1] == f)
29                         ++ff;
30                     break;
31             }
32         }
33     cout << aCnt << " " << eCnt << " " << iCnt << " " << oCnt << " " << uCnt << " " << ff << endl;
34     system("pause");
35     return 0;
36 }

最开始使用string类型的vector容器来储存字符串,利用双重for循环来取到每一个数的时候,再与f后面一个字符比较,这样做编译成功,但是遇到了执行上的错误,先贴上代码,等过段时间解决

 1 int main()
 2 {
 3     unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,ff = 0, fl = 0, fi = 0;
 4     vector<string> str1;
 5     string str;
 6     while (cin >> str)   
 7         str1.push_back(str);
 8     for (auto it = str1.begin();it !=str1.end(); it++)
 9         for (auto i = begin(*it); i != end(*it); i++)
10         {    
11 
12             switch ((*i))
13             {
14                 case a: case A:
15                     ++aCnt;
16                     break;
17                 case e: case E:
18                     ++eCnt;
19                     break;
20                 case i: case I:
21                     ++iCnt;
22                     break;
23                 case o: case O:
24                     ++oCnt;
25                     break;
26                 case u: case U:
27                     ++uCnt;
28                     break;
29                 case f:
30                     if (*(i + 1) == f)
31                         ++ff;
32                     break;
33             }
34         }
35     cout << aCnt << " " << eCnt << " " << iCnt << " " << oCnt << " " << uCnt << " " << ff << endl;
36     system("pause");
37     return 0;
38 }

 

练习5.13

a)每个case语句后少了break;

b)ix只是在1分支中定义初始化,在下面的分支中并没有定义;

c)应该写成

1 case 1 : case 3 : case 5 : case 7 : case 9 :

d)case标签必须是整形常量表达式

 

c++ primer 5.3.2节练习答案