《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;
}

《C++Primer》第五版习题答案--第五章【学习笔记】的更多相关文章

  1. 《C++Primer》第五版习题答案--第六章【学习笔记】

    <C++Primer>第五版习题答案--第六章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/16 第六章:函数 ...

  2. 《C++Primer》第五版习题答案--第三章【学习笔记】

    [C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...

  3. 《C++Primer》第五版习题解答--第四章【学习笔记】

    [C++Primer]第五版习题解答--第四章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/11 第四章:表达式 练习4. ...

  4. 《C++Primer》第五版习题答案--第一章【学习笔记】

    C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...

  5. 《C++Primer》第五版习题答案--第二章【学习笔记】

    C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...

  6. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  7. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  8. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  9. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

随机推荐

  1. Centos7安装 Anaconda + jupyter notebook

    一.安装Anaconda 1 下载Anaconda安装脚本 为了避免漫长的等待,镜像源选择国内的清华镜像源,镜像源地址:https://mirrors.tuna.tsinghua.edu.cn/ana ...

  2. Web自动化测试项目搭建目录

    Web自动化测试项目搭建(一) 需求与设计 Web自动化测试项目(二)BasePage实现 Web自动化测试项目(三)用例的组织与运行 Web自动化测试项目(四)测试报告 Web自动化测试项目(五)测 ...

  3. Mybatisplus代码生成器主类CodeGenerator配置

    //代码自动生成public class CodeGenerator { /** * <p> * 读取控制台内容 * </p> */ public static String ...

  4. 使用nutz框架,找不到入口函数,访问Url报404

    案例 今天在跟着nutz框架教程去配置demo时,发现访问URL找不到入口函数,出现了Search mapping for path=/user/count : NOT Action match 异常 ...

  5. Docker快速上手之搭建SpringBoot项目

    Docker是基于Go语言实现的云开源项目. Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装.分发.部署.运行等生命周期 ...

  6. 1757: 成绩稳定的学生(武汉科技大学结构体oj)

    #include<stdio.h>#include<string.h>struct student{ long no; char name[9]; int ch[20]; in ...

  7. 实验14:VLAN间的路由

    实验11-1: 单臂路由实现VLAN 间路由 Ø    实验目的通过本实验,读者可以掌握如下技能:(1) 路由器以太网接口上的子接口(2) 单臂路由实现VLAN 间路由的配置Ø    实验拓扑 实验步 ...

  8. 中国天气网API接口

    http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...

  9. Qt实现简易计算器

    麻烦到不能再麻烦的实现,简单到不能再简单的思路. calc.h #ifndef CALC_H #define CALC_H #include <QtWidgets/QMainWindow> ...

  10. C++ 中库函数bsearch的简单研究(含示例)

    /**//*bsearch函数声明如下: void *bsearch(const void *key, const void *base, size_t *nelem,                 ...