C++Primer第五版——习题答案详解(四)
习题答案目录: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第五版——习题答案详解(四)的更多相关文章
- C++Primer第五版——习题答案详解(一)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- C++Primer第五版——习题答案详解(五)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- C++Primer第五版——习题答案详解(七)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...
- C++Primer第五版——习题答案详解(八)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...
- C++Primer第五版——习题答案详解(九)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...
- C++Primer第五版——习题答案详解(十)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...
随机推荐
- restore not found的错误
tensorflow保存模型后,restore的时候报参数not found是什么原因呢 一般预测的流程是:建图然后restore参数,很有可能你的变量作用域和train的时候不一样,那么在现在的变量 ...
- a*b高精度数组算法
#include<stdio.h> #include<string.h> int main() { ]={},b[]={},c[]={},len1=,len2=; ],str2 ...
- vue-router中query和params传参(接收参数)以及$router、$route的区别
query传参: this.$router.push({ path:'/...' query:{ id:id } }) 接收参数:this.$route.query.id params传值: 传参: ...
- zabbix3.4.7Web页面监控
1. 新增Web monitoring 2. 新增一个触发器Trigger 点击“Insert”以后,完成后点击页面下的Add之后,可以看见如下 3. 测试-检测告警效果 接下来,我在Nginx上将i ...
- Vue(八) 数字输入框组件案例
数字输入框是对普通输入框的扩展,用来快捷输入一个标准的数字,如图: 代码: <div id="app"> <input-number v-model=" ...
- python四
三元运算 name = "张三" if 1 == 2 else "李四" print(name) name1 = "张三" if 1 == ...
- redis 集群常用命令
systemctl start redis.service #redis 启动redis-server /etc/redis.conf #redis 加载配置文件启动 redis-cli -h 192 ...
- Problem A: 平面上的点和线——Point类、Line类 (I)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段.现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作. 根据“append ...
- elasticsearch 使用快照进行备份
Elasticsearch也提供了备份集群中索引数据的策略——snapshot API.它会备份整个集群的当前状态和数据,并保存到集群中各个节点共享的仓库中.这个备份的进程是增量备份的,在第一次备份的 ...
- 基于Verilog的按键检测实验
一.模块框图及基本思路 detect_module:检测按键输入脚的电平边沿变化 delay_10ms_module:延时消抖,输出按键有效信号 debounce_module:前两个模块的组合模块 ...