c++第三十一天
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++第三十一天的更多相关文章
- Bootstrap <基础三十一>插件概览
在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...
- COJ969 WZJ的数据结构(负三十一)
WZJ的数据结构(负三十一) 难度级别:D: 运行时间限制:3000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 A国有两个主基站,供给全国的资源.定义一个主基站 ...
- NeHe OpenGL教程 第三十一课:加载模型
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 三十一、Java图形化界面设计——布局管理器之GridLayout(网格布局)
摘自http://blog.csdn.net/liujun13579/article/details/7772491 三十一.Java图形化界面设计--布局管理器之GridLayout(网格布局) 网 ...
- JAVA之旅(三十一)——JAVA的图形化界面,GUI布局,Frame,GUI事件监听机制,Action事件,鼠标事件
JAVA之旅(三十一)--JAVA的图形化界面,GUI布局,Frame,GUI事件监听机制,Action事件,鼠标事件 有段时间没有更新JAVA了,我们今天来说一下JAVA中的图形化界面,也就是GUI ...
- Java进阶(三十一) Web服务调用
Java进阶(三十一) Web服务调用 前言 有朋友问了一个问题:如何调用已知的音乐服务接口,服务文档如下: https://www.evernote.com/shard/s744/sh/c37cd5 ...
- Gradle 1.12用户指南翻译——第三十一章. FindBugs 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- SQL注入之Sqli-labs系列第三十关(基于WAF防护的双引号报错注入)和三十一关
开始挑战第三十关和三十一关(Protection with WAF) 0x1 前言 这关其实和29关是同样的,login.php页面存在防护,只要检测到存在问题就跳转到hacked.php页面,不同的 ...
- “全栈2019”Java多线程第三十一章:中断正在等待显式锁的线程
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- python第三十一课--递归(2.遍历某个路径下面的所有内容)
需求:遍历某个路径下面的所有内容(文件和目录,多层级的) import os #自定义函数(递归函数):遍历目录层级(多级) def printDirs(path): dirs=os.listdir( ...
随机推荐
- webpack配置(一)
这里再配置的时候走了些弯路,现在,把配置前的准备工作做好很重要: 首先,安装node.js,当然,npm也就有了: 其次,安装xampp,主要是为了配置Apache: 安装好后,xampp---htd ...
- 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于AD)
一.进管理中心,创建一个应用程序,配置如下: 二.填端口号,和选择form身份认证,以及填写成员和角色,其他都默认就可以了 三.使用SharePoint 2010 Management Shell在里 ...
- 小程序用scroll-view的scroll-to-view属性实现锚链接跳转
小程序没有锚链接,通过scroll-view可以实现类似锚链接的功能,点击锚链接,滚动条滚动到相应的位置 wxml <view class="wrap"> <!- ...
- 安装TortoiseSVN客户端时遇到的异常
环境:WindowsXP,安装 双击SVN安装程序"TortoiseSVN-1.8.5.25224-win32-svn-1.8.8.msi"后,出现 "无法通过Sindo ...
- Python全栈day10(运算符)
一,运算符 + - * . ** % // 二,in 和not in 一个字符串包含多个字符可以通过in判断字符是否属于改字符串 >>> name = "zhangsan& ...
- protobuf在java应用中通过反射动态创建对象(DynamicMessage)
---恢复内容开始--- 最近编写一个游戏用到protobuf数据格式进行前后台传输,苦于protobuf接受客户端的数据时是需要数据类型的如xxx.parseForm(...),这样就要求服务器在接 ...
- Truncate有外键约束的表
SET FOREIGN_KEY_CHECKS=0; TRUNCATE TABLE table_name; SET FOREIGN_KEY_CHECKS=1;
- Spark Streaming源码分析 – InputDStream
对于NetworkInputDStream而言,其实不是真正的流方式,将数据读出来后不是直接去处理,而是先写到blocks中,后面的RDD再从blocks中读取数据继续处理这就是一个将stream离散 ...
- super()函数的用法
http://www.runoob.com/python/python-func-super.html class FooParent(object): def __init__(self): sel ...
- servlet实现多文件打包下载
当用户一次下载多个文件时.普通情况是,每下载一个文件,均要弹出一个下载的对话框.这给用户造成了非常大不便. 比較理想的情况是,用户选择多个文件后.server后端直接将多个文件打包为zip.以下贴出实 ...