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( ...
随机推荐
- jsp导出到Excel
jsp模板文件 <%@ page isELIgnored="false" contentType="application/x-msdownload; charse ...
- pure
Pure也是一款很出色的CSS框架,之前分享的Bootstrap是由Twitter出品的,而Pure是来自雅虎的.尽管从UI界面效果上来说,Pure没有Bootstrap那样精美,但Pure是纯CSS ...
- SDL 威胁建模工具入门 threat modeling tool
http://msdn.microsoft.com/zh-cn/magazine/dd347831.aspx threat modeling tool 威胁建模工具 minifuzz 文件模糊工具 c ...
- R语言中的一些函数
1.控制输出数字的精度 format(123.123,digits=4) 输出4位数字123.1,如果整数超过4位,小数部分就全被略去. options(digits=4) 功能同上,不过在Rsess ...
- 170316、spring4:@Cacheable和@CacheEvict实现缓存及集成redis
注:1.放入cache中,采用@Cacheable;使缓存失效@CacheEvict 2.自定义CacheManager只需要继承org.springframework.cache.support.A ...
- 权限认证与授权(Shrio 框架)
权限概述 认证: 系统提供的用于识别用户身份的功能, 通常登录功能就是认证功能; -- 让系统知道你是谁 授权: 系统授予用户可以访问哪些功能的证书. -- 让系统知道你能做什么! 常见的权限控制方式 ...
- log4cpp简单示例
log4cpp简单示例 下载地址 Sample.cpp #include <iostream> #include <log4cpp/FileAppender.hh> #incl ...
- HBase-MR
一.需求1:对一张表的rowkey进行计数 官方HBase-Mapreduce 需求1:对一张表的rowkey进行计数 1)导入环境变量 export HBASE_HOME=/root/hd/hbas ...
- Amr and Chemistry---cf558C(暴力,加技巧)
题目链接:http://codeforces.com/problemset/problem/558/C 题意:有n个数,每个数都可以进行两个操作 *2 或者 /2,求最小的操作次数和,使得所有的数都相 ...
- Linux下桥接模式详解一
注册博客园已经好长时间,一直以来也没有在上面写过文章,都是随意的记录在了未知笔记上,今天开始本着分享和学习的精神想把之前总结的笔记逐步分享到博客园,和大家一起学习,一起进步吧! 2016-09-20 ...