《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. 智能指针shared_ptr使用学习

    当需要shared_ptr实现向上向下转换时,可以使用 dynamic_pointer_cast 来进行转换 下面是例子: #include <memory> using namespac ...

  2. Object类、常用API_2

    主要内容 Object类 Date类 DateFormat类 Calendar类 System类 StringBuilder类 包装类 学习目标 -[ ] 能够说出Object类的特点 -[ ] 能够 ...

  3. 创建dynamics CRM client-side (八) - 获取attribute的值 和 设置disable

    大家可以用下面的方式来获取attribute的值 formContext.getAttribute("address1_shippingmethodcode").getText() ...

  4. Liunx(centos8)下的yum的基本用法和实例

    yum 命令 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的 ...

  5. springboot集成quartz实现任务调度

    quartz 概述 特点 强大的调度功能 灵活的应用方式 分布式和集群能力 用到的设计模式 Builder 模式 factory模式 组件模式 链式写法 体系结构 调度器 任务 触发器 架构图 spr ...

  6. ios---photo实现保存图片到自定义相册

    #import "XMGSeeBigPictureViewController.h" #import "XMGTopic.h" #import <SVPr ...

  7. 【WPF学习】第二十六章 Application类——应用程序的生命周期

    在WPF中,应用程序会经历简单的生命周期.在应用程序启动后,将立即创建应用程序对象,在应用程序运行时触发各种应用程序事件,你可以选择监视其中的某些事件.最后,当释放应用程序对象时,应用程序将结束. 一 ...

  8. list练习题—输入工人信息

    1) 创建一个List,在List 中增加三个工人,基本信息如下: 姓名 年龄 工资 zhang3 18 3000 li4 25 3500 wang5 22 3200 2) 在li4 之前插入一个工人 ...

  9. 使用 FRP 让部门同事都能直接远程桌面办公( 适用于 TEAM 和向日葵卡顿的用户)

    背景说明 这两天由于疫情的原因,很多公司都得在家远程上班,然后像我们这类小公司有没有 VPN 这些东西.传统的远程回公司只能依靠 Teamviewer 或者向日葵等工具.但是由于最近用户量很多,可能会 ...

  10. 使用html及CSS实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部:

    使用html及CSS实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部: <!DOCTYPE html><html lang="en">< ...