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( ...
随机推荐
- poj_3250 单调栈
题目大意 N头牛排成一列,每头牛A都能看到它前面比它矮的牛i,若前面有一个比他高的牛X,则X之前的牛j,A都无法看到.给出N头牛的高度,求出这N头牛可以看到牛的数目的总数. 题目分析 画图之后,可以很 ...
- nginx的allow和deny配置
转自:http://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/ 单看nginx模块名ngx_http_access_module ...
- html 标签转译反转译
如果要在 html 标签内原样插入带 html 标签的文字,通常都会被自动解析掉,比如: document.getElementsByTagName('div')[0].innerHTML=" ...
- CodeForces 731A Night at the Museum
A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...
- poj1015 Jury Compromise【背包】
Jury Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions:32355 Accepted:8722 ...
- document.compatMode介绍
来自:http://www.cnblogs.com/fullhouse/archive/2012/01/17/2324706.html 问题描述:看到阮一峰的博客里面的代码使用到了document.c ...
- 运用JS设置cookie、读取cookie、删除cookiev
JS设置cookie: 假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为: document.cookie=&q ...
- [linux基础学习]默认的目录介绍
以下用一个表格来罗列linux默认的目录或文件及其用途: 目录/文件 用途 来源 / /处于Linux文件系统树形结构的最顶端,它是Linux文件系统的入口,所有的目录.文件.设备都在/之下. - / ...
- android开发笔记(二)导入项目到eclipse和另一个项目
NND,eclipse里导入工程出现问题了,整了半天,来个这问题,无效工程描述,找了半天看.projec文件是否工程名对应,看androidManifest.XML换里面的代码版本号,我擦都无济于事. ...
- Appium+python移动端自动化测试-环境搭建(一)
搭建所在系统环境:Windows7版本64位系统 一.环境准备 jdk8.0.151 android-sdk_r20.3.4-windows python3.5 appium1.4.16.1 Node ...