习题答案目录: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. Flutter(一)安装配置和几个注意点

    Flutter(一)安装配置和几个注意点 记住要仰望星空,不要低头看脚下.无论生活如何艰难,请保持一颗好奇心.你总会找到自己的路和属于你的成功. 愿您的来世灵魂依旧,躯体康健. 一句话来形容Flutt ...

  2. Forth 输入流处理

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  3. lenet-5,Alexnet详解以及tensorflow代码实现

    http://blog.csdn.net/OliverkingLi/article/details/73849228

  4. xenserver挂载新硬盘

    注意:新加硬盘请不要加入raid,否则不认盘 一: 1.1:查看磁盘列表 fdisk -l [root@xenserver zz]# fdisk -l Disk /dev/sdb: 7999.4 GB ...

  5. 二、JAVA基本数据类型:内置数据类型,引用类型

    变量的值存储在内存中,内存管理系统通过变量的类型分配存储空间,且该空间只能存储该类型数据,通过定义不同的变量,在内存中储存不同类型的数据. JAVA的两大数据类型 1. 内置数据类型 2.引用数据类型 ...

  6. ionic1 添加百度地图插件 cordova-plugin-baidumaplocation

    cordova-plugin-baidumaplocation 这个插件返回的数据是 json 格式的  可以直接获取  android 和 ios 都可用 1.先去百度地图平台去创建应用  获取访问 ...

  7. python 环境下 安装 gdal

    起因:需要做一个城市扩张的东西,然后再GitHub上下载了一段代码,不过作者没怎么说清楚要怎么用,早上琢磨半天,归结到我需要先下载python的gdal模块. 关于:搜索下来,发现gdal(Geosp ...

  8. 产品经理面试题——浅谈O2O

    分析:O2O也要分种类.      现在的O2O 已经是线上线下相互融合的阶段了,无论是线上体验,线下消费还是线下体验,线上下单.都已有比较成熟的模式.我对O2O的理解就是以消费者为中心,整合线上和线 ...

  9. svn同步小脚本

    企业中难免会有使用svn 的怎么给svn做好备份呢 下面是用两个脚本进行同步和监控 同步脚本: #!/bin/bash i=1 while true do if [ $(expr $i % 12) = ...

  10. sklearn.preprocessing.LabelBinarizer

    sklearn.preprocessing.LabelBinarizer