p159~p164:
switch语句
1、例程:统计文本中五个元音字母出现的次数。(利用输入输出重定向测试)

$ a <input.txt>output.txt
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
// unsigned int 可以缩写为 unsigned
unsigned aCnt = , eCnt = , iCnt = , oCnt = , uCnt = ;
char ch;
while (cin >> ch) {
switch (ch) {
case 'a':
++aCnt;
break;
case 'e':
++eCnt;
break;
case 'i':
++iCnt;
break;
case 'o':
++oCnt;
break;
case 'u':
++uCnt;
break;
}
}
cout << "Number of vowel a: " << aCnt << '\n'
<< "Number of vowel e: " << eCnt << '\n'
<< "Number of vowel i: " << iCnt << '\n'
<< "Number of vowel o: " << oCnt << '\n'
<< "Number of vowel u: " << uCnt << endl;
return ;
}

input.txt

 In the year  I took my degree of Doctor of Medicine of the
University of London, and proceeded to Netley to go through the
course prescribed for surgeons in the army. Having completed my
studies there, I was duly attached to the Fifth Northumberland
Fusiliers as Assistant Surgeon. The regiment was stationed in India
at the time, and before I could join it, the second Afghan war had
broken out. On landing at Bombay, I learned that my corps had
+
advanced through the passes, and was already deep in the enemy's
country. I followed, however, with many other officers who were in
the same situation as myself, and succeeded in reaching Candahar in
safety, where I found my regiment, and at once entered upon my new
duties.

output.txt

Number of vowel a:
Number of vowel e:
Number of vowel i:
Number of vowel o:
Number of vowel u:

2、case label必须整型常量表达式

3、switch内部的控制流:如果没有break;将从该标签往后顺序执行所有的case分支。

4、default label之后必须至少跟上一个空语句。

5、switch内部尽量不要定义变量。

练习 5.9

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
// unsigned int 可以缩写为 unsigned
unsigned aCnt = , eCnt = , iCnt = , oCnt = , uCnt = ;
char ch;
while (cin >> ch) {
if (ch == 'a') ++aCnt;
if (ch == 'e') ++eCnt;
if (ch == 'i') ++iCnt;
if (ch == 'o') ++oCnt;
if (ch == 'u') ++uCnt;
}
cout << "Number of vowel a: " << aCnt << '\n'
<< "Number of vowel e: " << eCnt << '\n'
<< "Number of vowel i: " << iCnt << '\n'
<< "Number of vowel o: " << oCnt << '\n'
<< "Number of vowel u: " << uCnt << endl;
return ;
}

练习 5.10

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
// unsigned int 可以缩写为 unsigned
unsigned aCnt = , eCnt = , iCnt = , oCnt = , uCnt = ;
char ch;
while (cin >> ch) {
if (ch == 'a' || ch == 'A') ++aCnt;
if (ch == 'e' || ch == 'E') ++eCnt;
if (ch == 'i' || ch == 'I') ++iCnt;
if (ch == 'o' || ch == 'O') ++oCnt;
if (ch == 'u' || ch == 'U') ++uCnt;
}
cout << "Number of vowel a: " << aCnt << '\n'
<< "Number of vowel e: " << eCnt << '\n'
<< "Number of vowel i: " << iCnt << '\n'
<< "Number of vowel o: " << oCnt << '\n'
<< "Number of vowel u: " << uCnt << endl;
return ;
}

练习 5.11

    while (cin >> ch) {
if (ch == 'a') ++aCnt;
if (ch == 'e') ++eCnt;
if (ch == 'i') ++iCnt;
if (ch == 'o') ++oCnt;
if (ch == 'u') ++uCnt;
if (ch == ' ') ++blankCnt;
if (ch == '\t') ++tabCnt;
if (ch == '\n') ++nCnt;
}

练习 5.12

没按题意做,另外题目中没有规定ffff怎么处理。

#include <iostream>
using namespace std;
int main()
{
unsigned nff, nfl, nfi;
nff = nfl = nfi = ;
char ch;
bool lastf = false;
while (cin >> ch) {
if (ch == 'f' && lastf == false) {
lastf = true;
continue;
}
if (lastf) {
if (ch == 'f') {
++nff;
}
if (ch == 'l') {
++nfl;
}
if (ch == 'i') {
++nfi;
}
}
}
cout << "nff=" << nff << '\n'
<< "nfl=" << nfl << '\n'
<< "nfi=" << nfi << '\n' << endl;
return ;
}

练习 5.13

a - 漏掉break

b - 在switch内定义变量有可能被跳过,所以是非法的

c - 非法的形式,编译器通过不了。正确的方式为case 'a': case 'b': case 'd':....

d - case label必须整型常量表达式。

c++第三十一天的更多相关文章

  1. Bootstrap <基础三十一>插件概览

    在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...

  2. COJ969 WZJ的数据结构(负三十一)

    WZJ的数据结构(负三十一) 难度级别:D: 运行时间限制:3000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 A国有两个主基站,供给全国的资源.定义一个主基站 ...

  3. NeHe OpenGL教程 第三十一课:加载模型

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. 三十一、Java图形化界面设计——布局管理器之GridLayout(网格布局)

    摘自http://blog.csdn.net/liujun13579/article/details/7772491 三十一.Java图形化界面设计--布局管理器之GridLayout(网格布局) 网 ...

  5. JAVA之旅(三十一)——JAVA的图形化界面,GUI布局,Frame,GUI事件监听机制,Action事件,鼠标事件

    JAVA之旅(三十一)--JAVA的图形化界面,GUI布局,Frame,GUI事件监听机制,Action事件,鼠标事件 有段时间没有更新JAVA了,我们今天来说一下JAVA中的图形化界面,也就是GUI ...

  6. Java进阶(三十一) Web服务调用

    Java进阶(三十一) Web服务调用 前言 有朋友问了一个问题:如何调用已知的音乐服务接口,服务文档如下: https://www.evernote.com/shard/s744/sh/c37cd5 ...

  7. Gradle 1.12用户指南翻译——第三十一章. FindBugs 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  8. SQL注入之Sqli-labs系列第三十关(基于WAF防护的双引号报错注入)和三十一关

    开始挑战第三十关和三十一关(Protection with WAF) 0x1 前言 这关其实和29关是同样的,login.php页面存在防护,只要检测到存在问题就跳转到hacked.php页面,不同的 ...

  9. “全栈2019”Java多线程第三十一章:中断正在等待显式锁的线程

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  10. python第三十一课--递归(2.遍历某个路径下面的所有内容)

    需求:遍历某个路径下面的所有内容(文件和目录,多层级的) import os #自定义函数(递归函数):遍历目录层级(多级) def printDirs(path): dirs=os.listdir( ...

随机推荐

  1. Zabbix的API的使用

    上一篇:Zabbix低级主动发现之MySQL多实例 登录请求(返回一个token,在后面的api中需要用到) curl -s -X POST -H 'Content-Type:application/ ...

  2. hdu2254 奥运 矩阵的应用

    hdu2254 奥运 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2254 题意:题目让我们求得是的可以得到的金牌数量,而和金牌数量=在t1到t2天( ...

  3. DocumentRoot \

    w  有无\的区别. https://httpd.apache.org/docs/2.4/vhosts/examples.html hosts 127.0.0.1 w.w httpd.conf Ser ...

  4. 在HTML里面HEAD部分的META元素要表达的内容是什么

    1.name属性主要有以下几种参数: A.Keywords(关键字) 说明:keywords用来告诉搜索引擎你网页的关键字是什么. 举例:<meta name ="keywords&q ...

  5. 向git服务器添加shh公钥

    步骤一,从客户端获得 SSH 公钥 为了使客户端可以向 Git 服务器提供 SSH 公钥,首先要确认客户端拥有公钥.SSH 的密钥存储在 ~/.ssh/ 目录下,下面我们查看一下这里面都有哪些文件: ...

  6. GNU Screen使用入门

    前些天开始学习使用GNU Screen程序,发现这个工具在管理服务器时候确实挺方便的,于是写一篇文章总结一下,顺便介绍Screen的基本使用方法. 简介 GNU Screen是 一个基于文本的全屏窗口 ...

  7. awk经常使用字符串处理函数

    gsub(regexp, replacement [, target]) Search target for all of the longest, leftmost, nonoverlapping  ...

  8. 如何制作一款HTML5 RPG游戏引擎——第五篇,人物&人物特效

    上一次,我们实现了对话类,今天就来做一个游戏中必不可少的——人物类. 当然,你完全是可以自己写一个人物类,但是为了方便起见,还是决定把人物类封装到这个引擎里. 为了使这个类更有意义,我还给人物类加了几 ...

  9. 机器学习算法(优化)之一:梯度下降算法、随机梯度下降(应用于线性回归、Logistic回归等等)

    本文介绍了机器学习中基本的优化算法—梯度下降算法和随机梯度下降算法,以及实际应用到线性回归.Logistic回归.矩阵分解推荐算法等ML中. 梯度下降算法基本公式 常见的符号说明和损失函数 X :所有 ...

  10. 微信js-sdk使用

    <?php $appid=""; $secret=""; class JSSDK { private $appId; private $appSecret ...