《C++Primer》第五版习题答案 -- 第五章 [学习笔记]
ps: 答案是个人在学习过程中书写, 可能存在错漏之处, 仅作参考.
作者: cosefy
Date: 2020/1/15
第三章: 语句
练习 5.3:
代码可读性降低了.
- while(val<=10)
- sum+=val,++val;
练习 5.4:
iter 未初始化.
if 语句中的 status 超过作用范围, 且 status 在 while 中进行了判断.
练习 5.5:
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- //vector<string> str{ "甲","乙","丙" };
- int score;
- string rank;
- while (cin>> score)
- {
- if (score <60)
- rank = "丙";
- else
- if (score < 90)
- rank = "乙";
- else
- rank = "甲";
- cout << rank << endl;
- return 0;
- }
- }
练习 5.6:
rank = score> 90 ? "甲" : score> 60 ? "乙":"丙";
练习 5.7:
ival2 后少了分号.
忘记加上花括号
第二个 if 语句中的 ival 超过了作用范围, 且第一个 if 语句已经进行了判断.
if 语句中条件永远为 False.
练习 5.9:
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- int a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count = 0;
- int other_count = 0;
- char c;
- while (cin>> c)
- if (c == 'a')
- ++a_count;
- else if (c == 'e')
- ++e_count;
- else if (c == 'i')
- ++i_count;
- else if (c == 'o')
- ++o_count;
- else if (c == 'u')
- ++u_count;
- else
- ++other_count;
- cout <<"a:" << a_count << endl;
- cout << "e:" << e_count << endl;
- cout << "i:" << i_count << endl;
- cout << "o:" << o_count << endl;
- cout << "u:" << u_count << endl;
- return 0;
- }
练习 5.10:
代码形式如下所示:
- case 'a':
- case'A':
- ++a_count;
- break;
练习 5.11:
修改 while 判断语句为:
while (cin>>noskipws>> c)
这样不会忽略空白, 另外加上统计制表符等的 case 语句就可以了.
练习 5.12:
保留上一个字符, 如果当前字符为'f'或'i'或'l', 则判断上个字符是否是'f'.
练习 5.13:
忘记 break
ix 定义位置错误
case 后面接整型常量表达式
case 后面是整型常量, 可以用 const 修饰 ival,jval,kval.
练习 5.14:
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- string now_word, last_word,record_word;
- int max = 1,count=1,flag=0;
- while (cin>> now_word)
- {
- if (now_word == last_word) {
- ++count;
- last_word = now_word;
- }
- else
- {
- if (max <count)
- {
- max = count;
- record_word = last_word;
- }
- count = 1;
- last_word = now_word;
- flag = 1; // 标记出现了不重复的单词
- }
- }
- if (flag==0)
- cout << "仅有单词"<<now_word << "出现了" << count << "次" << endl;
- else
- cout << record_word << "出现了"<<max << "次" << endl;
- return 0;
- }
练习 5.15:
ix 定义位置错误, 应在 for 循环外定义
缺少一个分号
无限循环
练习 5.17:
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- vector<int>v1{ 1,0,2,2,3,5 };
- vector<int>v2{ 1,0,2,2, };
- auto it1 = v1.begin();
- auto it2 = v2.begin();
- for (; it2 != v2.end(); ++it1, it2++)
- if (*it2 != *it1)
- break;
- cout <<(it2 == v2.end() ? "YES" : "NO");
- return 0;
- }
练习 5.18:
do 语句块忘记花括号
while 条件语句不用来定义变量
变量应定义在循环体外
练习 5.20:
- #include<iostream>
- #include<vector>
- using namespace std;
- int main()
- {
- string last_word, accur_word;
- bool flag = false;
- while (cin>> accur_word)
- {
- if (accur_word == last_word)
- {
- cout <<accur_word << endl;
- break;
- }
- else
- {
- last_word = accur_word;
- flag = true;
- }
- }
- if (!flag)
- cout << "不连续重复" << endl;
- return 0;
- }
练习 5.23:
- #include<iostream>
- using namespace std;
- int main()
- {
- int m, n;
- cin>> m>> n;
- cout <<"m/n:"<<m / n << endl;
- return 0;
- }
练习 5.24:
练习 5.25:
- #include<iostream>
- using namespace std;
- int main()
- {
- int m, n;
- while (cin>> m>> n)
- {
- try
- {
- if (n == 0)
- throw runtime_error("除数不可为 0");
- cout <<"m/n:" << m / n << endl;
- }
- catch (runtime_error err)
- {
- cout << err.what()
- << "\nTry Again? Enter Y or N" << endl;
- char c;
- cin>> c;
- if (!cin||c == 'N')
- break;
- }
- }
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3382473.html