C++Primer第五版习题答案详解(五)
发表于|更新于
|字数总计:2.7k|阅读时长:12分钟|阅读量:
答案目录
第5章 语句
练习5.1
空语句:最简单的语句,当程序中在语法上需要一条语句,但是逻辑上却不需要时,就可以用到空语句
练习5.2
用花括号括起来的语句和声明的序列成为块,一个块就是一个作用域。如循环体内有多条执行语句,就需要用到块,函数也需要用到块。
练习5.3
可读性并没有提高,用块更易读。
练习5.4
其他代码也需要访问控制变量时,变量需要定义在语句的外部
(a):iter变量未初始化,且需要定义在语句的外部
(b):if部分无意义,在while循环中已经完成了判断
练习5.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| int main() { const vector<string> scores = {"F", "D", "C", "B", "A", "A++"}; float grade; string letterGrade; while (cin >> grade) { if (grade < 60) { letterGrade = scores[0]; } else { letterGrade = scores[(grade-50)/10]; } cout << letterGrade << endl; } }
|
练习5.6
1 2 3 4 5 6 7 8 9 10 11
| int main() { const vector<string> scores = {"F", "D", "C", "B", "A", "A++"}; float grade; string letterGrade; while (cin >> grade) { (grade < 60) ? letterGrade = scores[0] : letterGrade = scores[(grade-50)/10]; cout << letterGrade << endl; } }
|
练习5.7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| -a if (ival1 != ival2)
ival1 = ival2
else ival1 = ival2 = 0;
-b if (ival < minval)
minval = ival;
occurs = 1;
-c if (int ival = get_value())
cout << "ival = " << ival << endl;
if (!ival)
cout << "ival = 0\n"; // 第二个if中的ival未定义,ival的作用域仅限于第一个if循环中
-d if (ival = 0)
ival = get_value(); // if中的判断应写为 if (0 == ival)
|
练习5.8
悬垂else:C++规定,else与其最近的尚未匹配的if相匹配
练习5.9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| void main() { char cval; int sum_a = 0,sum_e = 0,sum_i = 0,sum_o = 0,sum_u = 0; while (cin >> cval) { if (cval == 'a') { sum_a++; } else if (cval == 'e') { sum_e++; else if (cval == 'i') { sum_i++; } else if (cval == 'o') { sum_o++; } else if (cval == 'u') { sum_u++; } } cout<<"元音字母a的个数为:"<<sum_a<<endl; cout<<"元音字母e的个数为:"<<sum_e<<endl; cout<<"元音字母i的个数为:"<<sum_i<<endl; cout<<"元音字母o的个数为:"<<sum_o<<endl; cout<<"元音字母u的个数为:"<<sum_u<<endl; }
|
练习5.10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| void main() { char cval; int char_a = 0,char_e = 0,char_i = 0,char_o = 0,char_u = 0; while (cin >> cval) { switch (cval) { case 'a': case 'A': ++char_a; break; case 'e': case 'E': ++char_e; break; case 'i': case 'I': ++char_i; break; case 'o': case 'O': ++char_o; break; case 'u': case 'U': ++char_u; break; } } cout<<"元音字母a的个数为:"<<char_a<<endl; cout<<"元音字母e的个数为:"<<char_e<<endl; cout<<"元音字母i的个数为:"<<char_i<<endl; cout<<"元音字母o的个数为:"<<char_o<<endl; cout<<"元音字母u的个数为:"<<char_u<<endl; }
|
练习5.11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| void main() { char cval; int char_a = 0,char_e = 0,char_i = 0,char_o = 0,char_u = 0,sum_space = 0,sum_table = 0,sum_newline = 0; while (cin >> std::noskipws >> cval) { switch (cval) { case 'a': case 'A': ++char_a; break; case 'e': case 'E': ++char_e; break; case 'i': case 'I': ++char_i; break; case 'o': case 'O': ++char_o; break; case 'u': case 'U': ++char_u; break; case ' ': ++sum_space; break; case '\t': ++sum_table; break; case '\n': ++sum_newline; break; } } cout<<"元音字母a的个数为:"<<char_a<<endl; cout<<"元音字母e的个数为:"<<char_e<<endl; cout<<"元音字母i的个数为:"<<char_i<<endl; cout<<"元音字母o的个数为:"<<char_o<<endl; cout<<"元音字母u的个数为:"<<char_u<<endl; cout<<"空格的个数为:"<<sum_space<<endl; cout<<"制表符的个数为:"<<sum_table<<endl; cout<<"换行符的个数为:"<<sum_newline<<endl; }
|
练习5.12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| void main() { char cval, char_before = '\0'; int char_a = 0, char_e = 0, char_i = 0, char_o = 0, char_u = 0, sum_space = 0, sum_table = 0, sum_newline = 0, sum_ff = 0, sum_fl = 0, sum_fi = 0; while (cin >> std::noskipws >> cval) { switch (cval) { case 'a': case 'A': ++char_a; break; case 'e': case 'E': ++char_e; break; case 'i': if (char_before == 'f') { ++sum_fi; } case 'I': ++char_i; break; case 'o': case 'O': ++char_o; break; case 'u': case 'U': ++char_u; break; case ' ': ++sum_space; break; case '\t': ++sum_table; break; case '\n': ++sum_newline; break; case 'f': if (char_before == 'f') { ++sum_ff; } break; case 'l': if (char_before == 'f') { ++sum_fl; } break; } char_before = cval; } cout<<"元音字母a的个数为:"<<char_a<<endl; cout<<"元音字母e的个数为:"<<char_e<<endl; cout<<"元音字母i的个数为:"<<char_i<<endl; cout<<"元音字母o的个数为:"<<char_o<<endl; cout<<"元音字母u的个数为:"<<char_u<<endl; cout<<"空格的个数为:"<<sum_space<<endl; cout<<"制表符的个数为:"<<sum_table<<endl; cout<<"换行符的个数为:"<<sum_newline<<endl; cout<<"字符序列ff的个数为:"<<sum_ff<<endl; cout<<"字符序列fl的个数为:"<<sum_fl<<endl; cout<<"字符序列fi的个数为:"<<sum_fi<<endl; }
|
练习5.13
(a):缺少break;
(b):ix应在外部声明定义
©:不能用逗号加以表示包括case 1 : case 2: case : 3…
(d):case 后面需要加的是常量表达式,可以加const修饰符,将ival、jval、kval变成”常量“
练习5.14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream> #include <string> #include <vector> using namespace std; void main() { string My_string, before_string, max_repeatstring; vector<string> vec1; vector<int> vec2; int repeat_number = 0, flag = 0,m = 0; while (cin >> My_string) { if (My_string == before_string) { ++repeat_number; m++; } else { vec2.push_back(repeat_number); repeat_number = 1; before_string = My_string; vec1.push_back(My_string); m = 0; } } vec2.push_back(m+1); int a = 0; vector<int>::iterator it1 = vec2.begin(); for (it1; it1 != vec2.end(); ++it1) { if (*it1 >a) { a = *it1; } } for (int i = 0; i< vec2.size(); i++) { if (vec2[i] == a) { cout<<"单词"<<vec1[i-1]<<"出现的次数为:"<<vec2[i]<<"次"<<endl; } } }
|
练习5.15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| -- a
for(int ix = 0; ix != sz; ++ ix) {...}
if (ix != sz)
-- b
int ix;
for (ix != sz; ++ix) {}
--c
for (int ix = 0; ix != sz; ++ix; ++ sz) {}
|
练习5.17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> #include <string> #include <vector> using namespace std; int main() { int a[5] = {0,1,2,3,4}; int b[6] = {0,1,2,3,4,5}; vector<int> My_vec1(a, a+5); vector<int> My_vec2(b, b+6); int small_size = My_vec1.size()>My_vec2.size() ? My_vec2.size() : My_vec1.size(); for (int i = 0;i<=small_size;i++) { if (My_vec1[i] != My_vec2[i]) { cout<<"False"<<endl; return 0; } } cout<<"True"<<endl; return 0; }
|
练习5.18
(a):do后面那么多条语句,需要使用花括号
(b):因为do循环体中肯定还需要使用到ival这个变量,所以应该将其定义在循环体外部
©:同上
练习5.19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <iostream> #include <string> #include <vector> using namespace std; void main() { do { string My_string1, My_string2; cout<<"请输入两个字符串"<<endl; cin>>My_string1>>My_string2; if (My_string1.size() > My_string2.size()) { cout<<My_string2<<endl; } else { cout<<My_string1<<endl; } } while (cin); }
|
练习5.20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <iostream> #include <string> #include <vector> using namespace std; void main() { string My_string1, My_string2; do { cout<<"请输入字符串"<<endl; cin>>My_string1; if (My_string1 == My_string2) { cout<<"没有任何单词是重复的"<<endl; break; } else My_string2 = My_string1; } while (cin); }
|
练习5.21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream> #include <string> #include <vector> using namespace std; void main() { string My_string1, My_string2; int flag = 0; cout<<"请输入单词:"; while(cin>>My_string1) { if (My_string1[0] > 'Z' || My_string1[0] < 'A') { My_string2 = My_string1; cout<<"请继续输入单词:"; continue; } if (My_string1 == My_string2) { cout<<"重复单词为:"<<My_string1<<endl; flag = 1; break; } else My_string2 = My_string1; cout<<"请继续输入单词:"; } if (flag == 0) { cout<<"无重复单词出现"<<endl; } system("pause"); }
|