《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. .net core之编辑json配置文件

    .net core之编辑json配置文件 引言 最近在具体项目开发应用中,项目采用的json格式配置文件,配置文件的加载采用的IConfiguration接口对象进行的管理,这是.net standa ...

  2. c#数字图像处理(九)图像镜像

    private void mirror_Click(object sender, EventArgs e) { if (curBitmap!=null) { mirror mirForm = new ...

  3. mac电脑下使用fcrackzip破解zip压缩文件密码

    fcrackzip简介 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具小巧方便.破解速度快,能使用字典和指定字符集破解,适用于linux.mac osx 系统 fcrackzip安 ...

  4. Oracle批量创建同义词

    一.介绍 Oracle的同义词(synonyms)从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系.它可以节省大量的数据库空间,对不同用户的操作同一张表没有多少差别;它扩展了数据库的使用 ...

  5. 类与 Object 的应用

    # 类与 Object 的应用 + 面试题 类介绍 Java 程序是由若干个类组成的,类也是面向对象编程思想的具体实现. 以下为类的基本使用: public class Cat { // 私有属性 p ...

  6. 1759: 学生信息插入(武汉科技大学结构体oj)(已AC)

    #include<stdio.h>struct student { long no; char name[9]; int score;} t;void input(struct stude ...

  7. centos7安装lnmp

    一.配置CentOS 第三方yum源(CentOS默认的标准源里没有nginx软件包) [root@localhost ~]# yum install wget #安装下载工具wget [root@l ...

  8. LUAMD5加密

    md5里的方法: C:\Windows\System32>lua Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio > require( ...

  9. 脚本、脚本语言、shell脚本

    脚本是批处理文件的延伸,是一种纯文本保存的程序,一般来说的计算机脚本程序是确定的一系列控制计算机进行运算操作动作的组合,在其中可以实现一定的逻辑分支等.脚本程序相对一般程序开发来说比较接近自然语言,可 ...

  10. BZOJ1257 [CQOI2007]余数之和 (数论分块)

    题意: 给定n, k,求$\displaystyle \sum_{i=1}^nk\;mod\;i$ n,k<=1e9 思路: 先转化为$\displaystyle \sum_{i=1}^n(k- ...