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 ...
随机推荐
- python的闭包与延时绑定问题
起源于一道面试题... def mul(): return [lambda x : i*x for i in range(4)] print([m(2) for m in mul()]) # outp ...
- Annotations
一.介绍 注解,可以将注解看成一种特殊的接口.是一种特殊种类的元数据,它能够关联Java语言中不同元素和结构.有意思的是,在Java生态系统中大多数使用样板XML描述符的地方,注解在消除这些XML描述 ...
- koa和express对比
不同: 1.启动方式不同 koa采用了new Koa()的方式,而express采用传统的函数形式 2.中间件形式二者不一样,这是由二者处理中间件的逻辑差异导致的,实际上这也是二者最根本的差别 3.k ...
- serial front_door signment and gps signment
import socketimport serialimport osimport sysimport struct#serial ser_intf = serial.Serial(port='/de ...
- 厨娘ui设计文档
厨娘ui设计文档 一.概述 中国的饮食文化从古到今源远流长.在生活日益丰富的今天,人们对饮食的要求不仅仅是温饱,更讲究健康和美味.近年来,饮食甚至成为娱乐的一部分,关于吃的流行用语层出不穷,可见在当今 ...
- ubuntu1604使用之旅——安装samba
1.安装samba sudo apt-get install samba 2.安装sambaclient sudo apt-get install smbclient 3.修改配置文件 sudo vi ...
- BULK语句 将TXT数据塞入数据库表格
SET @iSQL=N'BULK INSERT [TEST].[dbo].[TEST_Interim]'+' FROM '+quotename(@fullFileName,'''')+' WITH ( ...
- python爬虫挂代理
以下是GET的方法,使用的代理接口网站是 http://www.xicidaili.com/nn/ #-*- coding:utf-8 -*- from bs4 import BeautifulSou ...
- 剑指Offer 15. 反转链表 (链表)
题目描述 输入一个链表,反转链表后,输出新链表的表头. 题目地址 https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca? ...
- cs特征性以及数据库的连接
笔记c3 五大浏览器内核: Ie浏览器:-ms 火狐:-moz Safari以及chorme:-webkit 欧朋:-o Word-wrap:break-word;设置换行. Border borde ...