习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第5章 语句


练习5.9

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t;
int cnt = 0;
while (cin >> t) {
if (t == 'a' || t == 'e' || t == 'i' || t == 'o' || t == 'u') {
cnt++;
}
}
cout << cnt;
return 0;
}

练习5.10

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
while (cin >> t) {
switch (t) {
case'a':
case'A':
++aCnt;
break;
case'e':
case'E':
++eCnt;
break;
case'i':
case'I':
++iCnt;
break;
case'o':
case'O':
++oCnt;
break;
case'u':
case'U':
++uCnt;
break;
} }
cout << "The number of vowel a(A):" << aCnt << endl;
cout << "The number of vowel e(E):" << eCnt << endl;
cout << "The number of vowel i(I):" << iCnt << endl;
cout << "The number of vowel o(O):" << oCnt << endl;
cout << "The number of vowel u(U):" << uCnt << endl;
system("pause");
return 0;
}

练习5.11

noskipws:no skip whitespace

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,newlineCnt=0,tabCnt=0;
while (cin >> noskipws >> t) {
switch (t) {
case'a':
case'A':
++aCnt;
break;
case'e':
case'E':
++eCnt;
break;
case'i':
case'I':
++iCnt;
break;
case'o':
case'O':
++oCnt;
break;
case'u':
case'U':
++uCnt;
break;
case'\n':
++newlineCnt;
break;
case'\t':
case'\v':
++tabCnt;
break;
} }
cout << "The number of vowel a(A):" << aCnt << endl;
cout << "The number of vowel e(E):" << eCnt << endl;
cout << "The number of vowel i(I):" << iCnt << endl;
cout << "The number of vowel o(O):" << oCnt << endl;
cout << "The number of vowel u(U):" << uCnt << endl;
cout << "The number of newline:" << newlineCnt << endl;
cout << "The number of tab:" << tabCnt << endl;
system("pause");
return 0;
}

练习5.12

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t,p;
int ffCnt=0,fiCnt=0,flCnt=0;
while (cin >> t) {
if (t == 'f') {
cin >> p;
switch (p) {
case'f':
++ffCnt;
break;
case'l':
++flCnt;
break;
case'i':
++fiCnt;
break;
}
}
}
cout << "The number of ff:" << ffCnt << endl;
cout << "The number of fl:" << flCnt << endl;
cout << "The number of fi:" << fiCnt << endl;
system("pause");
return 0;
}

练习5.13

a.没有break;

b.case1中包含变量的定义,如果越过case1会出错

c.应该改为case1: case3: case5格式

d.case标签必须是常量表达式,改为const unsigned ival.....

练习5.14

#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main() {
string nowString, lastString, resString;
int nowNum = 1, resNum = -1;
while (cin >> nowString) {
if (nowString == lastString) {
nowNum++;
if (nowNum > resNum) {
resString = nowString;
resNum = nowNum;
}
}
else {
nowNum = 1;
lastString = nowString;
}
}
if(resNum > 1) cout << resString << " " << resNum;
else cout << "None";
system("pause");
return 0;
}

练习5.17

#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool compare(vector<int> a, vector<int> b) {
for (decltype(a.size()) i = 0, Sa = a.size(), Sb = b.size();i != Sa && i!=Sb;++i) {
if (a[i] != b[i]) {
cout << "false" << endl;
return false;
}
}
cout << "true" << endl;
return true; }
int main() {
vector<int> a = { 0,1,1,2 };
vector<int> b = { 0,1,1,2,3,5,8 };
vector<int> c = { 0,1,2,5,5 };
compare(a, b);
compare(a, c); system("pause");
return 0;
}

练习5.18

a.少了花括号

b.变量申明放在了do的条件部分

c.变量申明必须定义在循环体外

练习5.19

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string a, b;
do {
cout << "Please input two strings:"<<endl;
cin >> a >> b;
cout << (a.size() < b.size() ? a : b) << endl;
} while (cin);
system("pause");
return 0;
}

练习5.20

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string now, pre;
bool flag = false;
while (cin >> now) {
if (now == pre) {
cout << now <<endl;
flag = true;
break;
}
else {
pre = now;
}
}
if (!flag) cout << "None" << endl;
system("pause");
return 0;
}

练习5.21

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string now, pre;
bool flag = false;
while (cin >> now) {
if (now == pre) {
if (!isupper(now[0])) continue;
cout << now <<endl;
flag = true;
break;
}
else {
pre = now;
}
}
if (!flag) cout << "None" << endl;
system("pause");
return 0;
}

练习5.22

do{
int sz=get_size();
}while(sz<=0);

练习5.25

#include<iostream>
#include<string>
#include<vector>
#include<stdexcept>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) {
try {
if (b == 0) {
throw runtime_error("除数不能为0\n");
}
else {
cout << a / b << endl;
}
}
catch (runtime_error err) {
cout << err.what() << "Try Again? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n') {
break;
}
}
}
system("pause");
return 0;
}

C++Primer第五版——习题答案详解(四)的更多相关文章

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

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

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

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

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

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

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...

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

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...

随机推荐

  1. 数据结构与算法之PHP排序算法(堆排序)

    一.堆的定义 堆通常是一个可以被看做一棵树的数组对象,其任一非叶节点满足以下性质: 1)堆中某个节点的值总是不大于或不小于其父节点的值: 每个节点的值都大于或等于其左右子节点的值,称为大顶堆.即:ar ...

  2. zabbix3.4.7常用监控项

    Zabbix中内置了很多监控参数(Key_),可以获取监控对象中的系统.CPU.网络.内存.文件系统等信息.下面就详细介绍一下这些监控参数的意义. 1. 测试获取监控参数内容的方法 在Zabbix S ...

  3. Java 代理

    代理做一个简单的抽象: 代理模式包含如下角色: Subject:抽象主题角色.可以是接口,也可以是抽象类. RealSubject:真实主题角色.业务逻辑的具体执行者. ProxySubject:代理 ...

  4. 指导手册02:伪分布式安装Hadoop(ubuntuLinux)

    指导手册02:伪分布式安装Hadoop(ubuntuLinux)   Part 1:安装及配置虚拟机 1.安装Linux. 1.安装Ubuntu1604 64位系统 2.设置语言,能输入中文 3.创建 ...

  5. 马凯军201771010116《面向对象程序设计(java)》第六周学习总结

    第一部分:理论知识学习部分 枚举是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁,安全性以及便捷性.创建枚举类型 ...

  6. wpf-xaml-命名空间

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns 为window控件的属性 代表声明引用 如同C ...

  7. 邮件远控电脑MCC-python实现

    本次实现的是一个通过邮件来远程控制电脑,以达到某些远程操作,例如让电脑执行CMD命令,播放音乐,打开指定文件等操作的项目.代码参考了网上的部分教程. 具体流程: 在python代码中,通过一个循环来接 ...

  8. Curl追踪请求延时问题

    背景原因:测试环境发现一个连接内网访问和外网访问延迟差别很大,内网访问很快.外网访问很慢.于是我们用curl来诊断问题所在的区域! 命令如下: curl -o /dev/null -s -w %{ti ...

  9. lettuce行为驱动框架实例

    练习: 一:e1_MyFirstBDD 使用方法的方式实现阶乘的计算 zero.feature: Feature: Compute factorial In order to play with Le ...

  10. 什么是node

    node 编辑 锁定讨论999 本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!   node(结点):网络连接的端点,或两条(或多条)线路的连接点.结点可以是处理器.控制器或 ...