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标签必须是整形常量表达式

时间: 2024-10-11 10:35:15

c++ primer 5.3.2节练习答案的相关文章

c++ primer 6.5.1节练习答案

练习6.40 a)正确 b)错误,一旦某个形参被赋予了默认值,他后面的所有形参都必须有默认值. 练习6.41 a)错误,ht没有默认实参,而a的实参列表里也没有给出实参: b)合法,调用init(24 ,10 ,' '): c)虽然合法,但是与程序猿的设计初衷不符,*会转换成十进制的数43,相当于调用init(14, 43, ' '): 练习6.42 1 string make_plural(size_t ctr, const string &word, const string &end

c++ primer 5.1.1节练习答案

练习5.20 1 int main() 2 { 3 vector<string> str; 4 string str1; 5 while (cin >> str1) 6 { 7 auto last = str.end(); 8 if (str.size() != 0 && str1 == (*(last-1))) 9 { 10 cout << str1 << endl; 11 break; 12 } 13 else 14 str.push_b

c++ primer 5.5.3节练习答案

练习5.22 1 int main() 2 { 3 int sz = get_size(); 4 while (sz <= 0) 5 { 6 sz = get_size(); 7 continue; 8 } 9 system("pause"); 10 return 0; 11 }

c++ primer 5.5.2节练习答案

练习5.21 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 int main() 7 { 8 vector<string> str; 9 string str1; 10 while (cin >> str1) 11 { 12 auto last = str.end()

c++ primer 6.5.2节练习答案

练习6.43 a)放在头文件,因为内联函数和constexpr函数一般写在头文件中 b)函数的定义一般写在源文件,而声明放在头文件 练习6.44 1 inline bool isShorter(const string &s1, const string &s2) 2 { 3 return s1.size() < s2.size(); 4 } 练习6.45 内联函数一般用于优化规模小.流程直接.频繁调用的函数. 练习6.46 不行,isShorter函数如果定义成constexpr函

c++ primer 5.4.4节练习答案

练习5.18 a)循环体少了花括号: b)不允许在条件部分定义变量: c)condition使用的变量必须定义在循环体之外: 练习5.19 1 int main() 2 { 3 string str1, str2; 4 int i; 5 do 6 { 7 cout << "please enter two string:"; 8 if (cin >> str1 >> str2) 9 { 10 if (str1.size() < str2.siz

c++ primer 6.2.4节练习答案

练习6.21 1 int max(const int x, const int *y) 2 { 3 if (x > *y) 4 return x; 5 else 6 return *y; 7 } 8 9 int main() 10 { 11 int a1, a2; 12 while (cin >> a1 >> a2) 13 { 14 cout << "the max is " << max(a1, &a2) <<

c++ primer 6.2.3节练习答案

练习6.16 1 bool is_empty(const string &s) { return s.empty(); } 练习6.17 1 bool have_upper(const string &s) 2 { 3 for (auto c : s) 4 { 5 if (isupper(c)) 6 return true; 7 } 8 return false; 9 } 10 11 void to_lower(string &s) 12 { 13 for (auto &c

c++ primer 5.4.1节练习答案

练习5.14 1 int main() 2 { 3 vector<string> str; 4 string str1,str2; 5 int max = 0; 6 while (cin >> str1) 7 { 8 int cnt = 0; 9 str.push_back(str1); 10 auto it = str.begin(); 11 while (it != str.end()) 12 { 13 if (*it == str1) 14 ++cnt; 15 ++it; 1

c++ primer 4.11.1节练习答案

练习4.34 a) float->bool b) int->float->double c) char->int->double 练习4.35 a) char->int->char b) int->unsigned int->float c) float->unsigned int->double d) int->float->double->char